Send Notification to specific user in spring boot websocket

后端 未结 2 1535
孤街浪徒
孤街浪徒 2021-02-07 11:30

I want to send notification to specific client.

e.g username user

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration exten         


        
相关标签:
2条回答
  • 2021-02-07 11:37

    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', ...)
    
    0 讨论(0)
  • 2021-02-07 11:40

    Seems you are missing a slash in your destination:

    template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications); 
    
    0 讨论(0)
提交回复
热议问题