Sending Message to Specific User on Spring Websocket

Sending messages to specific user with Spring Websocket

Can you see that the client subscribes successfully to your endpoint?

I think you are missing the first / in the client code 'user/queue/greetings' should be '/user/queue/greetings'

how to send message to a specific user by username at any application point

You can find answer to a similar question (with project exemple) here :
Spring websocket send to specific people

The fact that user is subscribing once is not a problem. One the connection is established, the server can send has much message as needed.

Spring websocket send to specific people

In your Websocket controller you should do something like this :

@Controller
public class GreetingController {

@Autowired
private SimpMessagingTemplate messagingTemplate;

@MessageMapping("/hello")
public void greeting(Principal principal, HelloMessage message) throws Exception {
Greeting greeting = new Greeting();
greeting.setContent("Hello!");
messagingTemplate.convertAndSendToUser(message.getToUser(), "/queue/reply", greeting);
}
}

On the client side, your user should subscribe to topic /user/queue/reply.

You must also add some destination prefixes :

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic", "/queue" ,"/user");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/user");
}
/*...*/
}

When your server receive a message on the /app/hello queue, it should send a message to the user in your dto. User must be equal to the user's principal.

I think the only problem in your code is that your "/user" is not in your destination prefixes. Your greetings messages are blocked because you sent them in a queue that begin with /user and this prefixe is not registered.

You can check the sources at git repo :
https://github.com/simvetanylen/test-spring-websocket

Hope it works!

Send Notification to specific user in spring boot websocket

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', ...)


Related Topics



Leave a reply



Submit