myspri
myspri

Reputation: 293

Multiple user sessions not receiving subscription message - spring stomp websocket

I am using Spring stomp websocket framework to send subscription messages to the clients. We are using ActiveMQ as the message broker and is using a stomp javascript client. Spring 4.1.5 and In this architecture, the messages are sent using

simplemessagingTemplate.convertAndSendToUser(user, "/queue/msg", serverMsg, map);

In order to ensure that only the right user receive their message, I am also making use of a QueueSubscriptionInterceptor that implements ChannelInterceptor. The messages are delivered to the destination correctly. The messages are received using the JS client like this.

  stompClient.subscribe('/user/guest/queue/msg', function(greeting){              
                    x = JSON.parse(greeting.body);
                    ...
                    }

So far so good. However, when there are multiple user session, only one session receives the message. For (e.g), if two "guest" users are logged in, I would like all the two "guest" users to receive the message. This doesnt seem to be happening. Looking into the logs, I see that the message seems to be sent..

2015-04-11 14:39:40 DEBUG StompBrokerRelayMessageHandler:738 - Forwarding SEND /queue/msg-user1 session=_system_ application/json;charset=UTF-8 payload={"my message to you...)
2015-04-11 14:39:40 DEBUG StompBrokerRelayMessageHandler:738 - Forwarding SEND /queue/msg-user0 session=_system_ application/json;charset=UTF-8 payload={"my message to you...)

I see only one client receiving the message and not the other. Reading the Spring documentation, I understand that this is the default behaviour. Can someone tell me what I am doing wrong.

Thanks.

Upvotes: 0

Views: 2676

Answers (2)

Malkeith Singh
Malkeith Singh

Reputation: 245

Set the ack header in the connect frame. What this will do is that the server will continue to send you the same message until you send back an ack frame. Which I am doing below by calling greeting.ack() as soon as receive the message. Setting it to 'client-individual' will mean that the server has to receive ack from all sessions of the particular client or it will keep re sending the same msg on every CONNECT. Hope this helps!! Use below code for reference.

   function connect() {
    var socket = new SockJS('/powerme-notification-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect(
        {client_id:'testClient'}, function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/user/topic/releaseLock', function (greeting) {
        stompClient.subscribe('/queue/releaseLock-testClient', function (greeting) {
            console.log(greeting);
            showGreeting(greeting.body);
            greeting.ack();
        },{durable:true,
            'auto-delete':false,
        ack:'client-individual',
        id:'testClient'});

    }); 
}

References: https://stomp.github.io/stomp-specification-1.2.html#ACK

Upvotes: 0

Petter Nordlander
Petter Nordlander

Reputation: 22279

You should use the semantic of topics instead of queues.

A queue allows the message to be consumed once, but a topic will allow it to be consumed once per subscriber. So something like /topic/user/guest/msg probably would do it.

Upvotes: 2

Related Questions