'wss://' NodeJS Connection - node.js

I am using BinaryJS to stream to a server. But at this moment I put my page in HTTPS (OPENSSL). This is why all create the connection in the client:
var client = new BinaryClient('wss://my_IP:9050');
If I use ws the browser will give error because this is not a protected connection and the page use https. But I am having an error anyway:
WebSocket connection to 'wss://my_IP:9050/' failed: Error in connection establishment: net::ERR_CONNECTION_CLOSED
And the server Code is:
var BinaryServer = require('binaryjs').BinaryServer;
binaryServer = BinaryServer({ port: 9050});
Should the binaryserver using some certificate? I cant understand the reason of the error.

The solution is here:
binaryServer = BinaryServer({
//port: 9000 //BinaryServer will share the same port of httpsServer
server: serverHTTPS, //Server previously defined
});
Based on this link

Related

why does Chrome connect to my nodejs server from 2 ports?

my nodejs script is:
const http = require("http");
const httpserver = http.createServer();
httpserver.on('connection', socket=>{
console.log(socket.remotePort, socket.address());
})
httpserver.listen(8080)
when navigating to the url: http://localhost:8080/ in Chrome I see that 2 connections are established on consecutive remote ports ex:
55413 {address: '::1', family: 'IPv6', port: 8080}
55414 {address: '::1', family: 'IPv6', port: 8080}
I'm confused as to why it is establishing 2 connections. Other browsers don't do this. Any thoughts are greatly appreciated. Thanks
I updated the node code to the following:
const http = require("http");
const httpserver = http.createServer();
httpserver.on('connection', socket=>{
console.log(socket.remotePort, socket.address());
socket.on('data',(data)=>{
console.log(socket.remotePort, socket.bytesRead);
console.log(data.toString())
})
socket.on('end',(data)=>{
console.log(socket.remotePort, 'ended');
})
})
httpserver.on('request', (req,res)=>console.log('request is made made'));
httpserver.listen(8080);
after refreshing the page a few times it seems that Chrome establishes 2 socket connections and uses the first to receive the GET request. The second receives no data. Upon refresh of the page it ends the first socket connection ( that was used for the GET request); opens a new socket connection (3rd) then uses the 2nd created for the new GET request. On each consecutive refresh it creates a new socket connection and uses the previous opened one, while ending the socket used for the previous GET request.
So it seems Chrome is creating a second socket in anticipation of a page refresh?

NodeJs - Secure Web Socket and Client Connection

I need to convert an application with websocket in a secure-websocket. (under windows)
Im using nodeJs as websocket server and a simple html page to connect to it.
Searching on google and here, I found this approach:
Create a certificate and a key for server. I've followed this tutorial:
https://www.cloudinsidr.com/content/how-to-install-the-most-recent-version-of-openssl-on-windows-10-in-64-bit/
After creating a .key and a .pem, I'have modified my nodejs websocket server to introduce the certificate:
const httpsOptions = {
key: fs.readFileSync('./api/security/cert.key'),
cert: fs.readFileSync('./api/security/cert.pem')
}
this._http = require('http');
this._server = this._http.createServer(httpsOptions , function(req, res) { this.closeCurrentConnections(req,res)}.bind(this));
var serverConfig = {
server: this._server,
autoAcceptConnections: false
}
this._wsServer = new WebSocketServer(serverConfig);
The Websocket server seems up when I start the nodejs
Now, in the client page I had this code:
var websocket_server = "ws://localhost:8128";
var echo_service = new WebSocket(websocket_server,"echo-protocol");
[...]
I changed it with the following code calling this page over HTTPS instead of simple HTTP:
var websocket_server = "wss://localhost:8128";
var echo_service = new WebSocket(websocket_server,"echo-protocol");
[...]
I got an error on client page:
testing_page.html:283 WebSocket connection to 'wss://localhost:8128/' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR
I think is due to missing certificate.
I've tried to import my previous create certificate on chrome but I cant import because chrome is especting a .crt and/or other format. I've tried to force .pem but it doens't work.
What Im missing?

Implementing wss (Secure WebSocket) in Nodejs server

I am trying to implement a secure websocket through a Nodejs server. The server is in the net of our university, and it works fine when I access it from inside the university net. However, I get a "net::ERR_CONNECTION_REFUSED" error when I try to connect from outside, and I made sure that the corresponding port is open.
Here is the code:
var ws_PORT = xxx;
var fs = require('fs');
var https = require('https');
var cert = fs.readFileSync('path/cert.pem', 'utf8');
var key = fs.readFileSync('path/privkey.pem', 'utf8');
var options = {key: key, cert: cert};
var server = https.createServer(options);
server.listen(ws_PORT);
var WebSocket = require('ws');
var wss = new WebSocket.Server({server: server});
wss.on('connection', function(ws) {
console.log('WS_SERV: Connected to port: '+ws_PORT);
// Manage ws
// ...
}
Any ideas...?
Thank you in advance,
If you are inside of a university network then the connection is going to be blocked by the university firewall. You would need to create some kind of port forwarding rule on the firewall but if you aren't an administrator than they aren't going to let you do that and if you are an administrator and already tried that and it is not working then double check the firewall rule and both the public and internal IP.
Or, if you are looking to make this publicly accessible, the easiest would just be to throw it on a VPS behind nginx on Digital Ocean, AWS, or linode

socket io connection working fine in localhost but not when deploying on heroku server

When I am running my code on localhost it is working fine but after deploying on heroku it shows error (GET Error)
Here Is My Code
Server Side Code
app = express()
app.listen(app.get('port'), function() {
console.log('Node app is running on port..', app.get('port'));
});
var server = app.listen(4200);
var io = require('socket.io')(server);
io.on('connect',(socket)=>{
console.log('connected..........');
})
Client Side Code
private socket = io('My-Heroku-server-address:4200');
Error
https:My-Heroku-server-address:4200/socket.io/?EIO=3&transport=polling&t=MDpJszb
Instead of specifying the URL to connect in the client side code like private socket = io('My-Heroku-server-address:4200'); just do
var socket = io();
This will try to connect to the host that serves the page.
Refer Socket.IO for more information.
Thanks all.
Problem has been solved.
I just removed the port no.
private socket = io('My-Heroku-server-address');
Thanks all

Socket.IO connection error

I've an application in node.js with socket.io. Everything was running completely fine, but suddenly the browser started to send this error.
failed: Error in connection
establishment:net::ERR_TUNNEL_CONNECTION_FAILED
I didn't make any code change.
The protocol used by socket is ws:// and when I try to use this url in browser
'ws://highgarden-nodejs-91180.sae1.nitrousbox.com/socket.io/?EIO=3&transport=websocket&sid=T9Unec8KbWw-GAL8AAAF'
Chrome returns this error:
Failed to load resource: net::ERR_DISALLOWED_URL_SCHEME
This is a part of the socket setup code:
server.js:
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var port = process.env.PORT || 3000
*------------------------------------------*
// routes ===
var routes = require('./config/routes.js');
var sock = {}
routes(app, passport, sock);
io.sockets.on('connection', sock.update);
// launch ===
server.listen(port);
Thanks advance.
Hi the exception ERR_TUNNEL_CONNECTION_FAILED happens when a tunnel connection through the proxy could not be established. And the ERR_DISALLOWED_URL_SCHEME happens when the scheme of the URL is disallowed.
May you need use it behind the proxy!
Chrome 45.0.2454.101 m says the page has been disabled or moved on the server.

Resources