Why do you need a message queue for a chat with web sockets?

前端 未结 3 1516
醉梦人生
醉梦人生 2021-02-02 01:58

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

相关标签:
3条回答
  • 2021-02-02 02:10

    Simple answer ...

    For a simple chat app you don't need a queue (e.g. signalr would do exactly this without the queue).

    Typically though real world applications are not just "a simple chat app", the queue might represent the current state of the room for new users joining perhaps, so the server knows what list of messages to serve up when that happens.

    Also it's worth noting that message queues are often implemented when you want reliable messaging (e.g. Service bus) to ensure that all messages definitely get to where they should go even if the first attempt fails. So it's likely that the queue is included in many examples as a default primer in to later problem solving.

    0 讨论(0)
  • 2021-02-02 02:22

    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.

    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.

    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.

    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.

    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.

    0 讨论(0)
  • 2021-02-02 02:23

    uhmm I didn't understand exactly for are you looking for...

    but In RabbiMQ you always publish a messages to an exchange and consume the message using a queue.

    to "broadcast that message" you need to consume it.

    hope it helps

    0 讨论(0)
提交回复
热议问题