socket.io 广播功能&Redis 发布/订阅架构 [英] socket.io broadcast function & Redis pub/sub architecture

查看:51
本文介绍了socket.io 广播功能&Redis 发布/订阅架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人能帮助我解决一个小问题,我将不胜感激.

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

在Redis上使用socket.io广播功能和使用pub/sub设计架构有什么区别?.

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

例如,在进一步的示例中,node.js 服务器正在侦听 (socket.io) CRUD 请求(创建)键"(模型todo")和值数据".收到它的那一刻,它再次发送给同一个用户,并广播给在同一个频道"上收听的所有用户.

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);
});

但是还有另一种使用 socket.io 来广播"某些东西的方法,并且使用带有 Redis 的发布/订阅平台来实现 key:value 函数.例如,在进一步的情况下,我们正在侦听基于键"(模型)、函数(创建)和值(数据)的 CRUD 请求.但是在这种情况下,一旦收到,就不会通过socket.broadcast.emit()"发送,而是在 Redis 上发布:

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}));
  });
});

因此在服务器端,对模型所做的每一个更改(并发布到 Redis),都将被赶上(处理程序),并发送到用户客户端(在我的情况下,backbone.js),这将呈现其模型根据接收到的键:值:

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?

在我看来,pub/sub 架构适用于自然不支持实时"的平台,例如 Ruby,与原生支持它的 Node.js 形成对比..我错了吗?

推荐答案

您可能想看看 Socket.io 中的 RedisStore.您可以将 socket.io 设置为使用 RedisStore,而不是使用 MemoryStore.以便您可以扩展您的应用程序.

You might want to take a look at RedisStore in Socket.io. Instead of using a MemoryStore, you can set socket.io to use RedisStore. so that you can scale up your application.

http://www.ranu.com.ar/2011/11/redisstore-and-rooms-with-socketio.html

如此处所述(https://stackoverflow.com/a/9275798/327004),使用 Socket.io使用 RedisStore 可能会导致您在会话方面出现一些问题,因为会话不会在工作人员之间共享.

As mentioned here ( https://stackoverflow.com/a/9275798/327004 ), using Socket.io with RedisStore can lead you to some problems with sessions because sessions are not shared between workers.

这篇关于socket.io 广播功能&Redis 发布/订阅架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