0
votes

I am new in MQTT so can someone help me for connecting MQTT with Mosquitto using javascript i am using this code but it give error...

Connection failed: AMQJS0007E Socket error:undefined.

My Code is :

<script type='text/javascript' src='jquery-1.10.1.js'></script>

<script type='text/javascript' src="mqttws31.js"></script>

    var client = new Messaging.Client("ns.testingindia.tld", 1883, "myclientid_" + parseInt(Math.random() * 100, 10));

     //Gets  called if the websocket/mqtt connection gets disconnected for any reason
     client.onConnectionLost = function (responseObject) {
         //Depending on your scenario you could implement a reconnect logic here
         alert("connection lost: " + responseObject.errorMessage);
     };

     //Gets called whenever you receive a message for your subscriptions
     client.onMessageArrived = function (message) {
         //Do something with the push message you received
         $('#messages').append('Topic: ' + message.destinationName + '  | ' + message.payloadString + '
'); }; //Connect Options var options = { timeout: 3, //Gets Called if the connection has sucessfully been established onSuccess: function () { alert("Connected"); }, //Gets Called if the connection could not be established onFailure: function (message) { document.write("Connection failed: " + message.errorMessage); alert("Connection failed: " + message.errorMessage); } }; //Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker var publish = function (payload, topic, qos) { //Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations) var message = new Messaging.Message(payload); message.destinationName = topic; message.qos = qos; client.send(message); } //]]>
3

3 Answers

2
votes

You are connecting to port 1883 which is the default MQTT port. I assume you mean to use Websockets, and that would typically be configured on a different port number. If the broker you're using has Websocket support, ensure you connect to the correct port with Messaging.Client().

If you're using the Mosquitto broker, you'll need version 1.4 from its bitbucket repository for Websocket support, but note that Mosquitto 1.4 hasn't yet been released.

2
votes

A quick way to test that your broker isn't causing the problem is to connect to broker.mqttdashboard.com port:8000 if that doesn't work my next guess is that you have just mosquitto installed and no websockets server, which you need if you want to use JS to connect directly to the broker over the web.

Another, but quicker way to get up and running now is downloading hivemq (trial version supports 25 connections) it has a mqtt broker with websockets built in and will run on windows and will be up and running in 5 mins.

1
votes

Which version of Mosquitto are you using?

The current release version (1.3.4) does not natively support Websockets (next version will)

You can use something like lighttpd with mod_websockets to supply websocket support (instructions for linux are linked to from here: http://test.mosquitto.org/ws.html) or you can build a new version of Mosquitto from the head of the source tree