Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Can't Get SSL to Work With NodeJS

Can't Get SSL to Work With NodeJS

Problem

I'm trying to setup a secure socket.io connection. I've setup a localhost apache Server to run https on 443 but now I can't get the client-side socket.io to open a secure connection on port 9650.

The server side code is simply:

this.socket = require("socket.io").listen(port);

And the clientside is simply:

(https won't work here)

this.Socket = io.connect("https://localhost", {secure: true, port:9650});

io is defined so I can call connect but I get this error when I try to connect:

GET https://localhost:9650/socket.io/1/?t=1381630341479  socket.io.js:1659

I've tried variations on connect and can't find a working solution. Everything does work is secure is set to false but that's not what I'm looking for.

This IS NOT an http node application. It's a general use application. It seems like everything I good favors http-type servers and I don't need that here.

Problem courtesy of: Micah Williamson

Solution

You are getting an error because the client can't find the HTTPS resource when it is trying to handshake with the server. To fix this, you will need Socket.IO to listen on an instance of a HTTPS server.

Socket.IO will internally use a Http Server regardless of if you need one or not. By default, it is a HTTP server, so if you want to use transports over TLS, you will need to manually create a HTTPS server.

var fs = require('fs');
var https = require('https');

var options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

var server = https.createServer(options);
var io = require('socket.io').listen(server);

server.listen();
Solution courtesy of: hexacyanide

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Can't Get SSL to Work With NodeJS

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×