James
James

Reputation: 1780

Stomp Websocket with Fanout Exchange

I'm using RabbitMQ and Spring Websockets for messages to be displayed on a webpage via STOMP. I'd like each web page to receive all messages sent to the exchange (fanout).

Messages are currently received on the webpage however the behaviour is like a queue (rather than fanout) in that if 2 web pages are open and 10 messages are added to the exchange then each web page receives 5 messages.

Does anyone know what config needs to be changed to use the fanout exchange?

Javascript

var socket = new WebSocket("ws://localhost:8080/messaging-example/portfolio/websocket");
var stompClient = Stomp.over(socket);

var headers = {};
var connectCallback = function(frame) {
    stompClient.subscribe("/queue/testQueue", function(message) {
        document.body.innerHTML += "<p>" + message + "</p>";
    }, { });
};
var errorCallback = function(frame) {
    console.log("Connection Error"); 
};
stompClient.connect(headers, connectCallback, errorCallback);

Spring

<websocket:message-broker application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/portfolio">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:stomp-broker-relay 
        relay-host="x.x.x.x"
        relay-port="61613"
        system-login="user1" 
        system-passcode="password"
        client-login="user1" 
        client-passcode="password"
        prefix="/queue"/>
</websocket:message-broker>

RabbitMQ

"queues":[  
   {  
      "name":"testQueue",
      "vhost":"/",
      "durable":true,
      "auto_delete":false,
      "arguments":{  

      }
   }
],
"exchanges":[  
   {  
      "name":"testExchange",
      "vhost":"/",
      "type":"fanout",
      "durable":true,
      "auto_delete":false,
      "internal":false,
      "arguments":{  

      }
   }
],
"bindings":[  
   {  
      "source":"testExchange",
      "vhost":"/",
      "destination":"testQueue",
      "destination_type":"queue",
      "routing_key":"",
      "arguments":{  

      }
   }
]

Upvotes: 2

Views: 2387

Answers (1)

James
James

Reputation: 1780

I found the answer with thanks to a post on rabbitmq user group in the Destinations section of the rabbit mq stomp documentation.

In order to specify subscription to the queue via the fanout exchange named testExchange the connection string in javascript should have been /exchange/testExchange/testQueue. The following two changes resulted in successful subscription so that all pages received all messages:

Spring

<websocket:message-broker application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/portfolio">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:stomp-broker-relay 
        relay-host="x.x.x.x"
        relay-port="61613"
        system-login="user1" 
        system-passcode="password"
        client-login="user1" 
        client-passcode="password"
        prefix="/exchange"/>
</websocket:message-broker>

Javascript

var socket = new WebSocket("ws://localhost:8080/messaging-example/portfolio/websocket");
var stompClient = Stomp.over(socket);

var headers = {};
var connectCallback = function(frame) {
    stompClient.subscribe("/exchange/testExchange/testQueue", function(message) {
        document.body.innerHTML += "<p>" + message + "</p>";
    }, { });
};
var errorCallback = function(frame) {
    console.log("Connection Error"); 
};
stompClient.connect(headers, connectCallback, errorCallback);

Upvotes: 4

Related Questions