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.
SSE is an excellent alternative to WebSockets. They are limited to the browser's connection pool limit of ~6 concurrent HTTP connections per server, but they provide a standard way of pushing data from the server to the clients over HTTP, which load balancers and proxies understand out-of-the-box.
Installing the DependenciesNow we can use Socket.IO on Android!
Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet.
transports The low-level connection to the Socket.IO server can either be established with: HTTP long-polling: successive HTTP requests ( POST for writing, GET for reading) WebSocket.
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/ .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With