MQTT 1884 connection failed - javascript

I am using mqtt for a messaging system on my site. Well on my local machine everything works fine but on my site (production) I am getting an error:
vendor.bbaf8c4….bundle.js:1 WebSocket connection to
'ws://localhost:1884/mqtt' failed: Error in connection establishment:
net::ERR_CONNECTION_REFUSED
I am using Ubuntu within my Webserver and I gave the port 1884 free.
What this error meaning?
I am using Angular4 and I add the connection within AfterViewInit
ngAfterViewInit() {
setTimeout(()=>{
this.messageservice.createMqttConnection(this.userId);
}, 200)
}
The connection with Paho on my client site looks like:
this._client = new Paho.MQTT.Client("localhost", 1884, id.toString());
Let assume my domains is mydomain.com should I replace it with localhost?

As thrashed out in the comments:
The hostname in your app needs to point to your domain e.g. mydomain.com not localhost
localhost will always point to the machine the code is running on so is very unlikely to be what you want for a production deployment of an application.

Related

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

How to configure websocket?

On the local server, these settings work
CHAT_WS_SERVER_HOST = '0.0.0.0'
CHAT_WS_SERVER_PORT = 3000
CHAT_WS_SERVER_PROTOCOL = 'ws'
And the chat is working fine, but on Heroku there is an error(Chrome):
WebSocket connection to 'ws://0.0.0.0:41540/jgvnf53ky1m1lxt1vrka9lcnfilwhkrv/Pechkin' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
On Heroku I use such settings:
CHAT_WS_SERVER_HOST = '0.0.0.0'
CHAT_WS_SERVER_PORT = os.environ['PORT'];
CHAT_WS_SERVER_PROTOCOL = 'ws'
Python code
websocket = new WebSocket(base_ws_server_path + '{{ request.session.session_key }}/' + opponent_username);
JS code on client
websocket = new WebSocket(base_ws_server_path + 'jgvnf53ky1m1lxt1vrka9lcnfilwhkrv/' + opponent_username);
Fihish
ws://0.0.0.0:41540/jgvnf53ky1m1lxt1vrka9lcnfilwhkrv/Pechkin
I read that this is a problem because of the certificate (wss \ https), but I don't use him now... or with the wrong IP
Upd_1
The IP address 0.0.0.0 refers to the local server. When your browser connects to that address, it will reach the computer where the browser is running (e.g. your laptop). Since you want it to connect to your Heroku app, you need to replace the value 0.0.0.0 with the hostname for your Heroku app, e.g. your-app-name.herokuapp.com. This way, the browser will connect over the internet to your Heroku app instead.
The URL in the browser should look like ws://your-app-name.herokuapp.com:...

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

Websocket connection refused. WebSocket connection to 'ws://127.0.0.1:2000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I have used websocket: https://github.com/kishor10d/CodeIgniter-Ratchet-Websocket
I have implemented websocket on a site. It is working great on localhost but when I uploaded files, it doesn't work. First there was confusion regarding the port I am using which is 2000. Now after contacting Inmotion's support they told me that 2000 is live and there are no firewall issues.
But I get this error when I reload the page:
WebSocket connection to 'ws://127.0.0.1:2000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
According to their support there is no service listing to this port.
I have used CodeIgniter, Javascript
If you're calling the localhost (127.0.0.1) from the client app, you're calling your own computer, once you have the backend running on a remote server, you have to use the server's domain name or IP address - I'd suppose your web/server hosting service (Inmotion) assigned you the latter.

WebSocket connection failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I am new to WebRTC and WebSockets and was following this tutorial to create a WebRTC demo project, but I am unable to create a WebSocket connection. I have followed the same steps as mentioned in the project.
His project is running on port 8080 and he mentioned ws://localhost:9090. My project is running on port 8081, but I copied his URL ws://localhost:9090 because I didn't know the significance of 9090 and I received this error and my server is node.js. i changed local host to 8081 as well but then i am getting hand shake error.
WebSocket connection to 'ws://localhost:9090/' failed: Error in
connection establishment: net::ERR_CONNECTION_REFUSED.
Chrome doesn't allow unsecure websocket (ws) connections to localhost (only wss, so you should setup a TLS certificate for your local web/websocket server).
However the same should work fine with Firefox.
You need to use ws://yourIp:9090/, where yourIP is like 192.168.?.?.
Usually WebRTC requires a secure connection (that is https).
The error you have got is due to TLS/SSL certificates occupied, may be they are not properly configured in your project.
Provide a valid TLS/SSL certificate and also configure it correctly in project, then it will work without the above error.
try to change the port to 8080
const ws = new WebSocket('ws://localhost:8080/chat')
I guess this is a generic websocket issue.
Change the url to a dynamic name using the built-in location.host variable and change the protocol to secure websocket wss if you have set-up the TLS:
const ws = new WebSocket("wss://" + location.host + "/")
Port 9090 is used by reactotron. Probably you are using it in your project and your app cannot connect with reactotron because it is closed. Just open reactotron and the error will disappear.
also you could easily change the mappings of IP addresses to host names,
on windows go to C:\Windows\System32\drivers\etc\hosts and uncomment this line
127.0.0.1 localhost
save and restart.
WebSocket connection to 'ws://localhost:8080/' failed
Must ensure server file is running
I git this above problem
maybe you forgot to start websocket server, check it again, with configuration in my project, run:
php artisan websocket:init

Categories

Resources