Question

I use node.js and socket.io for our chat application and everything works good until we decide to implement SSL certificate.

Before SSL our SocketServer connect like this:

// Start up a Node server with Socket.IO
var io = require('socket.io').listen(8080,{
  'log level':1
});

and Socket like this

socket : io.connect('http://111.111.111.111:8080'),

After we implement we setup the Proxy directives under domain. Now any requests that are sent to https://example.com/nodejs will be passed to localhost:8080. So I changed connection like this:

// Instantiate the Socket.IO client and connect to the server
socket : io.connect('https://example.com/nodejs'),

But the connection faild and redirect to 404 page. I don't know where can be the problem so I need your help.

Thanks

Was it helpful?

Solution

Socket.io page not found cause it listen on http server and the website js connect to it by https, this is my solution, hope it helpful:

var https = require('https');
var express = require('express');
var app = express();

// app.use ...
// app.set ...

var credentials = {
    key: fs.readFileSync('./config/privateKey'),
    cert: fs.readFileSync('./config/certificate')
};

var https_server = https.createServer(credentials, app);

var io = SocketIO.listen(https_server, {});

io.set('authorization', function(handshakeData, callback) {
    // ...
});

io.sockets.on('connection', function(socket) {
    // ...
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top