I try to get a MQTT JavaScript Client running. It's based on the Eclipse Paho Client Library (org.eclipse.paho.mqtt.javascript.git).
Before running the JavaScript Client I was performing some tests with
- mosquitto_pub -h test.mosquitto.org -t "/topic1" -m "test"
and
- mosquitto_sub -h test.mosquitto.org -t "/topic1" -v
which are working fine.
Then I called my own mqttTest.html which contains:
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/JavaScript" src="mqttws31.js"></script>
<script type="text/JavaScript">
var client;
function doConnect() {
client = new Messaging.Client("test.mosquitto.org", 1883, "mosqOtti");
console.log("Client instantiated.");
client.startTrace();
console.log("Now trying to connect...");
client.connect({onSuccess:onConnect});
}
function onConnect() {
console.log("connection established");
doSubscribe();
}
function doSubscribe() {
client.subscribe("/topic1");
}
window.onload = function() {
this.doConnect();
}
</script>
</head>
.
.
.
</body>
</html>
I tried to lauch this in Firefox. The debug console output tells me that
[09:58:27.825] Firefox can't establish a connection to the server at ws://test.mosquitto.org:1883/mqtt. @ file:///mqttws31.js:914
I know that moquitto does not support websockets natively. But I red that the lighttp running on test.mosquitto.org has mod_websockets installed.
Line 914 of mqttws31.js is trying to do this.socket = new WebSocket(wsurl, 'mqttv3.1');
So it seems that
- either websockets doesn't really work for test.mosquitto.org
- or my example is buggy!
I stuggled around for a long time now and need to get a JavaScript MQTT Client running.
Does anyone have an idea? Or another approach? Socket.IO seems not to be the right solution too.
Thanks very much in advance!