dro2
March 3, 2024, 3:16pm
1
Hi,
sorry to come up with this question, as it should have been asked already, but after hours of searching, I have not found the solution.
I would like to add a mqtt web broker on a Internet webpage, using paho javascript module. I have downloaded this version:
/*******************************************************************************
* Copyright (c) 2013 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Andrew Banks - initial API and implementation and initial documentation
*******************************************************************************/
// Only expose a single object name in the global namespace.
// Everything must go through this module. Global Paho module
// only has a single public function, client, which returns
This file has been truncated. show original
I managed to connect to the public server using:
var client = new Paho.Client('broker.hivemq.com', 8000, 'mqtt' );
client.connect({
onSuccess:function(){
console.log('Connected');
client.subscribe('test_web');
}
});
but I don’t manage to connect through my free hivemq account, with:
var client = new Paho.Client('[the correct ID].s2.eu.hivemq.cloud', Number(8884), '/mqtt', 'text');
client.connect({
onSuccess:function(){
console.log('Connected');
client.subscribe('test_web');
}
});
Firefox dev page indicates:
Firefox can’t connect to ws://[the correct ID].s2.eu.hivemq.cloud:8884/mqtt.
could you please provide a working examples in this case?
the HiveMQ article on this topic is from 2015 and probably too old.
I thank you very much !
Hi @dro2
I hope you’re doing well? I noticed you’re working on connecting your Paho JS client to the HiveMQ Cloud Cluster. It seems like you’ve got the right hostname and port, but there might be a small oversight regarding SSL usage.
In your first example with “broker.hivemq.com ” and port 8000, it’s an unsecured (not encrypted) WebSocket, hence it doesn’t require SSL, which is why your first example is working.
However, in your second example with the HiveMQ Cloud Broker and port 8884, it’s a secure (encrypted) WebSocket, and it requires SSL. This might be the reason your second example isn’t functioning as expected.
After checking the source code of your paho-mqtt.js client at paho.mqtt.javascript/src/paho-mqtt.js at master · eclipse/paho.mqtt.javascript · GitHub , I recommend trying to enable SSL by setting “clientOptions.useSSL = true” in your client.
I hope this information proves helpful. If you have any questions or need further clarification, feel free to ask. Good luck with your project!
Dasha from HiveMQ Team
dro2
March 4, 2024, 8:00pm
3
Hi,
thank you for your quick reply. I couldn’t manage to use your suggestion, but after some additional search on Internet I found the following article:
The Paho documentation is unfortunately quite fragmented at the moment, and some links on Eclipse’s website do not work. Here are some working links (as of 05/2019): https://www.eclipse.org/paho/clients/js/...
Est. reading time: 3 minutes
it works now in the following version:
<script type='text/javascript' src='paho-mqtt.js'></script>
<script>
var client = new Paho.Client("wss://[alphanumeric code].s2.eu.hivemq.cloud:8884/mqtt", "i");
client.connect({
userName : "userid",
password : "password",
onSuccess:function(){
console.log('Connected');
client.subscribe('/test_web');
message = new Paho.Message("Hello");
message.destinationName = "/test_web";
client.send(message);
}
});
After loading the page, I receive the message on the HiveMQ webclient and I manage to receive on the html page the message after having added the right client.onMessageArrived function.
Thank you again for your answer.
have a nice day!
1 Like