Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js, socket.io with SSL

I'm trying to get socket.io running with my SSL certificate however, it will not connect.

I based my code off the chat example:

var https = require('https'); var fs = require('fs'); /**  * Bootstrap app.  */ var sys = require('sys') require.paths.unshift(__dirname + '/../../lib/');  /** * Module dependencies. */  var express = require('express')   , stylus = require('stylus')   , nib = require('nib')   , sio = require('socket.io');  /**  * App.  */ var privateKey = fs.readFileSync('../key').toString(); var certificate = fs.readFileSync('../crt').toString(); var ca = fs.readFileSync('../intermediate.crt').toString();  var app = express.createServer({key:privateKey,cert:certificate,ca:ca });   /**  * App configuration.  */  ...  /**  * App routes.  */  app.get('/', function (req, res) {   res.render('index', { layout: false }); });  /**  * App listen.  */  app.listen(443, function () {   var addr = app.address();   console.log('   app listening on http://' + addr.address + ':' + addr.port); });  /**  * Socket.IO server (single process only)  */  var io = sio.listen(app,{key:privateKey,cert:certificate,ca:ca}); ... 

If I remove the SSL code it runs fine, however with it I get a request to http://domain.com/socket.io/1/?t=1309967919512

Note it's not trying https, which causes it to fail.

I'm testing on chrome, since it is the target browser for this application.

I apologize if this is a simple question, I'm a node/socket.io newbie.

Thanks!

like image 695
Beyond Avatar asked Jul 06 '11 16:07

Beyond


People also ask

Does Socket.IO work with https?

If you want socket.io to use https, just pass a key param to the listen() function. You can also run your server behind stunnel.

How do I secure a Socket.IO connection?

All you have to do is updating the remote session store on node server when a new login/logout happens in your php server. Show activity on this post. The excellent passport framework for express uses secure cookies to validate identity. There is even a module to access it from socket.io.

Does Socket.IO use WSS?

Note: You can use either https or wss (respectively, http or ws ).


2 Answers

Use a secure URL for your initial connection, i.e. instead of "http://" use "https://". If the WebSocket transport is chosen, then Socket.IO should automatically use "wss://" (SSL) for the WebSocket connection too.

Update:

You can also try creating the connection using the 'secure' option:

var socket = io.connect('https://localhost', {secure: true}); 
like image 111
kanaka Avatar answered Sep 18 '22 10:09

kanaka


The following is how I set up to set it up with express:

    var app = require('express')();     var https = require('https');     var fs = require( 'fs' );     var io = require('socket.io')(server);      var options = {         key: fs.readFileSync('./test_key.key'),         cert: fs.readFileSync('./test_cert.crt'),         ca: fs.readFileSync('./test_ca.crt'),          requestCert: false,         rejectUnauthorized: false     }      var server = https.createServer(options, app);     server.listen(8080);                io.sockets.on('connection', function (socket) {         // code goes here...     });          app.get("/", function(request, response){         // code goes here...     })     

Update : for those using lets encrypt use this

var server = https.createServer({                  key: fs.readFileSync('privkey.pem'),                 cert: fs.readFileSync('fullchain.pem')               }, app); 
like image 25
emonik Avatar answered Sep 18 '22 10:09

emonik