MQTT mosquitto - Implementing websocket in javascript using mqttws31.js - javascript

Implementing a websocket using MQTT with mosquitto broker using javascript by including mqttws32.js file.
Mosquitto version: 1.4.9
On start I got:
rohan#rohan-PC:~$ mosquitto
1470241326: mosquitto version 1.4.9 (build date Fri, 03 Jun 2016 09:22:47 +0100) starting
1470241326: Using default config.
1470241326: Opening ipv4 listen socket on port 1883.
1470241326: Opening ipv6 listen socket on port 1883.
As latest version of mosquitto supports Web socket.
I'm using following code:
client = new Paho.MQTT.Client("localhost", 1883, "myclientid_" + parseInt(Math.random() * 100, 10));
Getting error:
WebSocket connection to 'ws://localhost:1883/mqtt' failed: Error in
connection establishment: net::ERR_CONNECTION_REFUSED
I listener i got:
1470241581: New connection from ::1 on port 1883.
1470241581: Socket error on client <unknown>, disconnecting.
After searching I found a solution to add following line to mosquitto.conf file
listener 1884
protocol websockets
I changed to port to 1884 but still getting the same error.

Mosquitto does not have a default config file location so you need to actually specify it on the command line
$ mosquitto -c /path/to/mosquitto.conf
The config file also should have an extra line in it if you still want to listen on 1883 for native MQTT connections as well as websockets on 1884
port 1883
listener 1884
protocol websockets
That should get you a functioning broker that is actually listening for MQTT over Websockets on port 1884

Related

Web client was not able to connect to the MQTT web socket

I have created a MQTT broker, and I have configured a web socket.
This is my configuration file. (I don't use SSL)
#mqtt
listener 1883
protocol mqtt
#Websockets
listener 9001
protocol websockets
I running my MQTT broker (with this command $ mosquitto -c /path/to/mosquitto.conf) and this is the log file.
1569230648: mosquitto version 1.4.15 (build date Tue, 18 Jun 2019 11:42:22 -0300) starting
1569230648: Config loaded from /etc/mosquitto/mosquitto.conf.
1569230648: Opening ipv4 listen socket on port 1883.
1569230648: Opening ipv6 listen socket on port 1883.
1569230648: Opening websockets listen socket on port 9001.
In my web site I have this code.
<!-- Script to import MQTT -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript">
</script>
<!-- Script to connect to MQTT broker and retrieve current values -->
<script>
var websocket = "195.**.***.**";
var port = 9001;
client = new Paho.MQTT.Client(websocket, port, "web_connection" + parseInt(Math.random() * 100, 10));
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
var options = {
onSuccess: onConnect,
onFailure: doFail
}
// connect the client
client.connect(options);
.....
But when I try to connect with my web site, Chrome get this error:
mqttws31.js:977 WebSocket connection to 'ws://195.**.***.**:9001/mqtt' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
I open in my router 9001 port. Why can't the client web connect?
Thanks

Paho Client Web Socket Can't Find mqtt

I've been given a project that is using a Paho/MQTT client through Javascript to update a web page. The project starts an http server through python on localhost:8080 and then, when the webpage is loaded, a main.js script starts the client and runs the connect as shown below:
var client = new Paho.MQTT.Client("localhost",Number(8080),'0');
client.onConnectionLost = onConLost;
client.onMessageArrived = onMesArvd;
console.log("start connection...");
client.connect({onSuccess: onConnect});
The problem is that upon trying to connect the below error appears and the onConnect method does not appear to connect as a console.log does not appear:
WebSocket connection to 'ws://localhost:8080/mqtt' failed: Error during WebSocket handshake: Unexpected response code: 404
The code highlighted in red in the paho-mqtt.js is below:
new WebSocket(a, ["mqtt"])
I tried adding a mqtt file to the folder localhost is being run from but it only changes the response code (301 if a mqtt folder is present, 200 if a file).
Adding the mosquitto.conf that came with the project:
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
listener 1883
listener 8080
protocol websockets
I noticed it's not in the same folder as the paho/mqtt client but in ../dir2/setup/.
I've looked around but I haven't seen anyone with this issue so any help/guidance would be greatly appreciated. Thanks!
Thanks to Santosh Balaji for pointing me in the right direction on this one.
I believe I needed to install mosquitto on the pi and then change the mosquitto.conf file in the mosquitto install dir to the one provided by the project. After installing and confirming my conf was being used the js and Python connected without issue.
1) Is port 8080 occupied by other process before starting mqtt. Try to start your mosquitto with conf file. It will show up error if there is anything wrong with the start.
mosquitto -c mosquitto.conf
2) Try changing the port to 9001 as it is default port for using websockets in mqtt

Running Paho javascript example crashes network connection because of connect loop

