CS_noob
CS_noob

Reputation: 557

How to send and receive data from webstomp websocket provided by rabbitmq?

I am writing a chat application without explicit server side web socket. I am using RabbitMQ webstomp as the web socket container and plain Javascript as the cleint to both send and receive data.

Below is the flow : Browser -> native websocket/sockjs -> rabbitmq /sockjs websocket (ws://127.0.0.1:15674/ws or http://localhost:15674/stomp) -> put messages to queue.

However while testing the application, I am not able to send the data directly to ws://127.0.0.1:15674/ws. I am just able to connect to it.

I use the below template to send and receive data on client Javascript.

  1. ws = new WebSocket('ws://127.0.0.1:15674/ws');
  2. client = Stomp.over(ws);
  3. client.connect('guest','guest',on_connection,on_connect_error,'/');
  4. client.send(queue, {'reply-to':'/temp-queue/logs',priority: 9}, "msg" );
  5. client.onreceive = func()

Upvotes: 0

Views: 3678

Answers (1)

Gabriele Santomaggio
Gabriele Santomaggio

Reputation: 22682

The problem,most likely,in your code is:

client.send(queue, {'reply-to':'/temp-queue/logs',priority: 9}, "msg" );

you have to send the message to a topic

I suggest to see the example here: https://github.com/rabbitmq/rabbitmq-web-stomp-examples

Here is a rapid example i made starting for the original example:

var client = Stomp.client('ws://localhost:15674/ws');
      client.debug = pipe('#second');

      var print_first = pipe('#first', function(data) {
          client.send('/topic/test', {"content-type":"text/plain"}, data);
      });
      var on_connect = function(x) {
          id = client.subscribe("/topic/test", function(d) {
               print_first(d.body);
          });
      };
      var on_error =  function() {
        console.log('error');
      };
      client.connect('guest', 'guest', on_connect, on_error, '/');

enter image description here

Upvotes: 0

Related Questions