I am using JavaScript / mqtt.js to connect to a Mosquitto broker.
I had everything working, and then I upgraded to the Mosquitto version 2.0.15, and now I cannot connect to the broker.
Could you read the following, and see if you have any suggestions, plase.
Thanks
Garrett
OLD SET UP
I had v1.6.9 Mosquitto installed and running on Ubuntu (in Google Cloud).
I was able to communicate with the broker using:
desktop web client
MQTT Explorer
JavaScript using mqtt.js
NEW SETUP
I upgraded to latest Mosquitto version 2.0.15.
I had to create a username and password, so I used the instructions here:
https://mosquitto.org/documentation/authentication-methods/
I edited the mosquito.conf file, so it knows about the password file.
I restarted the VM and then started Mosquitto.
WHAT STILL WORKS
Now, I am able to communicate with the broker (with username/password) using:
desktop web client
MQTT Explorer
But not
JavaScript using mqtt.js
mqtt.js connect options
In JavaScript, my connect options used to be of the form (for v1.6.9):
const connection_options = {
port: 1883,
host: host,
clientId: client_id,
clean: true,
keepalive: false,
reconnectPeriod: 5000
};
where host is the IP address of the machine
And are now of the form (for v2.0.15):
const connection_options = {
port: 1883,
host: host,
username: ‘user’,
password: ‘password’,
clientId: client_id,
clean: true,
keepalive: false,
reconnectPeriod: 5000
};
I have double checked the username / password values etc.
UPDATE
Here are the contents of my mosquitto.conf file:
listener 1883
password_file /etc/mosquitto/mypasswordfile
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
Note: I also tried adding the following line to the .conf file:
allow_anonymous true
But that didn't solve anything.
Here’s another thing – possibly a distraction, or maybe there’s a clue in here:
In the past, if I stated Mosquitto with the following command
mosquitto -v
the communications would be outputted to the console (I could see all the PUBLISH and PUBACKS etc.). But if I started the broker with this command
sudo systemctl start mosquitto
I wouldn’t get any output to the console. Which makes sense. But now that I have upgraded to the latest Mosquitto, when I run the command
mosquitto -v
it says it is only starting in local only mode.
Even though I have a username/password file defined.
And when I start the broker this way I CANNOT connect to it with the web client or MQTT explorer. But when I start it with this command
sudo systemctl start mosquitto
the web client and mqtt explorer CAN connect to the broker, it's just the mqtt.js code that can't. So I don't really understand the difference. However, the fact remains that when starting with
sudo systemctl start mosquitto
my web client and mqtt explorer have no problem on that port etc., but something is still missing from the mqtt.js code.
So, in summary:
After updating to the latest Mosquitto version (and restarting VM and Mosquitto), my mqtt.js code does NOT connect with the broker anymore, but the web client and MQTT Explorer do.
All 3 of these (mqtt.js, web client, mqtt explorer) use the SAME port, the SAME ip address, the SAME user name and password.
The only change I made to the Javascript was to add the username and password.
So obviously this is not enough - although I cannot find any documentation about any other changes I need to make.
Any help / suggestions greatly appreciated
I solved the issue.
Once I turned logging on, I got some very helpful information
Note 1: I hadn't turned logging on because I am new to mosquitto, linux, mqtt - and I hadn't worked out how to do it!
Note 2: Thank you hardlib for prodding me a few times to get the logs.
What I saw, when I tried to connect with mqtt.js was:
Bad socket read/write on client my_publisher: Invalid arguments provided.
I found this on the github forum:
https://github.com/eclipse/mosquitto/issues/2462
I have only skimmed through it – I will re-read the full thing later.
The issue is the keep alive (seconds) value.
If it is set 0 or false, I couldn’t connect.
Once I set it to a non-zero value, I can connect.
I think there is a different way to get around this, a setting to allow keep alive to be zero, but I will look into this later.
I think it only affects certain versions of Mosquitto (and obviously the latest version).
Related
While using ReactJS, when we use the command npm start, It starts the development server on localhost:3000 and also on the network, 192.168.1.2:3000
This was super easy, I could test my app on all the devices by going into that address.
Recently I started learning NextJS and when I run the command npm run dev, I only see that it starts the development server on the local machine, not on the network. Is there a way to make it work just like it did with pure reactjs?
It also listens on your local IP, just find out your local ip and do "192.168.1.x:3000" -- this would be your IP , you will see the server running on that port.
To find local IP:
Open your terminal and run the command ipconfig for windows or ifconfig for MacBook. You'll see the network you're currently connected to, the IPv4 address being the current local IP address.
As #Deepinder Singh mentioned, Next.js dev server already listens on your local network.
To get your local network IP address run the following command. (tested only on mac)
ipconfig getifaddr en0
Mac/Linux
To find your local IP easily, you can use:
ifconfig | grep 'inet '
Then if you're on the same network, just navigate to http://YOUR_LOCAL_IP:3000
I can see you may be confused by the addres "0.0.0.0" you're seeing. But kindly go to your terminal and check your local ip address using the following command
ipconfig
then check the IPV4 address you see and use it instead of "0.0.0.0" and you're good to go.. for instance "192.168.43.131:3000"
I have two applications - browser based client and NodeJS based server that are both communicating using WebSockets (I'm using ColyseusJS library). Problem is, that everything works fine while I'm testing them on localhost but when I deploy the application to my Ubuntu VPS server it stops working.
The message I receive in the browser while trying to connect is:
WebSocket connection to 'ws://X.X.X.X:8001/?colyseusid=' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
So it reaches the server (because when server is disabled the message is "Error in connection establishment: net::ERR_CONNECTION_REFUSED") but it looks like it fails on Upgrade connection operation.
What is strange is that I managed to make it work yesterday (don't know how exactly), but had so much mess on my VPS that I decided to revert machine to it's starting state. After that it stopped working (code is unchanged). Maybe there are some additional dependencies that I need to install in order to make it work on my Ubuntu Server?
I would really appreciate your help.
Have you tried to port forward the port to the ip address of the said VPS in your router config? And also check out the firewalls.
Running the server and client on mac with localhost everything works fine.
Running the python program (server) on the raspberry pi and try to access it using its url doesn't work.
Python Server:
class Strompreisgenerator:
def __init__(self):
self.ws = websockets.serve(self.echo, 'localhost', 5001)
asyncio.get_event_loop().run_until_complete(self.ws)
asyncio.get_event_loop().run_forever()
async def echo(self, websocket, path):
async for message in websocket:
print(message)
Javascript Client:
var ws = new WebSocket("ws://www.tobiasschmocker.ch:5001");
While trying to instantiate the WebSocket the error "WebSocket network error: The operation couldn’t be completed. Connection refused" occurs in safari.
The Port 5001 is open on RPi. I also tried local IP. I forwarded the Port on my router but still nothing. If i trie other urls i get another error, so i suppose the url is correct but i have no rights somehow.
On my RPi i have ssh enabled, also php, apache, mysql and all the pip packages for my python server.
If you know, where the problem lies, i'd be happy to know. Thank you very much!
Now the websocket is running and available via url from external!
Following is the solution for completeness. All I had to do is:
Update to Python Version 3.6. The websockets package seems to work only from Python Version 3.6. Here's the tutorial I used to install Python 3.6 on my RPi: https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f
use 0.0.0.0 instead of localhost as host within python. On sudo netstat -lptu in the RPi terminal, I could see that all the other ports (mqtt, ssh, etc) where set to 0.0.0.0 instead of localhost so I just tried and it works.
Having set all that, the websocket is now running. Thanks to #YonatanKiron & #Reto!
I am trying to connect to a Raspberry Pi that has the Mosquitto broker installed. The client on the RPi is connected using:
client.connect("127.0.0.1", 1883, 60)
I tried to connect to it on my MQTT JavaScript client using the following specifications but I failed:
client = new Paho.MQTT.Client("10.101.125.190", 1883,"myclientid_" + parseInt(Math.random() * 100, 10));
I also tried changing the port to 8080 from the JavaScript side but I still failed. If I change the port to 8080 on the RPi then it won't even connect.
This is the error that I am getting at the moment:
WebSocket connection to 'ws://10.101.125.190:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
So, what do I need to change to fix this error? The RPi and my JS client are both in the same local network.
Edit:
I forgot to mention that I have already tried this with test.mosquitto.org - 8080 and it worked, but as soon as I change the address then I start getting the error.
MQTT over websockets does not share the same port as native MQTT.
You will need to add a new listener to your mosquitto config.
You will need to add something like the following to the end of your /etc/mosquitto/mosquitto.conf (or in a seperate file in /etc/mosquitto/mosquitto.d)
listener 1884
protocol websockets
Then need to update your JavaScript to connect to port 1884 not 1883
You will also need to be using a version of mosquitto newer than than 1.4.x iirc the default version that is packaged for raspbian is too old. Follow the instructions here to get a newer version.
I have this web app written with express and socket.io using node.js, the app works brillantly on localhost, but when i push to my ec2 server, it connects for like 20 seconds then disconnects, and then connects again etc...
giving me the error on the node console as
warn - websocket connection invalid
info - transport end
SERVER
app = express()
server = http.createServer(app)
io = require('socket.io').listen(server)
CLIENT
socket = io.connect()
I know the problem is not with my code, because I fully tested the web app on localhost, so the only problem is where this code is running, which is my ec2 instance?
There could be many possible reasons you can get this error:
You are using browser that partially or does not support websockets. You can check if your browser supports websockets here.
Using proxy that does not support websocket. If there is some server(load balancer) between your client and your node server that does not support websocket.
You are using socket.io version 0.9.1/0.9.1-1. This behaviour is a reported bug for this version. So upgrade to latest socket.io version which is 0.9.14.
Browser connectivity is firewalled/blocked.
Code related problem.
Make sure you're using latest versions of node, express and socket.io on your ec2. Also, provide some data about currently used versions both on your local machine and on ec2 instance.
Running on your local machine you don't have to deal with network latency, NAT issues, or firewalls. Running on EC2 you have all of those.
Web Sockets are relatively new and unstable. So to begin with be sure you're running the latest versions (and let us know what they are). Perhaps the version of socket.io installed on your local machine is different than the version installed in your EC2 server.
If there is no activity during those 20 seconds before losing the connection, one possibility is that keep-alive is set too low.
See https://groups.google.com/forum/?fromgroups=#!topic/socket_io/RUv70BguZ-U for a similar problem. The solution there was to use heartbeat to keep the connection open.
A bit more on socket.io heartbeats if you're not already using them:
Advantage/disadvantage of using socketio heartbeats