为什么你需要一个消息队列来与 Web 套接字聊天? [英] Why do you need a message queue for a chat with web sockets?

查看:17
本文介绍了为什么你需要一个消息队列来与 Web 套接字聊天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上看到了很多使用 Web 套接字和 RabbitMQ 的聊天示例(https://github.com/videlalvaro/rabbitmq-chat),但是我不明白为什么它需要它作为聊天应用程序的消息队列.

I have seen a lot of examples on the internet of chats using web sockets and RabbitMQ (https://github.com/videlalvaro/rabbitmq-chat), however I do not understand why it is need it a message queue for a chat application.

为什么不能通过 Web 套接字将消息从浏览器发送到服务器,然后服务器再使用具有广播方法的 Web 套接字将该消息广播到其余活动浏览器?(也许我错过了什么)

Why it is not ok to send the message from the browser via web sockets to the server and then the server to broadcast that message to the rest of active browsers using again web sockets with broadcast method? (maybe I am missing something)

伪代码示例(使用socket.io):

Pseudo code examples (using socket.io):

// client (browser)
socket.emit("message","my great message that will be received by all"


// server (any server can be, but let's just say that it is also written in JavaScript
socket.on("message", function(msg) {
  socket.broadcast.emit(data);
});

// the rest of the browsers
socket.on("message", function(msg) {
  // display on the screen the message 
});

推荐答案

我个人认为 RabbitMQ 不应该用于聊天室.至少,不在应用程序的聊天"或房间"部分.

i don't think RabbitMQ should be used for a chat room, personally. at least, not in the "chat" or "room" part of the application.

除非你的聊天室根本不关心历史——而且我认为大多数人都关心这一点——否则像 RMQ 这样的消息队列没有多大意义.

unless your chat rooms don't care about history at all - and i think most do care about that - a message queue like RMQ doesn't make much sense.

你会更好 将消息存储在数据库中并为每个用户保留一个标记,以说明他们上次看到的消息.

you would be better off storing the message in a database and keeping a marker for each user to say what message they last saw.

现在,您可能最终需要 RMQ 之类的东西来促进聊天应用程序的进程.例如,您可以从 Web 服务器卸载进程,并通过 RMQ 将所有消息推送到更新数据库和缓存层的后端服务.

now, you may end up needing something like RMQ to facilitate the process of the chat application. you can offload process from the web servers, for example, and push all messages through RMQ to a back-end service that updates the database and cache layers, for example.

这将使您能够更快地扩展前端 Web 服务器,并支持每台 Web 服务器更多的用户.这听起来像是对 RMQ 的一种很好的使用,但并不特定于聊天应用程序.这只是扩展 Web 应用程序/系统的好习惯.

this would allow you to scale the front-end web servers much faster, and support more users per web server. and that sounds like a good use of RMQ, but is not specific to chat apps. it's just good practice for scaling web apps / systems.

根据我的经验,关键是 RMQ 不负责将消息传递给用户/聊天室.这是通过 websocket 或旨在为每个用户使用的类似技术实现的.

the key, in my experience, is that RMQ is not responsible for delivery of the messages to the users / chat rooms. that happens through websockets or similar technologies that are designed to be used per user.

这篇关于为什么你需要一个消息队列来与 Web 套接字聊天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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