Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 630
notExactlyAHero Avatar asked Oct 11 '25 18:10

notExactlyAHero


1 Answers

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.

like image 95
Ioan Avatar answered Oct 14 '25 11:10

Ioan