Shailesh Vaishampayan
Shailesh Vaishampayan

Reputation: 1796

Not able to Send Message specific to User using Spring Websocket STOMP

I am trying to create a chat app using spring websocket and stomp.
I am using Spring 4.1.1,Stomp.js, ActiveMQ 5.9
In this user can send message to each of his/her friends, who are also logged in, by logging into the app. For sending message to particular user I take following steps:
1) User logs in
2) User subscribes to "/user/queue/messaging" destination.
This will be used to send private messages of users to each other.
3) when user wants to send a message he sends it to destination :
/user/{user_id}/queue/messaging where user_id is recipients user id.
I am trying to send this from client using STOMP.js send method.
4) Expected behaviour : now if recipient is logged in and his session id, for example, is DFT0GH then the message in step e should be delivered to Queue destination with name messaging-userDFT0GH. Instead of this it is delivered to the same user's queue destination who sent it.

Please find my example scenario :
1) User John logs in . He subscribes to /user/queue/messaging
His user id is john
His session id is ABCD01
Queue is created with name on activemq broker as messaging- userABCD01

2) User Alice logs in .
She subscribes to /user/queue/messaging
His user id is alice
Her session id is XYZ01
Queue is created with name on activemq broker as messaging- userXYZ01

3) user John sends a message through STOMP.js send method to Alice using destination as "/user/alice/queue/messaging"

4) now instead of delivering the message to queue messaging- UserXYZ01 it gets delivered to John's queue destination i.e messaging- userABCD01. Why is it so?

When i debugged this , I found following line in method private DestinationInfo parseUserDestination(Message message) of DefaultUserDestinationResolver class :

  if (SimpMessageType.MESSAGE.equals(messageType)) {
        ........
    sessionIds = (sessionId != null ?
                Collections.singleton(sessionId) : this.userSessionRegistry.getSessionIds(user));      
    }

In this sessionId is logged in user's (Principal) session id which is not null as user is logged in and so his sessionIds is returned and message is delivered to his queue even if intended recipient user is different.
When i check usersessionregistry's sessionIds collection I find an entry [alice]:XYZ01.
Shouldn't above line return session id if the user instead of logged in user's session to identify destination queue.?

Sorry I am trying this for the first time. So Please let me know if I miss anything here and of there is
1) any way to satisfy my use case
2) or my use case itself is invalid.

Thanks in advance.

Upvotes: 3

Views: 2215

Answers (1)

Brian Clozel
Brian Clozel

Reputation: 59056

Just so this question stays out of the unanswered list - this is indeed a bug and you raised it as SPR-12444.

This will be fixed in Spring Framework 4.1.3.

As a side note, I'd like to point out that if you're deploying your application with multiple instances, session registries are not shared between instances by default - so this will cause issues when sending a message from a alice (with a session to server #1) to bob (session to server #2).

Upvotes: 2

Related Questions