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
Related
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
I have two AWS EC2 instanses. A Server and a client Node JS app.
Locally my code is working fine.
But at AWS the client simply shuts down after some time - like 30 sec - without any warning/exception (For some reason it can't find and connect to the server)
Both AWS instances are running: Windows Server 2016 Base.
Both AWS instance have their own sererate "AWS security group". Just to make sure i'm not blocking any thing both security groups currently allow:
"All traffic to ANY IP" - both Ingoing and Outgoing traffic.
Both instances run in the same "Availability zone" in AWS.
The server is listening at host: '0.0.0.0', port: 4080.
And the client tries to connect to the server's IP at port 4080. I have tried to connect to all possible options - like:
Public DNS (IPv4)
IPv4 Public IP
Elastic IP
Private IPs
I can't even ping the server's IP from the client or from my own PC. I can access both AWS instances fine via RDP.
Here is a bit of my code:
SERVER.JS
var server = require('http').createServer();
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ server: server });
server.on('request', app);
server.listen(_port, '0.0.0.0', function () { console.log('SERVER STARTED! (listening on port # ' + _port + ')') });
CLIENT.JS
var WebSocket = require('ws');
var _ws = new WebSocket(THE_SERVER_IP);
_ws.on('open', function open() {
...
});
_ws.on('message', function (data, flags) {
...
});
_ws.on('close', function close() {
...
});
Solution: I added a custom TCP rule to the Windows Firewall in the server where my server.js app is running - which allow the client's IP to connect to port 4080. Done.
I'm trying to listen port 8080 for socket.io but I get error:
http://localhost:8080/socket.io/?EIO=3&transport=polling&t=Lai1FQh net::ERR_CONNECTION_REFUSED
I'm using ubuntu virtual machine from digitalocean and first when I ssh login I get message:
The "ufw" firewall is enabled. All ports except for 22, 80, and 443 are BLOCKED.
I'm beginner with this but doesn't that mean that port 8080 is blocked.
How do I fix this and what I need to do
Here is my code for server:
var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );
var app = express();
var server = http.createServer( app );
var io = socket.listen( server );
server.listen(8080, function(){
console.log("Listening");
})
In my website i just connect to that port like this:
<script src="node_modules/socket.io-client/dist/socket.io.js"></script>
<script>
var socket = io.connect( 'http://localhost:8080' );
...
<script>
The error message you get means that ufw (it's a firewall) is blocking all ports except those 3.
To enable port 8080:
sudo ufw allow 8080
Then reboot your server.
(As seen from the guide available on your hosting provider website)
You probably have another instance of your program running, or some other program that uses that port. Run:
ps aux
in your shell to see what processes are running and kill those that shouldn't be running. You can also run lsof or netstat to see what is listening on which port. See the manuals for those commands.
If you can open 8081 or something like that but not 8080 then it means that something is already listening on port 8080. You can verify it with trying to connect to that port with curl, wget, netcat or by using nmap. See their man pages for details.
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
I have a simple node application that sends data through tcp sockets. Essentially it’s two node applications a sender and a receiver, or a client and server. I can open two terminal shells on my localhost and see data transferring though tcp sockets, so I know it works. I want to send data from my localhost to my server with these applications but I can’t figure it out.. wether its the ip address routing or if I have to open tcp ports or disable firewalls, not sure. here's what the server and client apps look like when I can successfully send data on my localhost. I want to use this client app on my localhost and host the server app on my centos server and transfer data, is this possible?
/*** TCP Client ***/
/* Dependencies */
var fs = require('fs');
var hl7 = require('simple-hl7');
/* Build TCP Server */
var server = hl7.Server;
var tcpClient = server.createTcpClient();
/* Connection */
tcpClient.connect('127.0.0.1', 6969);
/* Get XML */
var msg = fs.readFileSync('./data/example.xml').toString();
/* Send Message */
setTimeout(function() {
tcpClient.send(msg, function(ack) {
console.log("ACK: ",ack.toString());
console.log("\nsuccessful transfer");
tcpClient.close();
});
}, 500);
the server can be found here. click here
but heres the config code..
/*
Config class
vars:
baseFolder: Where to save the messages
port : What port to listen on
ip : what ip to listen on
*/
var Config = new function() {
this.baseFolder = "data";
this.port = 6969;
this.ip = '127.0.0.1';
}
Ofcouce it is possible. Set server's IP and listening port in client. Then since client gonna connect to server, you will have to open this port on the server in both directions. And then just connect.
Basically here tcpClient.connect('127.0.0.1', 6969); Instead of 127... should be server's real external IP address.