Websocket Postman

I just wanted to check if someone notice anything wrong in my code.
Basically, what I want to do is that when a new user registers, I want the admins to be notified with "New user has been registered", but I want to do it in real-time using WebSocket.

I'm testing my WebSocket URL ws://localhost:8080/ws, and I successfully connect to it. I also subscribe to the destination:
destination:/topic/admins
id:sub-0


which works fine. But when I register a new user, I don't receive the message in the destination, even though I see it in the terminal 😅 hahaha.
It's weird, but I just wanted to check if someone notice anything off.

WebsocketConfig
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws")
                .setAllowedOrigins("*");
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");
        registry.setApplicationDestinationPrefixes("/app");
    }
}



NotificationService
@Service
public class NotificationService {

    private static final Logger logger = LoggerFactory.getLogger(NotificationService.class);
    private final SimpMessagingTemplate messagingTemplate;

    public NotificationService(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    public void notifyAdmin(String message) {
        logger.info("Sending WebSocket message to /topic/admins: {}", message);
        messagingTemplate.convertAndSend("/topic/admins", message);
        logger.info(" WebSocket message sent successfully!");
    }
}


AuthService
 User user = createUser(request);
        userRepository.save(user);

        System.out.println("User registered: " + user.getFirstname() + " " + user.getLastname());

        notificationService.notifyAdmin("New user registered: " + user.getFirstname() + " " + user.getLastname());
        System.out.println(" WebSocket message sent for user registration!");

        return ResponseEntity.status(HttpStatus.CREATED)
                .body(new AuthenticationResponse("User successfully registered"));
Was this page helpful?