Running Paho javascript example crashes network connection because of connect loop - javascript

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

Related

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

Ngrok doesn't change localhost inside the web client

I've node.js as a server and listen on port 3000. Node.js will render vue.js that has already built when the server start. I've declare var socket = io('http://127.0.0.1:3000'); inside the vue.js and then expose my localhost via ngrok. And it seems to work well inside the same localhost (intranet) until I try to connect from outside localhost. There's no connection between vue and the server. I've debugged and figured out that when I connect from outside the intranet it's trying to connect back to its localhost (127.0.0.1) instead of connecting back to its generated live tunnel like 6928b3e5.ngrok.io. I use a free version of ngrok and it gonna generate a new tunnel every time the server start so how do I make a connection back to its live tunnel?
I actually solve this now by using var socket = io(); inside the client

MQTT mosquitto - Implementing websocket in javascript using mqttws31.js

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

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

Dotcloud www and TCP in single app -

I'm trying to get a nodejs socket server running that will allow remote communication between two clients running a Flash game that communicates using a custom protocol. Due to Flash security restrictions, it seems that the socket server must be running on the same host as the web server that servers the Flash game. I've been continuously getting the following error:
The service crashed at startup or is listening to the wrong port. It failed to respond on port "nodejs" (8080) within 30 seconds
What I need is a way to run my nodeJS server code, while simultaneously serve the flash files.
I'm using the environment JSON variables to determine what port to listen on, and my YML is similar to the one discussed here but no luck...
Just wondering if I can get some info on how to create a working socket server/web server that will work for this (or if it is actually possible)
You can use the following dotcloud.yml file:
www:
type: nodejs
ports:
mything: tcp
Then in your Node.js app, you can bind a HTTP server to port 8080, and an arbitrary TCP server to the port contained by environment variable $PORT_MYTHING. Then run dotcloud info on your service; in the ports section, you will see something like this:
- name: mything
url: tcp://myapp-johndoe.dotcloud.com:12345
From now on, if you connect to myapp-johndoe.dotcloud.com on port 12345, you will actually connect to $PORT_MYTHING in your application.
I hope that it makes sense, and that it is what you were looking for!

Categories