Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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)

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 
});
like image 423
Alin Ciocan Avatar asked Aug 24 '16 11:08

Alin Ciocan


People also ask

Why message queuing is required?

Message queues provide communication and coordination for these distributed applications. Message queues can significantly simplify coding of decoupled applications, while improving performance, reliability and scalability. You can also combine message queues with Pub/Sub messaging in a fanout design pattern.

How does WebSocket work for chat?

WebSocket uses a unified TCP connection and needs one party to terminate the connection. Until it happens, the connection remains active. HTTP needs to build a distinct connection for separate requests. Once the request is completed, the connection breaks automatically.

What are the advantages of using message queue for IPC?

Advantages of message queues First, they are asynchronous. After the message has been queued, the caller doesn't wait for it to be processed. It can free up that thread for handling additional work. If the caller expects a response, then the caller must pull response messages from a second queue.

Why do we need a messaging system?

A messaging system is responsible for transferring data from one application to another so the applications can focus on data without getting bogged down on data transmission and sharing. Distributed messaging is based on the concept of reliable message queuing.


2 Answers

I may be late for the answer as the messaging domain changed rapidly in last few years. Applications like WhatsApp do not store messages in their database, and also provide E2E encryption. Coming to RabbitMQ, they support MQTT protocol which is ideal for low latency high scalability applications. Thus using such queuing services offload the heavy work from your server and provide features like scalability and security.

like image 93
HSharma Avatar answered Oct 18 '22 10:10

HSharma


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.

like image 42
Derick Bailey Avatar answered Oct 18 '22 09:10

Derick Bailey