Thanks for your code @Ronster ! It actually solved my problem and my socket.io server is now working over https!
I wonder if you or anyone could help me clean up my code a little though. I am not very good with node and all that.. it IS working which is good but it is now throwing the following error which it wasn't before (when I was using it over regular http):
WebSocket connection to 'wss://mydomain.com:3000/socket.io/?EIO=3&transport=websocket&sid=OnsWeQn1bjYhjVDDAAAB' failed: Connection closed before receiving a handshake response
Despite the error however everything is actually working fine. I guess that's because my client isn't trying to use a wss:// connection at all and just connecting via regular https://
Still could someone please take a look at my code below. Basically what I am doing is creating a node server that subscribes to a bunch of redis channels (it gets the channels dynamically over an api call via unirest), and whenever something is pushed to those channels it pushes it along over socket.io so that my client gets realtime notifications in its front-end. I hope that makes sense. I repeat.. this is working atm.. I just feel my code is probably weird and messy because I don't grasp node and all that. I got this setup and working by following some online examples of course.
First here is my previous implementation over http
const PORT = 3000;
const HOST = '0.0.0.0';
var express = require('express'),
http = require('http'),
server = http.createServer(app);
var app = express();
const redis = require('redis');
const unirest = require('unirest');
const io = require('socket.io');
if (!module.parent) {
server.listen(PORT, HOST);
const socket = io.listen(server);
socket.on('connection', function (client) {
const redisClient = redis.createClient()
unirest.get('http://myDomain.com/channels')
.end(function (response) {
if (response.error) {
console.log('GET error', response.error)
} else {
channels = response.body.data;
for (i = 0; i < channels.length; i++) {
redisClient.subscribe(channels[i]);
}
}
})
redisClient.on("message", function (channel, message) {
client.send([channel, message]);
});
client.on('disconnect', function () {
redisClient.quit();
});
});
}
And here it is with some modifications thanks to your code example above (and probably redundant dead bodies) which is making it work over https
var fs = require('fs');
var pkey = fs.readFileSync('/etc/nginx/ssl/mydomain.com/12345/server.key');
var pcert = fs.readFileSync('/etc/nginx/ssl/mydomain.com/12345/server.crt')
var options = {
key: pkey,
cert: pcert
};
var server = require('https').createServer( options ),
io = require('socket.io')(server),
redis = require('redis'),
unirest = require('unirest'),
PORT = 3000,
HOST = '0.0.0.0';
// Logger config
console.log('SocketIO > listening on port ' + PORT);
if (!module.parent) {
server.listen(PORT, HOST);
const socket = io.listen(server);
socket.on('connection', function (client) {
const redisClient = redis.createClient()
unirest.get('https://myDomain.com/channels')
.end(function (response) {
if (response.error) {
console.log('GET error', response.error)
} else {
channels = response.body.data;
for (i = 0; i < channels.length; i++) {
redisClient.subscribe(channels[i]);
}
}
})
redisClient.on("message", function (channel, message) {
client.send([channel, message]);
});
client.on('disconnect', function () {
redisClient.quit();
});
});
}
So you see only the top part has been changed. Unlike the http version I am no longer using express and declaring the app. Is this part completely redundant? Any idea why I'm getting the error over https even though my client works fine despite the error. Any chance you can clean up the code a little?
Many thanks!!! Hope maybe this can help someone else too.
Cheers!