Question

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:

<script type="text/javascript" src="http://localhost:9650/socket.io/socket.io.js"></script>

(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.

Was it helpful?

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top