简体   繁体   中英

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/sockjs-client@1.1/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): rabbitMQ 的用户界面

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

Thanks!

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM