How to send chat notificattion to user offline/online by socket.io? - node.js

I can send messages to others by socket when they are in a room but now I want to send a message when other users are not in a specific room or offline, as a notification.

You need to intergrate database with the socket application then only you store message offline and display to user when online.

Related

Need recommendation regarding rabbitmq

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
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
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.

How to send message to specific user in socket.io?

I am creating a chat application in Socket.io and node I am new in this I have a some query regarding chat app how can we maintain user's list and how can we send a message to specific person from user list?
Like this (On the server-side) Cheatsheet:
// sending to individual socketid (private message)
socket.to(<socketid>).emit('hey', 'I just met you');
The <> chatacters can be omitted, just make sure you replace socketid with whatever the socket ID truly is. This will sent a message to ONLY the socket id specified. I know this works because I use it.

How to send message after user's socket disconnect from the server

I'm using net module of node.js to build a chat server based on TCP. I have figured out how to handle the situation where two users both connect to the server. However, for a chat app, even if the user disconnect from the internet, people can still send message to those disconnected users. I just have no idea of how to achieve this.
You need to save messages in a database so that when they log in again you can retrieve sent messages and send them all at once.
There is no way to communicate to a user who has logged off. You just have to queue up the messages and deliver them when they reconnect.

Notifications about unread messages (node.js + sockets.io)

I have a system of personal messages between users on node.js sockets.io, is now faced with the problem that I do not understand how to make the notification about unread messages. Let's say, how to determine what a user who has received a message read this message, and how to make a notification message if the user is not in correspondence?
ps Chat History and active dialogs are stored in mysql.
Assume if the user has an open socket, and the message has been sent, then when the user clicks in a certain area of the page (perhaps the send message box?), an onClick event is fired to notify the server the user has received the message.
If the user is not on the webpage, there's not a way to contact them through sockets.io.

push notification like facebook

i want to build a push notifications service like facebook with nodejs and socketio. i have a question:
- how server can know what exactly client to send notification when many client connected to server?
Thanks for help!
Associate sockets with users when they connect, so when you have to notify user "Test" just look at the user.sockets[] list and send him whatever info you want to send.
In other words, Node.js doesn't have to "know" about users, you should take care of that and talk with Node.js in "socket" terms.
You can use socket.io's room feature. You can subscribe each client to its unique room and then emit an event in that room so that user will be the only one to get that.

Resources