17

I want to send notification to specific client.

e.g username user

@Configuration @EnableWebSocketMessageBroker public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) { stompEndpointRegistry.addEndpoint("/socket") .setAllowedOrigins("*") .withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker("/topic", "/queue"); registry.setApplicationDestinationPrefixes("/app"); } 

Controller

@GetMapping("/notify") public String getNotification(Principal principal) { String username = "user"; notifications.increment(); logger.info("counter" + notifications.getCount() + "" + principal.getName()); // logger.info("usersend:"+sha.getUser().getName()) ; //user template.convertAndSendToUser(principal.getName(), "queue/notification", notifications); return "Notifications successfully sent to Angular !"; } 

Client-Side

Angular Service

connect() { let socket = new SockJs(`api/socket`); let stompClient = Stomp.over(socket); return stompClient; } 

Angular Component

 let stompClient = this.webSocketService.connect(); stompClient.connect({}, frame => { stompClient.subscribe('/user/queue/notification', notifications => { console.log('test'+notifications) this.notifications = JSON.parse(notifications.body).count; }) }); 

I am have searched many other questions and tried but none of them worked for me e.g here answered by Thanh Nguyen Van and here

Console

 Opening Web Socket... stomp.js:134 Web Socket Opened... stomp.js:134 >>> CONNECT accept-version:1.1,1.0 heart-beat:10000,10000 stomp.js:134 <<< CONNECTED version:1.1 heart-beat:0,0 stomp.js:134 connected to server undefined reminder.component.ts:18 test callsed stomp.js:134 >>> SUBSCRIBE id:sub-0 destination:/user/queue/notification 

thanks in advance .

1
  • I have implemented stomp and it is working, the problem is that when I close my server then I get a warning -- The web application [ROOT] appears to have started a thread named [MessageBroker-4] but has failed to stop it. This is very likely to create a memory leak. Commented Aug 6, 2023 at 19:09

2 Answers 2

13
+50

The answer of gerrytan to Sending message to specific user on Spring Websocket mentions a web socket configuration change, to register the /user prefix. In your case I guess it means to replace

registry.enableSimpleBroker("/topic", "/queue"); 

with

registry.enableSimpleBroker("/topic", "/queue", "/user"); 

He also says that in controller you don't need the /user prefix because it is added automatically. So you could try this:

template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications); 

and this:

template.convertAndSendToUser(principal.getName(), "/user/queue/notification", notifications); 

On the client side you need to provide the username that you used to connect to server. You might insert it directly:

stompClient.subscribe('/user/naila/queue/notification', ...) 

or get it from a header. But Markus says at How to send websocket message to concrete user? that even here you don't need the username, so it might work like this:

stompClient.subscribe('/user/queue/notification', ...) 
Sign up to request clarification or add additional context in comments.

5 Comments

how do we handle closing the websocket connection for a particular user?
What is there to handle? How a connection would be closed for a particular user? Anyway, this is unrelated to the original post, you should post this as a distinct question.
Hi edixon your answer is very helpful. I have implemented stomp and it is working, the problem is that when I close my server then I get a warning -- The web application [ROOT] appears to have started a thread named [MessageBroker-4] but has failed to stop it. This is very likely to create a memory leak.
Your question has several answers here: stackoverflow.com/questions/48254517. I found it at the top when googling for "the web application appears to have started".
Hello thanks for url but it discusses other issue. Were you not getting any thread or memory leak warning after terminating your app locally when you implemented your solution.
7

Seems you are missing a slash in your destination:

template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications); 

2 Comments

Check the Spring Websocket Chat sample, might help you github.com/salmar/spring-websocket-chat
can i find what path has been produced by this line in spring template.convertAndSendToUser(.....) ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.