Setting up SSL with sockjs


How do I install an https/ssl certificate (and key) in sockjs (running on nodejs)?

I'm currently running the most basic setup, this is, the "echo" example setup. It is a follows:

var http = require('http');
var sockjs = require('sockjs');
var node_static = require('node-static');

// 1. Echo sockjs server
var sockjs_opts = {sockjs_url: ""};

var sockjs_echo = sockjs.createServer(sockjs_opts);
sockjs_echo.on('connection', function(conn) {
    conn.on('data', function(message) {

// 2. Static files server
var static_directory = new node_static.Server(__dirname);

// 3. Usual http stuff
var server = http.createServer();
server.addListener('request', function(req, res) {
    static_directory.serve(req, res);
server.addListener('upgrade', function(req,res){

sockjs_echo.installHandlers(server, {prefix:'/echo'});

console.log(' [*] Listening on' );
server.listen(9999, '');


Problem courtesy of: Fredefl


Okay I got it to work by changing the code to:

var http = require('https');
var sockjs = require('sockjs');
var fs = require("fs");

var options = {
  key: fs.readFileSync('ssl.key'),
  cert: fs.readFileSync('ssl.crt')

var echo = sockjs.createServer();
echo.on('connection', function(conn) {
    conn.on('data', function(message) {
    conn.on('close', function() {});

var server = http.createServer(options);
echo.installHandlers(server, {prefix:'/echo'});
server.listen(9999, '');
Solution courtesy of: Fredefl


There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.