Search code examples
javascriptmqttmosquittopaho

Paho MQTT JS client loses connection to Mosquitto broker when I publish or receive message (error AMQJS0005E)


Bottom line up front: The Paho MQTT client sucessfully connects to my Mosquitto broker, but immediately disconnects when I try to publish a message or when it receives a message from a topic it's subscribed to. I've tried changing Mosquitto's listening port and authentication settings, and using two different versions of Paho MQTT, and I still have the same problem.

Now let's get into detail.

Intro: I'm making a dashboard for some facial recognition devices that communicate through MQTT. I set up a Mosquitto broker and I've had no problems connecting to it and communicating with the devices using the Paho MQTT client for Python (I made a kind of server to sync the device's info to a database). Now I'm making the web interface, so I added a WebSockets listener to my mosquitto.conf and wrote a script using the Paho MQTT library for Javascript to connect to it, subscribe to topic sgdrf/out, send a simple JSON message to topic sgdrf/in to get the list of online devices, and process the response of the Python server once it arrives.

Problem and attempted solutions: I ran the Django server, loaded the web page and opened the JS console to find that the MQTT client successfully connected to the broker but immediately disconnected when it tried to publish the message to topic sgdrf/in. Here's each line of console output with their explanations:

  1. The message produced by the onSuccess function, which indicates that the client successfully connected to the Mosquitto broker:

    Conexión exitosa al broker MQTT.

  2. In the onConnected function, I added console.log(uri) to see the URI used by the client to connect to the broker. I got:

    ws://localhost:61613/

  3. After printing uri to console, I made the client subscribe to sgdrf/out and then print 'subscribed' to console:

    subscribed

  4. Then I call get_online_devices(mqtt_client), a function which creates a simple JSON string and publishes it to the topic sgdrf/in. But first, it prints the strign to the console so that I can check it (just in case):

    {"operator":"GetOnlineDevices","messageId":96792535859850080000,"info":{}}

  5. Then, when the publish method is actually executed, is when I get this error (captured by the onConnectionLost function):

    Pérdida de conexión con el broker MQTT: AMQJS0005E Internal error. Error Message: message is not defined, Stack trace: No Error Stack Available (código: 5)

I checked the Mosquitto log file and it only says when a new client was connected and then when it was disconnected because of a socket error (each time for every page reload). Tail of /var/log/mosquitto/mosquitto.log:

1614796149: New connection from 127.0.0.1 on port 61612.
1614796149: New client connected from 127.0.0.1 as mqttx_53195902 (p2, c1, k60, u'admin').
1614796182: Socket error on client sgdrf_dashboard_8499, disconnecting.
1614796325: New client connected from ::ffff:127.0.0.1 as sgdrf_dashboard_1597 (p2, c1, k60, u'admin').
1614796325: Socket error on client sgdrf_dashboard_1597, disconnecting.
1614796336: New client connected from ::ffff:127.0.0.1 as sgdrf_dashboard_6565 (p2, c1, k60, u'admin').
1614796336: Socket error on client sgdrf_dashboard_6565, disconnecting.
1614796931: New client connected from ::ffff:127.0.0.1 as sgdrf_dashboard_9773 (p2, c1, k60, u'admin').
1614796931: Socket error on client sgdrf_dashboard_9773, disconnecting.
1614797168: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.

I tried changing the listening port in mosquitto.conf, and enabling and disabling authentication, but it changes nothing. And obviously I've had to restart Mosquito every time I changed the config file. I don't think the problem is Mosquitto.

I have the same problem whether I use Paho MQTT version 1.1.0 or 1.0.3.

As an experiment, I commented out the call to get_online_devices in my Javascript so that it doesn't try to publish anything, reloaded the page and there was no error, as expected. Then, I used MQTTX to send a JSON message to the sgdrf/out topic to which the MQTT JS client is subscribed to, and it immediately disconnected with the same error message.

Code: At the bottom of the page (index.html) I have the following code (the original code has Django template tags to fill in some values, so this is the actual code received by the browser):

<!-- Paho MQTT -->
<script src="/static/js/paho-mqtt-min.js"></script>

<!-- Scripts -->
<script src="/static/js/dashboard.js"></script>
    
<script>
function get_online_devices(mqtt_client) {
    cmd = {
        operator: "GetOnlineDevices",
        messageId: generate_random_number_n_exp(20),
        info: {}
    };

    payload_string = JSON.stringify(cmd);
    console.log(payload_string)

    mqtt_client.publish("sgdrf/in", payload_string);
}

function add_device_to_list(device) {
    // Omitted for brevity. It's not being used yet.
}

let mqtt_client = make_mqtt_client("localhost", 61613);
let connection_options = make_connection_options(
    "admin",
    "CENSORED_PASSWORD"
);

mqtt_client.onConnected = function(reconnect, uri) {
    console.log(uri)
    mqtt_client.subscribe("sgdrf/out");
    console.log('subscribed');
    get_online_devices(mqtt_client);
};
mqtt_client.onConnectionLost = mqtt_client_on_connection_lost;
mqtt_client.onMessageDelivered = mqtt_client_on_message_delivered;

mqtt_client.onMessageArrived = function (msg) {
    // Omitted for brevity. Checks if the payload is a
    // JSON object with the right data and calls
    // add_device_to_list for each item of a list in it.
};

$(document).ready(function() {
    mqtt_client.connect(connection_options);

    $("#reload-device-list-btn").click(function() {
        get_online_devices(mqtt_client);
    });
});
</script>

The dashboard.js files mentioned above just has some functions that I think will be useful for other pages, so I separated them to a file:

// dashboard.js
function generate_random_number_n_exp(n) {
    return parseInt(Math.random() * Math.pow(10, n), 10)
}

function make_mqtt_client(host, port) {
    let client_id = "sgdrf_dashboard_" + generate_random_number_n_exp(4);
    return new Paho.Client(host, port, '/', client_id);
}

function make_connection_options(user, password) {
    let connection_options = {
        userName: user,
        password: password,
        onSuccess: mqtt_client_on_success,
        onFailure: mqtt_client_on_failure,
    };
    
    return connection_options;
}

function mqtt_client_on_success() {
    console.log('Conexión exitosa al broker MQTT.');
}

function mqtt_client_on_failure(error) {
    console.log(
        'Fallo de conexión con el broker MQTT: ' + error.errorMessage
        + ' (código: ' + error.errorCode + ')'
    );
}

function mqtt_client_on_connection_lost (error) {
    console.log('Pérdida de conexión con el broker MQTT: ' + error.errorMessage
        + ' (código: ' + error.errorCode + ')'
    );
}

function mqtt_client_on_message_delivered(msg) {
    let topic = message.destinationName;
    let payload = message.payloadString;
    console.log("Mensaje enviado a " + topic + ": " + payload);
}

function mqtt_client_on_message_arrived(msg) {
    let topic = message.destinationName;
    let payload = message.payloadString;
    console.log("Mensaje recibido de " + topic + ": " + payload);
}

Here are the contents of my mosquitto.conf file:

per_listener_settings true
listener 61612
allow_anonymous false
password_file /home/s8a/Projects/sgdrf/config/pwdfile.txt

listener 61613
protocol websockets
allow_anonymous false
password_file /home/s8a/Projects/sgdrf/config/pwdfile.txt

It just sets up a TCP listener and a WebSockets listener, both disallow anonymous connections, and authenticate using a pwdfile. As I said before, I have enabled and disabled anonymous connections, and changed the port number to 9001 and back to 61613, and I still have the same error.

Conclusion: I don't know what to do and this project's deadline is next week.


Solution

  • I feel kinda stupid, because it was really a trivial typing mistake. The problem is that the onMessageDelivered and onMessageArrived functions have msg as argument, but I wrote messagein the function body for some reason. That's what the "message is not defined" error meant, message is literally not defined. Anyway I fixed that and now it sends and receives messages without problems.

    ...

    More detailed story: What was not trivial is how I figured it out.

    I decided to get my hands dirty and opened the non-minified version of paho-mqtt.js. I looked for "Invalid error" and found where the error constant is defined, and two places where it's used in a catch block. In both catch blocks I noticed that there was a ternary operator checking if (error.hasOwnProperty("stack") == "undefined") but the true and false clauses were inverted, which is why I was getting "No Error Stack Available".

    So I inverted the clauses, and indeed I got a stack trace in the console (maybe I should file a bug report to the Paho dev team when I can). The stack trace had my mqtt_client_on_message_delivered function right at the top, so I read it again and suddenly everything made sense. Then I felt stupid for wasting an afternoon on this.