Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

SSL (HTTPS Socket.io) #62

Closed
Epexa opened this Issue · 7 comments

3 participants

@Epexa

How do I convert socket.io to make it work on HTTPS?

RTCMultiConnection.js:

var SIGNALING_SERVER = 'https:/domain.com:8888/';
var socket = io.connect(SIGNALING_SERVER + channel, {secure:true});

signaler.js

HTTP version:

var app = require('express')(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);
server.listen(8888);

HTTPS version:

var app = require('express')(),
    fs = require('fs'),
    options = {
        pfx: fs.readFileSync('secure.pfx'),
        requestCert: true
    }
    server = require('https').createServer(options, app).listen(8888),
    io = require('socket.io').listen(server);

Does not work!

Help please!

@muaz-khan
Owner

This doc may help you: http://nodejs.org/api/https.html

You may need to "manually" set port like 8542.

Also, you may need to edit node.js file to set HTTPs port to something like 8543.

Then you should add both ports to server's firewall.

I'll add a working demo soon.

@Epexa

https://domain.com:8543/socket.io/1/?t=1373541799009

The browser works fine.
Received from the server replies with the contents wss, but the flow of data between the users do not.

I'll wait for your example.

Thank you!

@muaz-khan
Owner

What I did is asked system admin to enable port 8542 in the firewall.Then I used following code; and it worked.

var fs = require('fs');

var options = {
    key: fs.readFileSync('ssl/private/domain.com.key'),
    cert: fs.readFileSync('ssl/certs/domain.com.crt'),
    ca: fs.readFileSync('ssl/certs/domain.com.cabundle')
};

var app = require('https').createServer(options, handler),
    io = require('socket.io').listen(app);

function handler(req, res) {
    res.writeHead(200);
    res.end("welcome sir!");
}

io.sockets.on('connection', function (socket) {
    socket.on('message', function (data) {
        socket.broadcast.emit('message', data);
    });
});

app.listen(8543);

Issue #1011 may help you.

Note

Use PuTTY (or any node.js command prompt) to stop all existing processes to make sure new updates works.

// list all active processes
ps aux | grep node

// kill all processes
killall node

Read more here.

@Epexa

But what about the module "express", it is required for the job.


I follow your example.

HTTP version.

RTCMultiConnection.js:

var SIGNALING_SERVER = 'http:/domain.com:8543/';
var socket = io.connect(SIGNALING_SERVER + channel);

signaler.js:

var port = 8543;
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);

server.listen(port);

/* -------------- for presence detection! -------------- */

var channels = {};

/* -------------- <socket.io> -------------- */

io.sockets.on('connection', function (socket) {
    var initiatorChannel = '';
    if (!io.connected)
        io.connected = true;

...

Works fine.


HTTPS version.

RTCMultiConnection.js:

var SIGNALING_SERVER = 'https:/domain.com:8543/';
var socket = io.connect(SIGNALING_SERVER + channel);

signaler.js:

var port = 8543;
var fs = require('fs');
var options = {
    pfx: fs.readFileSync('/home/mydir/secure.pfx')
};
var app = require('express')(),
server = require('https').createServer(options, app),
io = require('socket.io').listen(server);

server.listen(port);

/* -------------- for presence detection! -------------- */

var channels = {};

/* -------------- <socket.io> -------------- */

io.sockets.on('connection', function (socket) {
    var initiatorChannel = '';
    if (!io.connected)
        io.connected = true;

...

Does not work!


And you can see your version of the file for this version signaler.js.

@muaz-khan
Owner

Used following code:

var fs = require('fs');
var express = require('express');
var expressCallback = express();

var options = {
    key: fs.readFileSync('ssl/private/domain.com.key'),
    cert: fs.readFileSync('ssl/certs/domain.com.crt'),
    ca: fs.readFileSync('ssl/certs/domain.com.cabundle')
};

var app = require('https').createServer(options, expressCallback),
    io = require('socket.io').listen(app);

io.sockets.on('connection', function (socket) {
    socket.on('message', function (message) {
        socket.broadcast.emit('message', message);
    });
});

app.listen(8543);

expressCallback.configure(function () {
    var hourMs = 1000 * 60 * 60;

    expressCallback.use(express.static(__dirname + '/public_html', {
        maxAge: hourMs
    }));

    expressCallback.use(express.directory(__dirname + '/public_html/'));
    expressCallback.use(express.errorHandler());
});

But getting same error mentioned here and here.

Error

500 Error: Connection closed

at SendStream.pipe (/node_modules/express/node_modules/send/lib/send.js:322:26)

at Object.oncomplete (fs.js:297:15)

Maybe it will be fixed soon.

@Epexa

signaler.js written in the log:
"warn - websocket parser error: reserved fields must be empty"

It turns out the problem was with Node.js!

"It fixed and work with 0.10.10! I close this issue. Thank you for your support!"

Read more here: joyent/node#5557 (comment)


My working version of the script signaler.js:

    var fs = require('fs');
    var options = {
        pfx: fs.readFileSync('/mydir/secure.pfx')
    };
    var app = require('express')(),
    server = require('https').createServer(options, app),
    io = require('socket.io').listen(server);
    server.listen(8543);

    io.sockets.on('connection', function (socket) {
        if (!io.connected) io.connected = true;

        socket.on('new-channel', function (data) {
            onNewNamespace(data.channel, data.sender);
        });
    });

    function onNewNamespace(channel, sender) {
        io.of('/' + channel).on('connection', function (socket) {
            if (io.isConnected) {
                io.isConnected = false;
                socket.emit('connect', true);
            }

            socket.on('message', function (data) {
                if (data.sender == sender) socket.broadcast.emit('message', data.data);
            });
        });
    }

Muaz Khan thanks for the help!

@Epexa Epexa closed this
@NerdAlsoRan

I am trying to connect node.js server with socket.io using ssl. My server has windows 2008 server and iis installed. I intalled ssl my domain name on IIS and I can connect with my domain via http s://mydomain.com. But I cant connect my node.js server with ssl. I am using following code on my server,Have you got any ideas what the problem might be?

var fs = require('fs');

var options = {
pfx: fs.readFileSync('sslkey.pfx'),
passphrase:'password'
};

var express = require('express'),
    app = express(),
    server = require('https').createServer(options,app),    
    server2 = require('http').createServer(app),    
    io = require('socket.io').listen(server, {log: true});

function sendCrossDomain(req, res){
//return;
    res.set('Content-Type', 'text/xml; charset=utf-8');
    res.sendfile(__dirname + '/crossdomain-test.xml');
};

app.get('/crossdomain.xml', sendCrossDomain);
app.get('/', sendCrossDomain);

server.listen(9595);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.