Node.js, socket.io https connection - node.js

Server side code:
var io = require('socket.io').listen(8150);
io.sockets.on('connection', function (socket){
});
Client side code:
var socketIO = io('*.*.*.*:8150');
socketIO.once('connect', function(){
});
On http it's worked on https in same page it not connected.
Searched many examples, but all example for express. I dont create any http server in node.js need only to socket.io work.

When running the client over HTTPS, socket.io is attempting to connect to your server over HTTPS as well. Currently your server is only accepting HTTP connections, the listen(port) function does not support HTTPS.
You'll need to create an HTTPS server and then attach socket.io to it, something like this.
var fs = require('fs');
var options = {
key: fs.readFileSync('certs/privkey.pem'),
cert: fs.readFileSync('certs/fullchain.pem')
};
var app = require('https').createServer(options);
var io = require('socket.io').listen(app);
app.listen(8150);
io.sockets.on('connection', function (socket) {
});
And if you need both HTTP and HTTPS, you can start two servers and attach socket.io to both.
var fs = require('fs');
var options = {
key: fs.readFileSync('certs/privkey.pem'),
cert: fs.readFileSync('certs/fullchain.pem')
};
var httpServer = require('http').createServer();
var httpsServer = require('https').createServer(options);
var ioServer = require('socket.io');
var io = new ioServer();
io.attach(httpServer);
io.attach(httpsServer);
httpServer.listen(8150);
httpsServer.listen(8151);
io.sockets.on('connection', function (socket) {
});
Then on the client side you can determine which port to connect to based on whether the page was accessed over HTTP or HTTPS.
var port = location.protocol === 'https:' ? 8151 : 8150;
var socketIO = io('*.*.*.*:' + port);
socketIO.once('connect', function() {
});

Use letsencrypt with Plesk for a valid SSL certificat.
options = {
key: fs.readFileSync('/usr/local/psa/var/modules/letsencrypt/etc/live/mydomain.com/privkey.pem'),
cert: fs.readFileSync('/usr/local/psa/var/modules/letsencrypt/etc/live/mydomain.com/cert.pem'),
ca: fs.readFileSync('/usr/local/psa/var/modules/letsencrypt/etc/live/mydomain.com/chain.pem'),
rejectUnauthorized: false,
requestCert: true,
agent: false
}

Related

Socket.io not working with https (Let's Encrypt)

I am using ASP.NET CORE 2.0 to build an e-commerce. The e-commerce has a chat built using nodejs and the package socket.io. The socket.io server is remote on the server. When I use the Socket.io client locally, running Visual Studio Debugger, to access the remote socket.io, all works fine.
The code is like this, note that I am not using https
var app2 = require('express')();
var http = require('http').Server(app);
var http2 = require('http').Server(app2);
var io = require('socket.io')(http);
http.listen(3009, function () {
console.log('listening on port 3009');
});
http2.listen(3011, function () {
console.log('listening on port 3011');
});
But when I publish my web site and get the html page along with the socket.io client served by Nginx/kestrel I got an error message saying that I was mixing something, I didn't pay attention to the error message because I remembered that I was using http on my server socket.io and clients. So I changed the socket.io server and clients but now I cannot connect.
My changes are like this:
var app2 = require('express')();
var http = require('https').Server(app);
var http2 = require('https').Server(app2);
var io = require('socket.io')(http);
http.listen(3009, function () {
console.log('listening on port 3009');
});
http2.listen(3011, function () {
console.log('listening on port 3011');
});
clients
myIo = io('https://www.example.com.br:3009', { secure: true, reconnect: true, rejectUnauthorized: false });
I used Let's encrypt to enable https connections, I am using Nginx as proxy for Kestrel, I am using ufw on Ubuntu 17.
I got this error yesterday. I couln't even sleep at night. But I got it working. I sent the certificates like this.
var app = require('express')();
var app2 = require('express')();
var fs = require('fs');
var options = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/cert.pem')
};
var http = require('https').Server(options, app);
var io = require('socket.io')(http);
app.get('/', function (req, res) {
res.send('server is running');
});
app2.get('/', function (req, res) {
res.send('admin area');
});
I don't want anyone passing the frustration felt. Hope I can help somebody.
You need to add the transports type for your client and server
server
var io = require('socket.io')(http);
io.set('transports', ['websocket']);
client
myIo = io('https://www.example.com.br:3009', { transports:
['websocket'], upgrade: false }, { 'force new connection': true });

Node.js and Socket.io: on ssl protocol switching is pending forever

