I am creating an chat application where I have a rest API and a socket.io server, What I want to do is the user will send messages to rest API, The api will persist those messages in database and then send these messages to the rabbimq queue then rabbitmq will send these messages to socket.io if the receiving user is online, Else the message will be stored in the queue and when the user will come online the user will retrieve these messages from the queue however I want to implement this in a way like whatsapp, The messages will be for a particular user and the user will only receive those messages which are meant for them i.e I don't want to broadcast messages I want only particular user to receive those messages
2 Answers
Chat should be a near-real-time application, there are multiple ways of modeling such a thing. First of all, you can use HTTP pooling
, HTTP long pooling
but some time ago there where introduced the new application-level protocol - web socket
. It would be good for you, along with Stomp messages
. Here you can check a brief example. And sending messages to specific users is also supported out-of-the-box example

- 353
- 1
- 5
-
Yeah but how can I solve the problem where if the user is offline the messages will be stored in a queue? – Jun 01 '19 at 20:18
1
To send messages to specific sockets you can use rooms: io.to(room).emit(msg)
. Every socket is a part of a room with the same name as the socket id.
2
I wouldn't wait for the message to be written to the database before sending it out through socket.io, your API can do both at once. When a user connects they can retrieve their messages from the database then listen for new ones on their socket.

- 1,028
- 2
- 10
- 22