node.js 在 socket.io 中使用 RedisStore 的例子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9267292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:10:46  来源:igfitidea点击:

Examples in using RedisStore in socket.io

node.jsredissocket.io

提问by Rollin_s

I am trying to scale a simple socket.io app across multiple processes and/or servers.

我正在尝试跨多个进程和/或服务器扩展一个简单的 socket.io 应用程序。

Socket.io supports RedisStore but I'm confused as to how to use it.

Socket.io 支持 RedisStore 但我对如何使用它感到困惑。

I'm looking at this example, http://www.ranu.com.ar/post/50418940422/redisstore-and-rooms-with-socket-io

我在看这个例子, http://www.ranu.com.ar/post/50418940422/redisstore-and-rooms-with-socket-io

but I don't understand how using RedisStore in that code would be any different from using MemoryStore. Can someone explain it to me?

但我不明白在该代码中使用 RedisStore 与使用 MemoryStore 有何不同。有人可以向我解释一下吗?

Also what is difference between configuring socket.io to use redisstore vs. creating your own redis client and set/get your own data?

另外,配置 socket.io 以使用 redisstore 与创建自己的 redis 客户端并设置/获取自己的数据有什么区别?

I'm new to node.js, socket.io and redis so please point out if I missed something obvious.

我是 node.js、socket.io 和 redis 的新手,所以请指出我是否遗漏了一些明显的东西。

采纳答案by Linus Gustav Larsson Thiel

but I don't understand how using RedisStore in that code would be any different from using MemoryStore. Can someone explain it to me?

但我不明白在该代码中使用 RedisStore 与使用 MemoryStore 有何不同。有人可以向我解释一下吗?

The difference is that when using the default MemoryStore, any message that you emit in a worker will only be sent to clients connected to the same worker, since there is no IPC between the workers. Using the RedisStore, your message will be published to a redis server, which all your workers are subscribing to. Thus, the message will be picked up and broadcast by all workers, and all connected clients.

不同之处在于,当使用 default 时MemoryStore,您在工作人员中发出的任何消息只会发送到连接到同一工作人员的客户端,因为工作人员之间没有 IPC。使用RedisStore,您的消息将发布到您的所有工作人员都订阅的 redis 服务器。因此,该消息将被所有工作人员和所有连接的客户端接收和广播。

Also what is difference between configuring socket.io to use redisstore vs. creating your own redis client and set/get your own data?

另外,配置 socket.io 以使用 redisstore 与创建自己的 redis 客户端并设置/获取自己的数据有什么区别?

I'm not intimately familiar with RedisStore, and so I'm not sure about all differences. But doing it yourself would be a perfectly valid practice. In that case, you could publish all messages to a redis server, and listen to those in your socket handler. It would probably be more work for you, but you would also have more control over how you want to set it up. I've done something similar myself:

我对 并不十分熟悉RedisStore,因此我不确定所有差异。但是自己做这件事是一种完全有效的做法。在这种情况下,您可以将所有消息发布到 redis 服务器,并侦听套接字处理程序中的消息。这对您来说可能会更多,但您也可以更好地控制如何设置它。我自己也做过类似的事情:

// Publishing a message somewhere
var pub = redis.createClient();
pub.publish("messages", JSON.stringify({type: "foo", content: "bar"}));

// Socket handler
io.sockets.on("connection", function(socket) {
  var sub = redis.createClient();
  sub.subscribe("messages");
  sub.on("message", function(channel, message) {
    socket.send(message);
  });

  socket.on("disconnect", function() {
    sub.unsubscribe("messages");
    sub.quit();
  });
});

This also means you have to take care of more advanced message routing yourself, for instance by publishing/subscribing to different channels. With RedisStore, you get that functionality for free by using socket.io channels (io.sockets.of("channel").emit(...)).

这也意味着您必须自己处理更高级的消息路由,例如通过发布/订阅不同的频道。使用RedisStore,您可以通过使用 socket.io 通道 ( io.sockets.of("channel").emit(...))免费获得该功能。

A potentially big drawback with this is that socket.io sessions are not shared between workers. This will probably mean problems if you use any of the long-polling transports.

一个潜在的大缺点是 socket.io 会话不在工作人员之间共享。如果您使用任何长轮询传输,这可能意味着问题。

回答by Martin Kapfhammer

I set up a small github project to use redis as datastore.

我建立了一个小的 github 项目来使用 redis 作为数据存储。

Now you can run multiple socket.io server processes.

现在您可以运行多个 socket.io 服务器进程。

https://github.com/markap/socket.io-scale

https://github.com/markap/socket.io-scale

回答by Kaicui

Also what is difference between configuring socket.io to use redisstore vs. creating your own redis client and set/get your own data?

另外,配置 socket.io 以使用 redisstore 与创建自己的 redis 客户端并设置/获取自己的数据有什么区别?

The difference is that, when you use 'RedisStore', the socket.io itself will save the socket heartbeat and session info into the Redis, and if you use cluster with node.js, the user client can work.

不同的是,当你使用'RedisStore'时,socket.io本身会将socket心跳和会话信息保存到Redis中,如果你使用集群和node.js,用户客户端可以工作。

Without redis, the client might change the node.js process next time, so the session will be lost.

如果没有redis,客户端下次可能会更改node.js进程,因此会话将丢失。

回答by Tie Zhong

The difference is, if you have a cluster of node.js instances running, memStore won't work since it's only visible to a single process.

不同之处在于,如果您有一组 node.js 实例正在运行,则 memStore 将无法工作,因为它仅对单个进程可见。