Socket.io handle online/offline status - node.js

I've a real time chat application i've implemented it using socket.io, nodejs and angularJS but i'm having issues while handling the online/offline status. I'm not clear on how to know when the user is online or offline. What i thought is hitting a api in every 3 seconds and update the online/offline status in the database but i don't feel it's a good way so is there any better way to handle it?
Thanks in advance!
Here is the link to my backend code.
DesktopChat-Backend

You don't need to hit every 3 seconds , socket.io do this automatically, so you have just to handle it inside on. ('connection') and on.('disconnect'), so you get the user connected or disconnected then you broadcast the information to the clients, and update their status.
Look at this example :
Showing online users using Nodejs and Socket.io and angular js
One-to-One-Chat-using-Node.js-Socket-IO-Library

you can make map, where your userId will be associated with all socket.io client ids for this user. Then you can just look into this map, if specific user has some open connections.

Related

Getting realtime notification whenever a new friend request is received MERN Stack using Socket.io

I am trying to add a functionality in my web app where whenever a new friend request is received in the database (mongodb) then i get a notification through from backend (Node.js) to my frontend (React.js)
Now i researched about this functionality and get to know about socket.io but the problem is the solutions i found which were using socket.io were kind of a brute force according to me ,
In those solutions they were querying the database inside the socket.emit(),
Now according to me if I keep querying the database every 4-5 seconds is it a good approach to do that doesn't it put load on database?
What is the right way to do this?
What i have tried so far is finding a better solution than querying the database again and again till i get an update. But i had no luck ..
The best approach is to connect frontend with backend using websocket/socket.io and as soon as you add a new object the server should push the data to frontend. You don't have to run a database query every 4-5 second. Write a server push event in your data.save() function. So as soon as you create a new object, the backend sends data to frontend.

How to implement Laravel, node.js, socket.io and redis for creating realtime chat/notifications with database

I spend already 3 days for reading, watching tutorials about WebSockets, socket.io, node.js and so on.
Basically, I'm a Laravel developer and have just a basic idea about all the rest components.
With regret, after these 3 days I don't have in mind all logic step-by-step of implementing this architecture. I will try to explain what did I understand and you please correct me.
So :
WebSockets - is a bidirectional continuous connection between client and server. It uses another port, and basically it is not a HTTP/S connection.
For making this kind of app like I said, we need one more server, and idk why, but this is Node.js. At this Node.js server we should install socket.io (server-side package) and Redis.
Then, we need to add client-side socket.io (via CDN probably).
At Node.js server we are creating a server.js file where require all modules that we need, like socket.io and Redis. We open a connection for a specific non-used port (as 6001). Then we run this node server.
At front-end we are subscribe-ing to this channel and define method for emitting and listening to the server.
Example :
User1 is connecting to a specific route. User2 as well. User1 type a message for User2, when press Submit, message from User1 is sent to Node.js server, where it is sent in Redis(yes? if yes - Why?), and then Node.js is listening for what to do in this case, and send this message to specific user, or with broadcast to all users except the publisher.
Oh, it's even hard to explain that, too much steps and tehcnologies used.
Can please someone correct my logic? I really want to understand all that process and logic of using this components. Or please, give me some useful articles and videos, may be I didn't saw them. Thanks!
I suggest you read the official docs on how to build chat. Basically what you will have in the end is 2 servers, 1 for your Laravel app and the other for chat (Socket.io) . The key to this is using broadcasters and listening for events on both sides, frontend and backend.
Events are broadcast over "channels", which may be specified as public or private. Any visitor to your application may subscribe to a public channel without any authentication or authorization; however, in order to subscribe to a private channel, a user must be authenticated and authorized to listen on that channel.

Tell server that user is no more on internet

I am new to node.js and trying to develop group chat using node.js and socket.io. I am able to do group chat also able to manage data in the system.
Now, the problem with me is with offline users i.e not connected to internet.
I am having connection stream of this user and as if I do receiverUserSocket.emit("sendMsg",data) there is no way to verify if this user received message or not.
Yes, there is an event .on("disconnect") but I am getting delay of approx 30 - 40 seconds.
Is there any way we can identify that the user to whom we want to send message is online or offline.
The best way is to use socket.io heartbeats - assuming you're using socket.io.
Heartbeats and the problem with detecting a terminated connection are well explained here:
Advantage/disadvantage of using socketio heartbeats
Keep in mind that you can control heartbeat timeout values (thus forcing the disconnect event to appear much faster), as explained here:
Controlling the heartbeat timeout from the client in socket.io
But that might put much more strain on your server.

Switching from stored messages to real time chatting in node and express

I am new to server-side development. I'm trying to learn by doing so I'm building an application with express on the server, mongodb as my database and angularjs with twitter bootstrap on the client-side.
I dont know if this is the most practical way but when thinking about how to implement messaging between users I thought of a mongodb model called Conversation with an id and an array of the ids of every user in the conversation and another array of strings that correspond to messages. And then add this model to my REST API.
But lets say all/some of the users in the conversation are online, why not benefit from socket.io. So how can i switch from this to real time chat? Does the interaction with mongodb occure exactly as explained and socket.io just notifies every online user that an interaction has occured? If yes, how? Or is it something else?
socket.io can send real time events to connected sockets, you can use a database for storing messages that are failed to deliver and for offline users.
also, you might want to use something like Redis for this as it has channels with subscribe and publish capabilities.

Notifying a browser about events on server

I have a java based web application(struts 1.2). I have a requirement to display a status on the frontend (jsp). Now the status might change which my server gets notified by another server. But I want this status change to be notified to the browser.
I don't want to make a refresh at intervals. Rather I have to implement something like done in gmail chat, ie. the browser gets notified by changing events on the server.
Any ideas on how to go about this?
I was thinking on lines of opening a request to server for status, and at the server end I would hold the request and wouldn't respond back until there is a status change. Any pointers, examples on this?
Best possible solution will be to make use of XMPP protocol. It's standardized and a lot of open source solutions will get you started within minutes. You can use combination of Smack, StropheJS and Openfire to get your java based app work as desired.
There's a method called Long Polling (Comet). It basically sends a request to the server. The request thread created on the server simply waits for new data for the user, with a time limit of maybe 1 minute or more. When new data is available it is returned.
The main problem is to tackle the server-side issue, you don't want to have one thread for every user just waiting for new data. Of course you could use some asynchronous methods depending on your back-end.
Ref: http://en.wikipedia.org/wiki/Push_technology
Alternative way would be to use WebSockets. The problem is that it's not supported by all browsers today.

Resources