0

Here is my paho client code

// Create a client instance
client = new Paho.MQTT.Client('127.0.0.1', 1883, "clientId");

// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({onSuccess:onConnect});


// called when the client connects
function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe("/World");
  message = new Paho.MQTT.Message("Hello");
  message.destinationName = "/World";
  client.send(message); 
}

// called when the client loses its connection
function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:"+responseObject.errorMessage);
  }
}

// called when a message arrives
function onMessageArrived(message) {
  console.log("onMessageArrived:"+message.payloadString);
}  

On Rabbitmq server everything is default seetings. When i run this code i get WebSocket connection to 'ws://127.0.0.1:1883/mqtt' failed: Connection closed before receiving a handshake response

What i am missing ?

Manish Kumar
  • 10,214
  • 25
  • 77
  • 147

2 Answers2

2

From my personal experience with Paho MQTT JavaScript library and RabbitMQ broker on windows, here is a list of things that you need to do to be able to use MQTT from JS from within a browser:

  1. Install rabbitmq_web_mqtt plugin (you may find latest binary here, copy it to "c:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.2\plugins\", and enable from command line using "rabbitmq-plugins enable rabbitmq_web_mqtt".
  2. Of course, MQTT plugin also needs to be enabled on broker
  3. For me, client was not working with version 3.6.1 of RabbitMQ, while it works fine with version 3.6.2 (Windows)
  4. Port to be used for connections is 15675, NOT 1883!
  5. Make sure to specify all 4 parameters when making instance of Paho.MQTT.Client. In case when you omit one, you get websocket connection error which may be quite misleading. Finally, here is a code snippet which I tested and works perfectly (just makes connection):

 client = new Paho.MQTT.Client("localhost", 15675, "/ws", "client-1");

 //set callback handlers
 client.onConnectionLost = onConnectionLost;
 client.onMessageArrived = onMessageArrived;

 //connect the client
 client.connect({
  onSuccess : onConnect
 });

 //called when the client connects
 function onConnect() {
  console.log("Connected");
 }

 //called when the client loses its connection
 function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
   console.log("onConnectionLost:" + responseObject.errorMessage);
  }
 }

 //called when a message arrives
 function onMessageArrived(message) {
  console.log("onMessageArrived:" + message.payloadString);
 }
dstefanox
  • 2,222
  • 3
  • 19
  • 21
1

It's not clear in the question but I assume you are running the code above in a web browser.

This will be making a MQTT connection over Websockets (as shown in the error). This is different from a native MQTT over TCP connection.

The default pure MQTT port if 1883, Websocket support is likely to be on a different port.

You will need to configure RabbitMQ to accept MQTT over Websockets as well as pure MQTT, this pull request for RabbitMQ seams to talk about adding this capability. It mentions that this capability was only added in version 3.6.x and that the documentaion is still outstanding (as of 9th Feb 2016)

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • you mean it will not b supported in current version ? Can you tell me how czn i configure MQTT in rabitmq – Manish Kumar Feb 09 '16 at 23:37
  • The pull request does imply this feature is not in the current release. Do you have to use RabbitMQ? Could you use a different broker that already has Websockets support such as mosquitto – hardillb Feb 09 '16 at 23:42
  • no i am not bound to any broker yet. Just searhing for a nice broker for mobile chat app? so what will be the other nice option ? – Manish Kumar Feb 10 '16 at 05:12