0

Following the RabbitMQ documentation I'm trying to subscribe and receive messages in the browser from the /topic/test topic. Meanwhile to send them to RabbitMQ I'm using a program called mqtt-explorer.
In the console of the browser I'm able to see that the messages I'm sending actually arrive even though when I try to open RabbitMQ's managment portal I only see some queues automatically generated which always say that I have 0 messages on the queue.

This is what I'm sending with mqtt-explorer: mqtt-explorer screenshot This is the browser when I'm connected as the same time as mqtt-explorer is sending: browser screenshot

If the browser is not connected in that moment the messages don't appear in the queue on RabbitMQ.
This is what I'm able to see when only mqtt-explorer is connected and is sending messages. RqbbitMQ Queues screenshot

Below the code of the browser:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello MQTT</title>
</head>
<body>
    <h1>Hello MQTT</h1>
    <p>This is a basic HTML page with the title</p>

    <!-- Source library -->
    <script src="mqttws31.js" type="text/javascript"></script>

    <script>

        var wsbroker = "localhost"
        var wsport = 15675; // port for above
        var client = new Paho.MQTT.Client(wsbroker, wsport, "/ws",
            "myclientid_" + parseInt(Math.random() * 100, 10));

        client.onConnectionLost = function (responseObject) {
            console.log("CONNECTION LOST - " + responseObject.errorMessage);
        };

        client.onMessageArrived = function (message) {
            console.log("RECEIVE ON " + message.destinationName + " PAYLOAD " + message.payloadString);
        };

        var options = {
            timeout: 3,
            keepAliveInterval: 30,

            onSuccess: function () {
                console.log("CONNECTION SUCCESS");
                client.subscribe('/topic/test', {qos: 1});
            },

            onFailure: function (message) {
                console.log("CONNECTION FAILURE - " + message.errorMessage);
            }
        };
        
        console.log("CONNECT TO " + wsbroker + ":" + wsport);
        client.connect(options);

    </script>
</body>
</html>
Brits
  • 14,829
  • 2
  • 18
  • 31
Eridano
  • 1
  • 1
  • If I understand you correctly your issue is that when the javascript connects it is not receiving messages sent whilst it was disconnected? If that is the case then the absence of `cleanSession: false` in `options` is probably the issue (MQTT subscriptions are cleared on disconnect with the default setting). See [this answer](https://stackoverflow.com/a/65640804/11810946) for details and [this in the docs](https://www.rabbitmq.com/mqtt.html#durability). – Brits Jul 20 '23 at 21:19
  • 1
    @Brits thanks a lot, this has greatly helped me. I wasn't able to see the messages in the queue because the js was consuming them too fast for me too see. With this I'm able to stop the js and see the messages accumulate in the queue as I wanted. – Eridano Jul 21 '23 at 08:11

0 Answers0