0

I have implemented a WebSocket server using Spring WebSocket and STOMP. There are multiple subscriptions over a single session and I want to send message to a specific subscription only.

Steps to reproduce:

  1. Client connects to server by calling registered STOMP endpoint. enter image description here
  2. Client makes 2 subscription over the connection.
SUBSCRIBE
country:germany
id:sub-0
destination:/user/queue/countryUpdates
SUBSCRIBE
country:france
id:sub-1
destination:/user/queue/countryUpdates
  1. In SimpUserRegistry there is 1 user and 1 session with 2 subscription.enter image description here

  2. Problem is if I send a message to one subscription it's send to other subscription as well.enter image description here

  3. At the time of sending the message I am adding subscription id in native headers, but it's not working.

SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
accessor.setNativeHeader("id", subscriptionId);
accessor.setNativeHeader("subscription", subscriptionId);
accessor.setSubscriptionId(subscriptionId);
accessor.setLeaveMutable(true);
messagingTemplate.convertAndSendToUser(simpUserId, REPLY_DESTINATION, message, accessor.getMessageHeaders());

What I've tried: If I add country to destination, each subscription have unique destination than there are no duplicate messages.

I want to use id/subscriptionId to determine the subscription and send message to that particular subscription.

kulsin
  • 398
  • 4
  • 18
  • Anyways, I had to use different destination in subscription as it was working properly. – kulsin Jan 15 '21 at 15:31
  • possible [duplicate](https://stackoverflow.com/questions/60488396/how-to-send-message-to-a-specific-subscription-over-spring-websocket-stomp). I have been trying to solve the same issue, the (self-)answer to the duplicate doesn't manage to go further than sending to _session_ (rather than subscription) – almondandapricot Jan 31 '21 at 04:24

0 Answers0