I'm trying to connect to a local mosquitto mqqt broker via websockets and the paho javascript client. However, using the example shown at https://www.eclipse.org/paho/clients/js/ crashes my network connection. In the chrome or ie console I found the problem: The connection is made over and over again (function onConnect is called multiple times per second).
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("Connection was successful");
client.subscribe("World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "World";
client.send(message);
}
On the broker I can see the client connecting:
New client connected from 192.168.1.3 as web_78 (c1, k60, u'user123').
But the message does not get broadcasted by the client. When I terminate the browser, I see on the client:
Socket error on client web_78, disconnecting.
The same thing happens, when I try different Codeexample like this one: https://jpmens.net/2014/07/03/the-mosquitto-mqtt-broker-gets-websockets-support/
I'm using mosquitto version 1.5 on my Raspberry Pi and have really no Idea, how I can get this running. Maybe the problem is the mosquitto server and not the client?
This is the part of my mosquitto.conf where I define the ports
listener 9001
protocol websockets
Other clients can connect (e.g via paho python) and mqtt (without websockets).
pi#raspberrypi ~ $ sudo mosquitto -c /etc/mosquitto/mosquitto.conf
1530009485: mosquitto version 1.5 starting
1530009485: Config loaded from /etc/mosquitto/mosquitto.conf.
1530009485: Opening websockets listen socket on port 9001.
1530009485: Opening ipv4 listen socket on port 1883.
1530009485: Opening ipv6 listen socket on port 1883.
1530009485: New connection from 192.168.1.51 on port 1883.
1530009485: New client connected from 192.168.1.51 as DVES_9CE05F (c1, k15, u'johann').
Any help would be highly appreaciated!
EDIT Update:
The problem must be within my server, as I can access public broker like HiveMQ.
I had the same problem. There is a problem related to libwebsockets in Mosquitto 1.5.x, at least in the version that are installed on Raspberry Pi with Raspbian jessie. When I downgraded Mosquitto to 1.4.15 this problem was solved for me.
Read about it in, https://github.com/eclipse/mosquitto/issues/1050

Using Paho to connect to Mosquito MQTT server

I am trying to use Paho's JavaScript library to connect to a Mosquito MQTT server. I have just used the example on the Paho website. Which is:
function onConnect() {
console.log("onConnect");
client.subscribe("harleyRowland/myTopic");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "harleyRowland/myTopic";
client.send(message);
}
onConnect();
On Safari, I get the following error:
Error: AMQJS0011E Invalid state not connected.
and on Chrome, I get the following error as well as the one above:
WebSocket connection to 'ws://mosquitoServer.co.uk:1883/mqtt' failed:
Error during WebSocket handshake: net::ERR_CONNECTION_RESET
I need to use the Mosquito server for this work. Is it possible using JavaScript? If it isn't possible, is there another web technology I could use?
You need to add a websockets listener to your mosquitto setup. you do this by adding something like the following to your mosquitto.conf
listener 1884
protocol websockets
This will listen for MQTT over websockets on port 1884 (not 1883 as you can not run both on the same socket with mosquitto).
You will also need to be running mosquitto newer than 1.4 and not on Windows unless you compile it yourself

MQTT over WebSockets "AMQJS0008I Socket closed" connection error on PhantomJS

The JBoss AeroGear JavaScript notifier is a collection of adapters which provide a unified/similar API for interacting with different messaging services and protocols.
Issue
I'm writing some QUnit tests for the JBoss AeroGear JS MQTT over WS adapter which depends on the Eclipse Paho MQTT JavaScript client. The tests work on Firefox/Chrome but fail on PhantomJS 1.9.7 with AMQJS0008I Socket closed connection error.
Initially, my tests were running against an Apache Active MQ broker version 5.9.0 which supports MQTT over WebSockets out of the box. I've also modified them to use the Eclipse Paho MQTT over WebSockets sandbox environment ws://iot.eclipse.org/ws but the problem still exists.
How to reproduce?
Clone my GitHub repository https://github.com/tolis-e/aerogear-js-integration. Checkout the stackoverflow branch, execute npm install and then grunt integration-iot --debug. In order to verify that the tests are working on Firefox/Chrome, open the tests/notifier/mqttws.html in a browser. The QUnit tests are located inside the mqttws.js file. As you can see at the logs below, I'm getting a AMQJS0008I Socket closed error.
Any idea about the error cause? My sense is that the issue is related to the fact that PhantomJS 1.9.7 does not support Web Sockets RFC 6455. It looks like RFC 6455 will be supported by PhantomJS 2.x
Logs
[D] ["phantomjs","qunit.begin"]
[D] ["phantomjs","onLoadFinished","success"]
[D] ["phantomjs","qunit.moduleStart","Connect / Disconnect"]
[D] ["phantomjs","qunit.testStart","Connect to and Disconnect from MQTT Server"]
[D] ["phantomjs","console","Unexpected response code: 404"]
Unexpected response code: 404
[D] ["phantomjs","console","Connection error: AMQJS0008I Socket closed."]
Connection error: AMQJS0008I Socket closed.
[D] ["phantomjs","fail.timeout"]

Categories