Is there a lightweight alternative to socket.io for transport only?
I have an node.js application that uses socket.io simply as an message transport. My application is managing sessions and message routing on its own, I am simply using socket.io for transport -- websocket + whatever the default fallback is for older browsers.
The newer version of socket.io seems to get heavier and heavier, now comes with redis support, which I totally do not need.
The ws module is amazingly fast (look at the benchmarks), well tested, very very very lightweight, but with no you would have to do the fall-backs yourself, plus, it doesn't have an event emitter on top of it. But it's amazing at transporting only, if that's what you want. If you want a poor man's "session", just attach something to the ws object, like this:
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer( /* some config */);
wss.on('connection', function(ws) {
ws.on('message', function (message) {
try {
var obj = JSON.parse(message) // using JSON over the conversation
} catch (err) {
var obj = {};
console.log('probably not valid json');
}
switch (true) {
case obj.name !== undefined:
ws.name = obj.name; // Here's the poor man's session variable
ws.send('Hello '+ws.name);
break;
}
});
});
Now the only thing missing would be an event emitter on top of it...
There are other alternatives. faye
- http://faye.jcoglan.com/ is one of them. Its similar to socket.io but uses Bayeux
protocol. The other one if you prefer not to run a server - pusher
- http://pusher.com/ .
Look at sock.js - it tries to implement cross-browser websockets api and nothing more.