Socket.IO can't connect through https

25,784

You cannot initalize socket.io server like https server. You have to start a separate https server and then attach socket.io server to it.

var https = require('https'),     
    fs =    require('fs');        

var options = {
    key:    fs.readFileSync('ssl/server.key'),
    cert:   fs.readFileSync('ssl/server.crt'),
    ca:     fs.readFileSync('ssl/ca.crt')
};
var app = https.createServer(options);
io = require('socket.io').listen(app);     //socket.io server listens to https connections
app.listen(8895, "0.0.0.0");
Share:
25,784
undefined
Author by

undefined

Updated on July 05, 2022

Comments

  • undefined
    undefined almost 2 years

    I have a node.js app, that uses socket.IO. It works fine on http, but when trying to connect to the socket through https - nothing happens.
    Here's some part of the code:

    var fs = require('fs');  
    
    var ioHttp = require('socket.io').listen(8899, {  
        'flash policy port': -1  
    });  
    
    initSocket(ioHttp);  
    
    var ioHttps = require('socket.io').listen(8895, {  
        key: fs.readFileSync('/path/to/file/file.key'),  
        cert: fs.readFileSync('/path/to/file/file.crt'),  
        ca: [  
            fs.readFileSync('/path/to/file/sub.class1.server.ca.pem'),  
            fs.readFileSync('/path/to/file/ca.pem')  
        ],  
        'flash policy port': -1   
    });  
    
    initSocket(ioHttps);  
    

    and the initSocket function:

    function initSocket(io) {  
        io.enable('browser client minification');  
        io.enable('browser client etag');  
        io.enable('browser client gzip');  
    
        io.set('transports', [  
            'websocket',  
            'htmlfile',
            'flashsocket',
            'jsonp-polling'  
        ]);  
    
        io.sockets.on('connection', function (client) {
            //the connnection is handled here
        });
    }
    

    The client connect like this:

    var secureConnection = false;  
    var port = 8899;
    if (window.location.protocol === 'https:') {  
        port = 8895;
        secureConnection = true;
    }
    
    var socket = io.connect('domain.org', {port: port, secure: secureConnection});
    

    As I said everything works fine on http, but connecting on https gives me "The connection was interrupted". What am I doing wrong?

  • Titan
    Titan over 9 years
    Can you self sign or does socket.io behave like normal HTTP in browsers where it checks the certificates and warns you?
  • Adrian Salazar
    Adrian Salazar over 9 years
    Hey, where do you get the server.key, server.crt and ca.crt? is it self-made? will it work on production if self made?
  • user568109
    user568109 over 9 years
    @GreenGiant Look for the requestCert and rejectUnauthorized inside options in the documentation
  • user568109
    user568109 over 9 years
    @AdrianSalazar Yes, I used self-made files, with openssl commandline tool. For production it will not work as certificates needs to be verified by the cert. authority -web of trust. Luckily for you, now there is a way to do this. The EFF recently announced Let's Encrypt which helps devs in acquiring and maintaining certificates easily and free of cost. Use their library node-acme. I cant give detailed instructions as this is the limit of my knowledge, maybe others can.
  • undefined
    undefined over 7 years
    @DarkNeuron Yes, here's a link to the Let’s Encrypt website