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

查看:98
本文介绍了为什么与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天全站免登陆