Mark Hughes
Mark Hughes

Reputation: 438

NodeJS + Express + Socket.IO + SSL

So I'm trying to use NodeJS with Express + Socket.IO.

I've looked at about 5 other stack overflow questions, and looked over the documentation to the point my eyes feel like they're going to bleed!

I've got that to work by the following:

var fs           =  require('fs'),
    privateKey   =  fs.readFileSync('/path/to/private.key').toString(),
    certificate  =  fs.readFileSync('/path/to/certificate.crt').toString();

var options  = {
                   key: privateKey,
                   cert: certificate
               };

var express  = require('express'),
    app      = express(express.logger()),
    https    = require('https'),
    server   = https.createServer(options, app),
    io       = require('socket.io').listen(server),
    routes   = require('./routes');

app.configure(function() {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
});


server.listen(5050, function() {
    console.log("Server a-hoy!");
});

app.get('/', routes.index);

io.sockets.on('connection', function (socket) {
  socket.on('givemeresults', function (data) {
    io.sockets.emit('results', { some: 'data' });
  });
});

Except for the fact that when I run this:

curl https://test.something.net:5050

I get the following (not wanted) results

curl: (35) Unknown SSL protocol error in connection to test.something.net:5050 

The whole point of this is so we can use Socket.io in the background of our real webpage to deliver information back to the user.

It works when I use normal http. The certificates are correct as well.

Can you see any possible reason to this failing?

I have had to remove the existing SSL Certificate locations, and the domain name, for security reasons.

Edit:

When running:

[root@example exampleapp]# node app.js
   info  - socket.io started
Server a-hoy!

Upvotes: 1

Views: 3524

Answers (2)

Christian Nelson
Christian Nelson

Reputation: 96

Newer versions of haproxy support ssl. Haproxy is incredibly stable and performant, and I trust it to handle ssl over other (software) solutions. I spent a fair bit of time trying to get haproxy + socket.io (web and flash socket support) + ssl working. I blogged about it so that others might avoid spending as much time on it.

http://blog.carbonfive.com/2013/05/02/using-haproxy-with-socket-io-and-ssl/

This configuration has been working pretty awesome for us.

Cheers, Christian

Upvotes: 2

Matt Sergeant
Matt Sergeant

Reputation: 2852

Honestly it's far easier to have something in front of Node do SSL (and faster too - Node's SSL support hasn't been optimized too well yet). We use Stud in front of HAProxy in front of Node, others use just HAProxy now that it has SSL support.

Stud is very easy to setup, and has been rock solid for us.

Upvotes: 1

Related Questions