我应该使用什么? Socket.io房间还是Redis pub-sub? [英] What should I be using? Socket.io rooms or Redis pub-sub?

查看:219
本文介绍了我应该使用什么? Socket.io房间还是Redis pub-sub?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单的问题.我正在使用nodejs作为后端构建实时游戏,我想知道是否有任何信息可用于确定哪个更可靠,哪个更有效? 我在整个代码中都大量使用Redis和Socket.io.所以我想知道我是否应该使用Socket.io的房间还是我会使用使用redis' pub-sub 更好吗?

Pretty simple question. I am building a realtime game using nodejs as my backend and I am wondering if there is any information available on which one is more reliable and which one is more efficient? I am heavily using both Redis and Socket.io throughout my code. So I want to know whether I should be utilizing Socket.io's Rooms or I would be better off using redis' pub-sub ?

更新: 刚意识到有一个非常重要的原因,为什么您可能想在socket.io房间中使用redis pub/sub.使用Socket.io房间,当您发布给侦听器时,(浏览器)客户端会接收消息,而redis实际上是(重新分配到服务器上的)客户端接收消息.因此,如果您想通知所有(服务器)客户端特定于每个客户端的信息,并且可能在传递给浏览器客户端之前进行一些处理,那么最好使用Redis.使用redis可以触发一个事件以生成每个用户的个人数据,就像使用socket.io一样,您实际上必须一次生成所有用户的唯一数据,然后遍历他们并发送他们的个人数据,这几乎破坏了房间的目的,至少对我来说是这样.

Update: Just realized there is a very important reason why you may want to use redis pub/sub over socket.io rooms. With Socket.io rooms when you publish to listeners, the (browser)clients recieve the message, with redis it is actually the (redis~on server)clients who recieve messages. For this reason, if you want to inform all (server)clients of information specific to each client and maybe do some processing before passing on to browser clients, you are better off using redis. Using redis you can just fire off an event to generate each users individual data, where as with socket.io you have to actually generate all the users unique data at once, then loop through them and send them their individual data, which almost defeats the purpose of rooms, at least for me.

不幸的是,出于我的目的,我暂时只使用redis.

Unfortunately for my purposes I am stuck with redis for now.

更新2:最终开发了一个插件,仅使用2个redis连接,但仍允许单个客户端处理,请参见下面的答案....

Update 2: Ended up developing a plugin to use only 2 redis connections but still allow for individual client processing, see answer below....

推荐答案

如果所有客户端都可以直接访问redis,Redis pub/sub非常有用.如果您有多个节点服务器,则一台服务器可以向其他服务器推送一条消息.

Redis pub/sub is great in case all clients have direct access to redis. If you have multiple node servers, one can push a message to the others.

但是如果您在浏览器中也有客户端,则需要其他一些东西来将数据从服务器推送到客户端,在这种情况下,socket.io很棒.

But if you also have clients in the browser, you need something else to push data from a server to a client, and in this case, socket.io is great.

现在,如果将socket.io与Redis存储一起使用,socket.io将在内部使用Redis pub/sub在服务器之间传播消息,并且服务器会将消息传播到客户端.

Now, if you use socket.io with the Redis store, socket.io will use Redis pub/sub under the hood to propagate messages between servers, and servers will propagate messages to clients.

因此,使用带有Redis存储配置了socket.io的socket.io房间可能对您来说最简单.

So using socket.io rooms with socket.io configured with the Redis store is probably the simplest for you.

这篇关于我应该使用什么? Socket.io房间还是Redis pub-sub?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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