MQTT JavaScript client not connecting (wrong protocol?) - javascript

I am trying really hard to make my MQTT client work inside my application. The broker is provided by CloudMQTT.
When trying to run the following code in NodeJS, the client connects properly to MQTT;
var mqtt = require('mqtt');
var client = mqtt.connect(
"mqtt://m20.cloudmqtt.com",
{
port: 11212,
username: "XXXXXXXX",
password: "XXXXXXXX"
}
);
client.on('connect', function () {
console.log('connected');
});
However, when I run the same code (without the require of course) in the frontend using the Bower package, the client does not connect. I have also tried other MQTT browserfied JS packages. I prefer MQTT.js and not Paho, because I would like to use multiple subscribes with one connected client.
If console.log(client);, NodeJS uses protocol: 'mqtt' and protocolId: 'MQTT'. The frontend uses protocol: 'ws' and protocolId: 'MQTT'. Could this be the problem? Adding these options to mqtt.connect has no effect.

From with in the browser the only option is going to be to connect via MQTT over Websockets. CloudMQTT use different port numbers for native MQTT and MQTT over websockets so you will need to also change the port number for the browser based code to the Websocket port listed in your CloudMQTT dashboard.

Related

Run Node JS websocket on Azure

The server app was running quite fine on Heroku but after migrating to Azure the server would not just start.
Here is the code..
const PORT = process.env.PORT || 2498;
const INDEX = '/index.html';
const server = express()
.use((req, res) => res.sendFile(INDEX, { root: __dirname }))
.listen(PORT, () => console.log(`We\'re live on channel : ${PORT}`));
const wss = new Server({ server });
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('close', () => console.log('Client disconnected'));
ws.on('message', (message) =>{
// this stays within the server
console.log('[SERVER]: Received a message => %s', message );
})
})```
.........
Clients connected are returning not establish an handshake ....
Azure web app only supports the exposure of port 80 and 443 for website access.
My previous requirements and test results:
I think your problem is a typical problem. I also had related requirements before, and the test failed in the webapp. All documents of Azure app service, talking about websocket, all use SignalR. I also tested the signalr service and it was successful, but this service is free and currently only supports 20 client connections.
What I thought before was to create a console program and put it in webjob to let webjob provide websocket service, but it all failed in the end.
Because azure webapp runs in a sandbox environment, and only open ports 80 and 443, we can't do more configuration. Obviously, the port that your websocket service starts is 2498, so it is not supported.
Suggestion
If you must use websocket, regardless of programming language, you must support custom port opening, such as using Virtual Machine.
You can compare the charges of azure cloud service and virtaul machine. Which one is right for you and which one to use.
Another suggestion is that you can buy a third-party intranet penetration tool and use a fixed IP or URL, such as ngrok, so that you can put the websocket service on your company's internal server. (The test is valid).
Of these three options, intranet penetration may be the most cost-effective choice. You can choose according to your needs.

Connecting SOCKET.IO-client (angular) to SOCKET.IO-server (node.js) over https net::ERR_CONNECTION_CLOSED

Our website has been running on an internal test machine where it could be accessed by all computers inside the network.
Now we want to deploy this website on a webserver (Apache2) to make it available to our clients. We want to use https and this is where we encountered a problem.
The Socket.io client canĀ“t connect to the node.js server since switching to https. We are using a signed certificate from a trusted CA. I have tried every solution I could find but none seem to work for our case.
Constructor used with ngx-socket-io in angular:
constructor() {
super({url: 'https://mywebPage.com:8080',options:{secure: true, origin: '*', transport:['websocket']}})
}
Our certificate seems to be valid since it works for our angular page. We are also able to make HTTPS GET/POST requests to our API which is located on the same server.
node.js socket.io server code:
var options = {
key: fs.readFileSync('/etc/apache2/folder/certificate.com.key'),
cert: fs.readFileSync('/etc/apache2/folder/certificate.com.public.crt'),
ca: fs.readFileSync('/etc/apache2/folder/certificate-intermediate.crt'),
requestCert: true
};
let server = require('https').createServer(options);
let io = require('socket.io')(server);
server.listen(8080);
console.log("Server started on Port 8080");
the client tries to connect to the socket-Server but fails and gets the net::ERR_CONNECTION_CLOSED the rest of the web page loads fine and has a valid certificate
We have also tested to see if the port on the web server is accesible and it seems to be open in netstat and nma.
If any more information is needed I am glad to provide.
EDIT: have tested the setup with rejectUnauthorized:false on the client side but that does not change the error.
similar stack overflow questions which i have considered in solving the problem:
socket.io net::ERR_CONNECTION_CLOSED
Setup Server-Server SSL communication using socket.io in node.js
EDIT 2: added requestCert: false, rejectUnauthorized: false into my node.js options.
Now the previous Error has been resolved now:
error during WebSocket handshake: Unexpected response code: 400

NodeJS - How to connect to remote PC that runs Node app?

I have a Nodejs client app, and a Nodejs server app. I use Apollo GraphQL for network.
These communicate fine if run on the same pc. They also work fine if the client is on another pc and server on my pc, over LAN only. They fail to communicate via internet.
My code that works right now:
server:
server.listen(PORT, '0.0.0.0',() ...
client:
const wsLink = new WebSocketLink({
uri: 'ws://192.168.10.41:8081/subscriptions',
Firs of all you must be sure that both nodes have a public IP, otherwise if you are behind a symmetric NAT you will not be able to do that.
After that you can use some of the modules of node, for example UDP(user datagram protocol) to try the connection.
Good Luck

Connect to redis server using socket.io-client without having node for react native

Main goal is to use redis pub/sub in react-native without have middle server.
I am trying to find a solution to connect to redis sever in the network, without have another server in between.
As react-native has no redis client, I am trying to find alternative.
wanted to see if we can connect to redis server using socket.io-client.
I tried using socket.io-redis
this.socket = SocketIOClient('http://192.168.3.1:6379');
this.redisAdapter = this.socket.adapter(redisAdapter({ host: '192.168.3.1', port: 6379 }));
this.socket.on('Channel1', this.onReceivedMessage);
I have build the redis client for react native .
https://www.npmjs.com/package/react-native-redispubsub
we can use this till we get it working with socket id

Standalone socket.io and Websocket

I need to build a socket.io server that will intercept incoming connections from an app which is not stored in the same directory as the server.
The client side app does not contain node.js, thus I'm trying to use a websocket :
Telnet.Socket = new WebSocket('ws://127.0.0.1:3000');
My node.js server does not need a http server but must be a standalone socket.io app. Thus, I've tried the following code :
var io = require('socket.io')();
io.on('connection', function(socket){
console.log('connexion entrante');
});
io.listen(3000);
Unfortunately, the server part does not seem to get the Websocket connection request. My firefox says :
Firefox cannot establish a connection with the server at adress ws://127.0.0.1:3000/.
What am I missing ?
Thx in advance !
socket.io need client use socket.io to connect because it use many kind of connection. For connect websocket only you can use ws node module

Categories

Resources