Socket.IO can't connect through https

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('').listen(8899, {  
    'flash policy port': -1  


var ioHttps = require('').listen(8895, {  
    key: fs.readFileSync('/path/to/file/file.key'),  
    cert: fs.readFileSync('/path/to/file/file.crt'),  
    ca: [  
    'flash policy port': -1   


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', [  

    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('', {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?



You cannot initalize server like https server. You have to start a separate https server and then attach 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('').listen(app);     // server listens to https connections
app.listen(8895, "");


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.