2

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":{  

      }
   }
]
James
  • 1,720
  • 5
  • 29
  • 50
  • Are you listening on the same queue on both web pages? If yes, then you cannot send same messages to both the listeners. It is [load balanced](https://stackoverflow.com/a/10621516/5189158) – raiyan Jun 30 '17 at 16:04
  • It is the same queue definition @RaiyanMohammed but I want to connect via a fanout exchange so that all subscribers receive the same messages. I've now resolved the issue - see answer below – James Jun 30 '17 at 16:30

1 Answers1

4

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);
James
  • 1,720
  • 5
  • 29
  • 50