Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io broadcast function & Redis pub/sub architecture

i would appreciate if somebody could help me with a small doubt.

Whats the difference between using socket.io broadcast function and designing the architecture with pub/sub on Redis?.

For instance, on the further example, the node.js server is listening (socket.io) CRUD requests (create) for a "key" (model 'todo'), and value 'data'. The moment it receive it, it emits again to the same user, and broadcast to all users listening on the same "channel".

socket.on('todo:create', function (data, callback) {
    var id = guid.gen()
      , todo = db.set('/todo/' + id, data)
      , json = todo._attributes;

    socket.emit('todos:create', json);
    socket.broadcast.emit('todos:create', json);
    callback(null, json);
});

But there is another way of "broadcasting" something using socket.io, and is using a pub/sub platform with Redis for key:value functions. For instance, on the further case, we are listening for a CRUD request based on a "key" (model), function (create), and value (data). But on this case, once its received, is not sent via "socket.broadcast.emit()", but published on Redis:

socket.on(key + ':create', function (data, callback) {
  var t = new ModelClass(data)
    , name = '/' + key + ':create';
  t.save(function (err) {
    pub.publish(key, JSON.stringify({key: name, data: t}));
  });
});

So on the server side, every change made on a model (and published to Redis), will be catched up (handler), and sent to the user client side (on my case, backbone.js), that will render its model according to the key:value received:

sio.on('connection', function (socket) {

   sub.on('message', function (channel, message) {
      var msg = JSON.parse(message);
      if (msg && msg.key) {
          socket.emit(msg.key, msg.data);
      }
});

So my question is very simple :-) : whats the difference of both architectures? which one is more scalable? better design? mode advance?

It looks to me that the pub/sub architecture is suitable for platforms that doesn't support 'realtime' naturally, like Ruby, in contrast to Node.js, that supports it natively.. Am i wrong?

like image 228
user1106811 Avatar asked Jun 04 '12 11:06

user1106811


People also ask

How do I send a broadcast message to socket?

To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it. var io = require('socket.io'). listen(80); io.

What is IO sockets emit?

emit() to send a message to all the connected clients. This code will notify when a user connects to the server. io.on("connection", function(socket) { io.emit(“user connected”); }); If you want to broadcast to everyone except the person who connected you can use.

Which event is used to send data to all the clients in the current namespace except the sender?

emit('message', 'gg'); //sending to all clients in namespace 'myNamespace', include sender socket. emit(); //send to all connected clients socket. broadcast. emit(); //send to all connected clients except the one that sent the message socket.

When should I use Socket.IO rooms?

those objects are not meant to be directly modified, you should always use socket. join(...) and socket. leave(...) instead. in a multi-server setup, the rooms and sids objects are not shared between the Socket.IO servers (a room may only "exist" on one server and not on another).


1 Answers

Socket.io's broadcast method will broadcast to all the sockets that are connected to the current Node.js server, but what if your application becomes popular and you need more than one server to host all the socket.io connections? Using Redis' Pub/Sub your messages can be distributed to many servers at once.

like image 145
Jesse Hattabaugh Avatar answered Oct 05 '22 22:10

Jesse Hattabaugh