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

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

问题描述

很简单的问题.我正在使用 nodejs 作为我的后端构建一个实时游戏,我想知道是否有任何可用的信息来说明哪个更可靠,哪个更有效?我在整个代码中大量使用 Redis 和 Socket.io.所以我想知道我是否应该使用 Socket.io 的 Rooms 或者我会最好使用 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~on 服务器)客户端接收消息.出于这个原因,如果您想通知所有(服务器)客户端特定于每个客户端的信息,并且可能在传递给浏览器客户端之前进行一些处理,那么最好使用 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 pub/sub 非常适合所有客户端都可以直接访问 redis 的情况.如果你有多个节点服务器,一个可以向其他节点推送消息.

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.

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

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

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

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