I had built an application using Node JS and socket.io. It was working fine. Since I had to move the application over a secure connection, I changed the code to support SSL.
But, now I am unable to do a socket.emit. Neither server, nor the client is able to emit the messages.
The connections are made successfully, but nothing gets emitted afterwards.
Here is the code.
Server Side
var fs = require( 'fs' );
var options = {
key: fs.readFileSync('/etc/httpd/conf.d/ssl/*.xyz.in.key'),
cert: fs.readFileSync('/etc/httpd/conf.d/ssl/xyz.in.crt'),
requestCert: true,
rejectUnauthorized: false // Tried both, with and without this line.
};
var io = require( 'socket.io' ).listen( 8888, options );
io.sockets.on('connection', OnConnection );
io.set("origins", "*:*"); // Tried with and without this line.
// Called when a connection is made with a new Client.
function OnConnection ( socket )
{
console.log( socket.id + " connected" ); // This line gets printed.
socket.on('ev_SendMsgForApproval', OnMsgReceivedForModApproval );
}
function OnMsgReceivedForModApproval( data )
{
console.log( data ); //This does not get executed. :(
}
Client Side
var socket = io.connect("https://xyz.com:8888", {secure : true} );
// This message get called for sure.
function OnMsgSendClick()
{
console.log( "Hi" ); // This gets printed on browser's console.
socket.emit( 'ev_SendMsgForApproval', { chatMsg: "Hi" } );
return false;
}
I have also tried to do emit from server, but that data, does not reach the client either. In debug I am able to see that server has emitted something, but that does not reach the client.
All this was working fine without the SSL.
What could be the issue? Stuck with this from a long time. Please help.
I always leave the ssl burden to stunnel, an ssl proxy that listens on a secure port defined by you (such as 443) and forwards all incoming traffic, already decrypted, to another port where your non-ssl application is listening (usually 80).
Here you have a gist with a simple stunnel configuration: https://gist.github.com/mbenedettini/5911415
I solved the problem after a few days.
The problem was in the following line on the client side.
var socket = io.connect("https://xyz.com:8888", {secure : true} );
We are not supposed to add {secure : true}, so the line simply becomes
var socket = io.connect("https://xyz.com:8888" );
How I came to know about this solution? I learnt how to analyse the Web Socket data traffic using Chrome Dev tools
Very soon I found out that the client was emitting {secure : true} as the 1st element of the array. This changed the structure of the entire object. As a result of all this server was not able to resolve the Event Name, since the first array element was not containing the event name.
I removed {secure : true} and it worked. :)