When I want to connect to a node server through client, protocol switching is pending forever. Can someone tell me why that's happening?
Here is the server-side code:
var https = require('https');
var fs = require('fs');
var options = {
key: fs.readFileSync('*******'),
cert: fs.readFileSync('*******'),
passphrase: '*******',
rejectUnauthorized: false
};
var app = https.createServer(options);
var io = require('socket.io').listen(app);
app.listen(3700);
io.sockets.on('connection', function (client) {
client.on('join', function() {
//
});
});
And here's the client-side one:
var baseURL = getBaseURL();
var socketIOPort = 3700;
var socketIOLocation = baseURL + socketIOPort;
var socket = io.connect(socketIOLocation, {secure: true, rejectUnauthorized: false});
function getBaseURL()
{
return (location.protocol + "//" + location.hostname + ":" + location.port);
}
I'm using self-signed certificates on localhost.
Mozilla says the connection is aborted and tries to reconnect, Chrome is saying the status is '101 Switching protocols' and time(latency) is 'Pending'.
getBaseURL() already includes a port (location.port) but then you append to that 3700. Remove the location.port from getBaseURL() and that should fix that particular problem.

HTTP and HTTPS same time (Express.Io)

I'm successfully listenin port 443 and can access server over https, but I can't access it with http.
var fs = require('fs')
options = {
ca : fs.readFileSync('./ssl/site.com.pem'),
key: fs.readFileSync('./ssl/site.com.key'),
cert: fs.readFileSync('./ssl/site_com.crt')
}
var app = require('express.io')
app.https(options).io()
....
app.listen(443);
I've tried using http and https modules:
app.http().io();
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
But this time socket.io is giving 404 in browser. How can I solve this? I need to use Express.Io's socket connection because application is based on it.
You should redirect http to https
var express = require('express'),
app = express(),
httpapp = express();
//........................
var credentials = {key: privateKey, cert: certificate, ca: ca};
var httpsServer = https.createServer(credentials, app);
var httpServer = http.createServer(httpapp);
httpsServer.listen(443);
httpServer.listen(80);
httpapp.route('*').get(function(req,res){
res.redirect('https://yourdomain.com'+req.url)
});
Had the same problem a few days ago, and this GitHub issue helped:
https://github.com/techpines/express.io/issues/17#issuecomment-26191447
Your code is on the right way, it just need some changes. The code below is a slightly modified version of the snippet you provided.
var fs = require('fs'),
express = require('express.io');
options = {
ca : fs.readFileSync('./ssl/site.com.pem'),
key: fs.readFileSync('./ssl/site.com.key'),
cert: fs.readFileSync('./ssl/site_com.crt')
};
var app = express();
app.https(options).io();
var httpServer = require('http').createServer(app);
// ...
app.listen(443);
express.io.listen(httpServer);
httpServer.listen(80, function() { }, function() { });

Secure inter-server communication with node.js

is there any example about secure inter-server communication in node.js?
Is socket.io necessary to realize this or I may use express too?
I would like to realize mutual authentication between two servers.
Thanks to all!!!
This is an example of what i've done; unfortunately it doesn't work...
Server 1
...
var ioc = require('socket.io-client');
var socketc = ioc.connect("https://127.0.0.1:5000", {'force new connection':true, 'secure':true});
...
Server 2
var fs = require('fs');
var https = require('https');
var io = require('socket.io');
var crypto = require('crypto');
// path to private key and certificate
var pathSSH = 'C:\\cert\\';
//array containing private key and certificate
var options = {
key: fs.readFileSync(pathSSH + 'keySatellite.pem'),
cert: fs.readFileSync(pathSSH + 'keySatellite-cert.pem')
};
var port = 5000;
var server = https.createServer(options, function(req, res) {
console.log('Connection with satellite node');
});
var sio = io.listen(server);
// ...listening at port XXXXX
server.listen(port, function(){
console.log("HTTPS server listening on port " + port);
});
sio.sockets.on('connection', function(socket) {
console.log('connected');
socket.disconnect();
});
UPDATE 1
I've noticed that server to server communication works fine if I set a http connection instead of https.

Can't connect to Socket IO with SSL

Server:
// Load libraries
var https = require('https');
var fs = require('fs');
var socketio = require('socket.io');
// The server options
var srvAddress = '123.123.123.123';
var srvPort = 8888;
var srvOptions = {
key: fs.readFileSync('ssl/cert.key'),
cert: fs.readFileSync('ssl/cert.crt'),
ca: fs.readFileSync('ssl/cert-ca.crt')
};
// Create a Basic server and response
var app = https.createServer(srvOptions, function(req, res) {
res.writeHead(200);
res.end('Online...');
});
// Create the Socket.io Server over the HTTPS Server
var io = socketio.listen(app, srvAddress);
// Now listen in the specified Port
app.listen(srvPort, srvAddress);
Client:
var socket = require("socket.io-client").connect('https://123.123.123.123:8888', {secure: true});
But client don't connect to server. I tried http instead of https, but the same result.
Server works fine. Without ssl certificate connection works. When try to open via web, then all works too.

Resources