Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lightweight alternative to socket.io for transport only

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.

like image 510
voidvector Avatar asked Aug 19 '12 02:08

voidvector


People also ask

What can I use instead of WebSockets?

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.

Does Socket.IO work on mobile?

Installing the Dependencies​Now we can use Socket.IO on Android!

Is Socket.IO a WebSocket?

Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet.

What is Socket.IO transport?

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.


2 Answers

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...

like image 192
João Pinto Jerónimo Avatar answered Oct 19 '22 04:10

João Pinto Jerónimo


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/ .

like image 33
First Zero Avatar answered Oct 19 '22 03:10

First Zero