Search code examples
rabbitmqstomppikarabbitmq-exchangepython-pika

stomp.js cannot receive message sent from pika (RabbitMQ version: 3.11.7)


I have a web page that should receive messages from RabbitMQ using STOMP:

<body>
    <script src="stomp.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sockjs.min.js"></script>

    <script>
        var client = Stomp.client('ws://localhost:15674/ws');

        client.debug = null;
        var sub = function(d) {
            // print_first(d.body);
            console.log("got the message! ", d.body)
        }
        var on_connect = function(x) {
            id = client.subscribe("/topic/test", sub);
            console.log("connected")
        };
        var on_error =  function() {
          console.log('error');
        };
        
        client.connect('guest', 'guest', on_connect, on_error, '/');
    </script>
</body>

when I run this code, it shows connected in the console (so far so good)

I also have a python backend, which should send messages to the queue (send.py):

import pika

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare("/topic/test")

properties = pika.BasicProperties(
        content_type='application/json',
        content_encoding='utf-8',
        delivery_mode=2,
    )

channel.basic_publish(exchange='',
                      routing_key='/topic/test',
                      body='Hello World!',
                      properties=properties)

The messages are sent (I ran the script with py -m send; the messages appear in the RabbitMQ Management) : the UI for rabbitMQ

However the console.log in sub isn't running. Any idea how I can fix this?

Thanks!


Solution

  • Ended up using stomp.py instead of pika:

    import stomp
    PORT = 61613
    LOCALHOST = '0.0.0.0'
    conn = stomp.Connection11([(LOCALHOST, PORT)])  
    conn.connect('guest','guest')
    conn.send(body="start",destination='/queue/test')
    conn.send(body="end",destination='/queue/test')
    conn.disconnect()