I am playing around with node.js and socket.io-client. I am trying to connect to a channel that does not exist in order to trigger the event ‘connect_failed’ (as specified at https://github.com/LearnBoost/socket.io-client ).
However I can’t get the event working:
var clientio = require('socket.io-client');
console.log('Trying stuff ...');
// the channel does not exist
var socket = clientio.connect( 'http://localhost:4000/news' );
// I expect this event to be triggered
socket.on('connect_error', function(){
console.log('Connection Failed');
});
socket.on('connect', function(){
console.log('Connected');
});
socket.on('disconnect', function () {
console.log('Disconnected');
});
socket.send('hi there');
If I execute will this will happen:
$ node tmp_clientio.js
Trying stuff ...
Any ideas about how to trigger an error if connecting to a channel that does not exist?
UPDATE: Renamed connect_failed to connect_error
I ran into the same issue. There is a un-documented difference between namespace mode on and off that I also ran into, in the end I had to use the chrome debugger to work it out.
So, you see above that you have to get the actual socket
ns_news.socketfrom your namespace if you want to bind “socket level” events such as connect failed, disconnect, etcNote that you now use ns_news to send or receive events
Finally, be sure to bind the
socket.on('error', ...)event as well – it occurs if the port wasn’t availableFull implimentation of 8080 connect with fallback to port 80
I did this in my implementation (try port 8080, if that fails, try port 80 through nginx)
I also added an nginx proxy_pass rule, which looks like this
The Upgrade and Connection options here (from more recent versions of nginx) are required to upgrade the connection between nginx and socket.io to support websockets. That means that your webserver can support websockets on port 80.
Furthermore if your server supports ssl / https, this will proxy wss which is the secure version of websockets.