Paho Client Web Socket Can't Find mqtt - javascript

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

Related

How to do a reverse proxy with node.js for a shiny app

EDIT
I think the problem might be that the WebSocket connection does not go through the proxy node.js. How to authorize the connection of the WebSocket?
I have a web app hosted on a nodeJS server. On the same vm I also have a shiny serveur hosting an app. I use node to redirect traffic to port 3838 (shiny) when a somes URL are requested.
I use this code on node :
app.use('/the-shiny-app', proxy({target: 'http://localhost:3838', changeOrigin: true}));
With this setting everything works fine on the shiny app when I go on mydomain/the-shiny-app/* except when I try to run code in a code box.
When I try to run code I get this error on the chrome console :
Connection closed. Info: {"type":"close","code":4503,"reason":"The application unexpectedly exited","wasClean":true}
An example of what I mean by code box :
if I do not use node.js and I redirect the traffic (on OS level) from port 80 directly to 3838 everything works fine.
sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-ports 3838
I do not know exactly what kind of exchange is made between the browser and the shiny server when we run code, but maybe the protocol used is blocked by node.js.
I found the problem. As Antony Gibbs said you need to setup a WebSocket upgrade. I'm using http-proxy-middleware you cans find the doc here : https://github.com/chimurai/http-proxy-middleware

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

Socket IO not getting connected to server using swift

I've downloaded a sample socket project from github https://github.com/appcoda/SocketIOChat
I'm using EICapitan and I've installed node js and I've also setup the IP in the sample var socket: SocketIOClient = SocketIOClient(socketURL: NSURL(string: "http://192.168.1.2:3000")!)
When I run node index.js command on terminal it shows Listening on *:3000 But when I run the sample app enter a name to connect user I dont receive any message like user connected. Is there something that I'm missing, I'm a beginner to node.js and socket.io. Any help is appreciated.
Seems like you are trying connect to incorrect ip address.
Get your inet ip from ifconfig and place it to SocketIOClient. I would recommend you to use localhost or 127.0.0.1 for development instead of inet address

socket.io/socket.io.js stuck on (pending) - Node.js

So whenever I check if the /socket.io/socket.io.js file is loaded onto my webpage is says (pending) and after a while (failed) net::ERR_CONNECTION_REFUSED
I've been looking through some other posts and trying some stuff.
I've tried using <script src="/socket.io/socket.io.js"></script>
and <script src="http://127.0.0.1:8080/socket.io/socket.io.js"></script>
My original code includes <script src="http://10.0.0.199:8080/socket.io/socket.io.js"></script> where 10.0.0.199 is my server which is running node.js and mongoDB.
My full code of my server.js file is as follows: (this file starts the socket.io)
var mongo = require('mongodb').MongoClient,
client = require('socket.io').listen(8080).sockets;
console.log('info - socket.io started');
Do not try to import Node JS script on web page
You can not play directly with socket in JS under Web Browser. Currently web browsers support only websockets.
Apparently my firewall somehow managed to block the 8080 port again.
I used iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT -m comment --comment "Socket.io port" to open the port on my firewall but this did not help. I managed to open the port using the GUI on my server.
Thanks everyone, and I hope that this might help someone in the future as this was very confusing.

OpenShift NodeJS deployment : socket.io index.html port assignment, etc

I locally wrote a nodeJS app using socket.io and express modules.
I wanted to use openshift for hosting.
So I changed the main .js to server.js which seems to be the index equivalent of the openshift file and changed the server port setting to:
var server = require('http').createServer(app).listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);
as indicated in some posts.
However after git commit, I am still getting:
remote: info: socket.io started
remote: warn: error raised: Error: listen EACCES
remote: DEBUG: Program node server.js exited with code 0
remote:
remote: DEBUG: Starting child process with 'node server.js'
and the website doesn't work.
As the app is serving a html file, there are two more places, where the port is mentioned, which sit in the index.html that is served:
header:
<script src='//localhost:3000/socket.io/socket.io.js'></script>
and within javascript for the html file:
var socket = io.connect('//localhost:'+process.env.OPENSHIFT_NODEJS_PORT || 3000);
// intial vars and multi list from server
socket.on('clientConfig', onClientConfig);
All files and modules are seemingly uploaded, but the EACCES error still prevails.
I get the feeling that maybe the header link to localhost:3000 might be the skipping point, but I am not sure. Anyone have any idea, what the problem is?
Also, there is no : socket.io/socket.io.js file in the socket.io modules folder, which I find confusing.
I had recently developed a chat client application using socket.io and also had webrtc in it. I was able to deploy the app on openshift by making the following changes into code.
Client Side
Keep the include script tag in a relative manner like so
<script src="/socket.io/socket.io.js"></script>
While declaring io.connection, change the ip part to point the application to server in this format.
var socket = io.connect('http://yourapp-domain.rhcloud.com:8000/', {'forceNew':true });
8000 is for http and 8443 is for https
Server Side
The io and the server should both be listening on the same port and the order in which the statements are run should also be given attention.
Step 1: Declare the http server using app.
( app is obtained from express)
var express = require('express');var app = express();)
var server = require('http').Server(app);
Step 2:
Declare io from socket.io and combine it with the server object.
var io = require('socket.io').listen(server);
Step 3:
Now, allow the server to listen to openshift port and ip.
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
Please pay special attention to the order of the statements you write, it is the order which causes issues.
The server side of your websocket needs to listen on port 8080 on your openshift ip address, the CLIENT side needs to connect to your ws://app-domain.rhcloud.com:8000
I have a few notes on how to use WebSockets here: https://www.openshift.com/blogs/10-reasons-openshift-is-the-best-place-to-host-your-nodejs-app#websockets
You don't need any additional server-side changes after adapting your code to take advantage of environment variables (when available)
OpenShift's routing layer exposes your application on several externally-accessible ports: 80, 443, 8000, 8443.
Ports 8000 and 8443 are both capable of handling websocket connection upgrades. We're hoping to add support for WebSocket connections over ports 80 and 443 soon.

Categories