Paho Client 'onMessageArrived' Not Called In Javascript [message #1872680] |
Thu, 14 November 2024 08:20  |
Eclipse User |
|
|
|
I've finally managed to connect, using Paho and browser Javascript, to a local computer that's running Mosquitto.
I can publish messages which I know the broker is receiving and distributing thanks to using the MQTT Explorer app. I can also see a Terminal window that's subscribed to the topic printing out the messages that are sent from the browser.
unfortunately, the same browser refuses to trigger 'onMessageArrived'. Here's my .js code:
let client = null;
let connected = false;
let connectionAttempt = 1;
const clientId = "vFwKg";
const REMOTE = false;
let hostname = "192.168.1.186"
const port = 8080;
let path = "";
const mainTopic = "train_control";
const user = "1731445474889";
const pass = "5180YRZIQbdSfeca<,.>";
const keepAlive = 60;
const timeout = 30;
const tls = REMOTE;
const cleanSession = true;
const lastWillTopic = `Test/LastWills/${clientId}`;
const lastWillQos = 0;
const lastWillRetain = true;
const lastWillMessageVal = `${clientId}:closed`;
let connectOptions;
var messageElement;
document.addEventListener("DOMContentLoaded", (event) => {
messageElement = document.getElementById('message');
// body...
client = new Paho.MQTT.Client(hostname, Number(port), path, clientId);
logMessage(`INFO Connecting to Server: [Host: ${hostname}, Port: ${port}, Path: ${client.path}, ID: ${clientId}]`);
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.onConnected = onConnected;
let lastWillMessage = new Paho.MQTT.Message(lastWillMessageVal);
lastWillMessage.destinationName = lastWillTopic;
lastWillMessage.qos = lastWillQos;
lastWillMessage.retained = lastWillRetain;
connectOptions = {
invocationContext: {
host: hostname,
port: port,
path: client.path,
clientId: clientId
},
timeout: timeout,
keepAliveInterval: keepAlive,
cleanSession: cleanSession,
useSSL: tls,
onSuccess: onConnect,
onFailure: onFail,
userName: user,
password: pass,
willMessage: lastWillMessage
};
// connect the client
client.connect(connectOptions);
});
No errors at all are displayed in the Console of the browser.
Is there anyone who's had luck with 'hearing' messages via a browser? Is it something I've missed? I'm using Firefox and Safari with the same results.
|
|
|
Re: Paho Client 'onMessageArrived' Not Called In Javascript [message #1872689 is a reply to message #1872680] |
Thu, 14 November 2024 13:02  |
Eclipse User |
|
|
|
The problem was actaully in my subscription line - which I hadn't posted here as I thought it was working!
Instead of:
client.subscribe('*{mainTopic}/#', { qos: Number(0) });
I just needed:
client.subscribe(mainTopic);
The QoS value seemed to block the messages incoming despite all my code contain QoS values of 0 and MQTT Explorer confirmed the messages as having that value.
|
|
|
Powered by
FUDForum. Page generated in 0.04228 seconds