We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No description provided.
The text was updated successfully, but these errors were encountered:
在开始思考分布式会有什么问题时,先来回答一个问题: 服务端如何与客户端交流?
在 ws 服务端,当与客户端连接成功后,会生成一个对象 connection,ws 会维护一个与客户端所有连接的 connections。如果想要主动推送消息到客户端,只需要调用API connection.sendText(message)。
connection
connections
connection.sendText(message)
那如何给所有人广播消息呢?
服务器只需要与它自身的所有连接 server.connections 挨个发消息就是广播,所以它只是一个伪广播:我要给群里所有人发消息,但我不能在群里发,只能挨个私发。
server.connections
当单节点时所有用户都能正常受到通知,流程如下
这时所有用户都能收到消息通知
当多节点时,就会有部分用户无法正常受到通知,从以下流程图中可以很清楚地看到问题所在
负载到节点2的所有用户都没有收到消息通知
多节点服务器就会有分布式问题,解决分布式问题就找一个大家都能找到的地,比如说 Redis,比如说 Kafka 等消息件
Redis
Kafka
改进后流程图如下
其中有一个细节是 pub/sub 那里,redis 的 pubsub 较 Kafka 等消息中间件更为轻便,最主要的是与ws集成的社区方案比较成熟,这点很重要,如 Node 中的以下两个
pubsub
pubsub 在 redis 中的命令如下
publish channel message
subscribe
如果我们要订阅 eat 这个 channel 的话,图示如下
eat
channel
面试官见我回答完问题后,又一次追问
那 websocket 如何向特定的用户组推送消息?
假如一个学校有以下数据结构
Class
Student
那假如要向 Class:201901 班级的所有学生发送通知,应该如何实现
Class:201901
欢迎在 Issue 中讨论: 【Q029】websocket 如何向特定用户组推送消息
借用解决方案的图作为小结
Sorry, something went wrong.
No branches or pull requests
No description provided.
The text was updated successfully, but these errors were encountered: