io.adapter如何在引擎盖下工作? [英] How io.adapter works under the hood?

查看:189
本文介绍了io.adapter如何在引擎盖下工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用由node.js + express + socket.io提供的1-1个聊天室应用程序。
我正在追踪文章: Socket.IO - 房间和命名空间

I'm working on 1-1 chat rooms application powered by node.js + express + socket.io. I am following the article: Socket.IO - Rooms and Namespaces

在本文中,他们演示了如何使用模块 socket.io-redis

In the article they demonstrate how to initiate the io.adapter using the module socket.io-redis:

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));



两个问题:



Two questions:


  1. 在文档中,他们提到另外两个参数: pubClient subClient 。我应该提供吗?有什么区别?

  2. io.adapter的行为如何?例如,如果用户A连接到服务器A,用户B是服务器B,并且他们想要相互交谈。

  1. In the docs, They are mentioning two more arguments: pubClient and subClient. Should I supply them? What's the difference?
  2. How the io.adapter behaves? For example, if user A is connected to server A and user B is server B, and they want to "talk" with each other. What's going under the hood?

谢谢。

推荐答案


  1. 您不需要传递自己的pubClient / subClient。如果您传递主机/端口,将为您创建。但是,如果您想要自己创建,无论什么原因(例如,您想要调整重新连接超时),您将创建这两个客户端并将其传递给适配器。

  1. You do not need to pass your own pubClient/subClient. If you pass host/port, they will be created for you. But, if you want to create them yourself, for any reason (e.g. you want to tweak reconnection timeouts), you create those 2 clients and pass it to adapter.

适配器广播全部内部发送。所以,它给你集群功能。例如。假设您有聊天应用程序,并且在负载平衡器后面有3个node.js服务器(因此它们共享单个URL)。还假设6个不同的浏览器连接到负载均衡器URL,并将它们路由到3个独立的node.js进程,每个node.js服务器有2个用户。如果客户端#1发送消息,node.js#1将执行类似 io.to('chatroom')的操作。emit('来自用户#1'的msg)。没有适配器,服务器#1用户将接收到发射,但不会剩余4个用户。但是,如果使用适配器,则node.js#2和node.js#3的其余部分将会收到发送完成的信息,并向其客户端发出相同的发送信息,所有6个用户将收到初始消息。

The adapter broadcasts all emits internally. So, it gives you the cluster feature. E.g. lets suppose that you have chat application, and you have 3 node.js servers behind load balancer (so they share single URL). Lets also assume that 6 different browsers connect to load balancer URL and they are routed to 3 separate node.js processes, 2 users per node.js server. If client #1 sends a message, node.js #1 will do something like io.to('chatroom').emit('msg from user #1'). Without adapter, both server #1 users will receive the emit, but not the remaining 4 users. If you use adapter, however, remaining node.js #2 and node.js #3 will receive info that emit was done and will issue identical emit to their clients - and all 6 users will receive initial message.

这篇关于io.adapter如何在引擎盖下工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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