How to send message after user's socket disconnect from the server - node.js

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.

Related

How does WhatsApp, send messages, so that i get messages instantly, when I am online?

I know how to send data to server, how to get data from server, but I don't understand how client gets messages when both of clients are online.
I want that the client doesn't ping the server every second, but clients to get messages instantly.
I will use React JS for the UI with node JS for the server part.
Imagine you have:
A central server
client1 (clt1) that sends the message
client2 (clt2) that receives the message
When only clt1 is online and it sends a message:
Store the message of clt1 in the server.
When clt2 reconnects, it sends a request to the server to get the messages.
When both are online:
clt1 sends a message to the server.
As the server knows that clt2 is online, it immediatly sends the message to clt2.
When only only clt2 is online:
clt2 sends a request to the server to know if messages have been send for him/her.
If yes, get those.
It works only if the server knows at any moment if the clients are online or not.
Hope it helps
EDIT: it can also work vice versa.

Show user status(online or offline) in my app

I have an education app that written by node.js and express.js
I want to show user status(online or offline) side profile photo
How can do it???
Using socket.io is a good choice here: https://socket.io.
Once a user logs in successfully, you can emit an event to broadcast to everybody else that he is online. Similarly, when the user logs out, you can emit another event to broadcast to everybody else that he went offline. (Also when he closes the browser without logging out properly).
Using socketio you can emit and listen to events between the client and the server. You can broadcast events to multiple clients, you can add clients to rooms and broadcast events specifically to the clients in the room, and do a whole lot of things!
Good luck.

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 chat notificattion to user offline/online by socket.io?

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.

How to send real time notification to single user using node.js and PHP

I am trying to integrate real time notifications with Node and socket.io in a Symfony Application. I have read a lot of information about this topic and have a working Node application.
nodeClient.js
var socket = io.connect( 'http://192.168.15.106:8080' );
$('a.sendSmile').click(function(){
socket.emit( 'message', { name: 'something' } );
});
socket.on('message', function(data){
console.log(data.name);
});
The problem now is with the above which is working perfectly I am able to send real time notification to all the users at once. But what's the best way to target a single user?
For example a user can send a smile to another user so only the second user should receive the notification and not all the users.
Should I make multiple listeners for node? or any other method to do this?
You need some way of identifying which socket that connected to your server is the one you want to send data to and then you can send to just that socket. You can keep track of user names when users connect or if you have some auth system, you can keep track of which socket belongs to which authenticated user.
Your server holds a list of connected sockets. Each connected one at a time and triggered a connection event on your server when they connected. Your application needs to create a way of knowing which of those connected sockets you want to send the data to. This is not something you've described anything about how you want that to work so we can't really help more specifically.
You can dispatch a notification to single user if you can discriminate that user. For example you can get a user_id on client connection to your nodejs server (the user_id is send from client, inside message) and save it in a key-value store (like Redis, memcache, ...). In this way you can correctly dispatch the notification, arrived from the server (like Symfony2 application), to right user.
I suggest you use Redis, both as a key-value store and for its implementation pattern of the publish/subscribe usable as a channel of communication between the server and the application of realtime.

Resources