A
A
Alexander2016-05-22 11:07:39
Redis
Alexander, 2016-05-22 11:07:39

How to connect an ssl certificate to a server on socket.io, node.js, redis, express.js?

I made a socket.io server according to the tutorial. There was a problem connecting to the server via https request. Please explain how to connect ssl certificates specifically in this case.

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');

server.listen(8890);

io.on('connection', function (socket) {

    console.log("new client connected");

    var redisClient = redis.createClient();

    redisClient.subscribe('notification');

    redisClient.on("message", function(channel, message) {
        console.log("New message: " + message + ". In channel: " + channel);
        socket.emit(channel, message);
    });

    socket.on('disconnect', function() {
        redisClient.quit();
    });

});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Volintsev, 2016-08-19
@LordGuard

// Create context using SSL for socket.io
var fs = require('fs'),
    express = require('express'),
    socketio = require('socket.io'),
    config = require('./config');

var serverPort = config.port || 8765, // Listen port
    secure = config.secure || false; // use HTTPS/SSL

var app = express();
if (secure)
{
    var options = {
        key: fs.readFileSync(config.secure_key),
        cert: fs.readFileSync(config.secure_cert)
    };
    var server = require('https').createServer(options, app);
} else
{
    var server = require('http').createServer(app);
}

server.listen(serverPort, function() {
  var addr = server.address();
  console.log('   app listening on ' + (secure ? 'https://' : 'http://') + addr.address + ':' + addr.port);
});

var io = socketio(server);
// и так далее для io.listen и прочее

config.json file
{
    "port": "9876",
    "secure": true,
    "secure_key" : "./ssl_certificate.key",
    "secure_cert" : "./ssl_certificate.crt"
}

S
Sergey Savostin, 2016-05-22
@savostin

Why is he looking out into the world? Put nginx outside, and it already has ssl, caching, balancing, and other things.
I understand that this is not an answer, but IMHO this is more correct.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question