how to get user online status with socket.io & nodeJS immediately - node.js

socket.io has a setting 'close timeout' 60s and so there is a delay about 75 seconds from user closed the webpage to trigger the event 'disconnect'(so I know he was offline).It's too long for me..what I want is a very quick response to know a user ONLINE STATUS CHANGED.I use a interval ajax get method on front-end so I know wether a user is online and if timeout he if offline , but I think it's a awful design and there must be some way better then this,will you guys help me?thx very much

Which version of socket.io do you use ?
I never see disconnect event fired after more than 1s.

Related

Socket.io handle online/offline status

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.

Best way to detect a loss of connection to a server using Angular 4 and Nodejs

Essentially, I'm trying to work out the best way to ensure that a user is connected to the server / the internet and is thus able to make requests in my application without error.
I have come across various solutions, but I can;t really decide what is the best performing or useful.
Websockets, using Socket.io to keep an open connection with the server for each client. Also opens up the possibility for real time updates in my app, which could be a nice thing in the future. However, having lots of open sockets is sure to be hard hitting in performance.
Polling, so having an endpoint in my API that the angular app hits every 5 seconds or so to check the user is connected. Again, seems like it isn't a good idea to be hitting the server a load.
Waiting for an error, then start polling every couple of seconds to wait for the connection to be re-established. This is a little change on the above. However, you are still waiting for a user to fail, which isn't good for user experience.
Does anybody have any informed input on this issue?
Thanks

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.

Some kind of timeout proglem with node.js / socket.io chat

We've built simple node.js socket.io chat from an example chat document. Here is our problem that we've stucked with;
When A person clicks to B person for chat it says "it's ready start conversation"
When they send message nothing happens, hitting enter only clearing message neither A nor B person sees it.
But if they do f5 both it says "it's ready to start conversation" again and now they can chat without any problem.
With this first trigger every chat can be made without f5. For example after 1-5 hours if C person clicks to D person they can start conversation without problem.
But if a day passed without any conversation people that starts first conversation on that day has to make f5 to make some kind of trigger. Then anyone after them starts without any problem.
The server goes some kind of timeout or screensaver mode..! People on first conversation always have to make f5. Then it works for a day.. I am not sending any code or something we need guidance what to look for. Node.js? socket.io? What can cause this silly problem? Thx
But if a day passed without any conversation people that starts first conversation on that day has to make f5 to make some kind of trigger. Then anyone after them starts without any problem.
This sounds like you're trying to keep a websocket connection open for several days without any client doing any reconnect?
If this is the case, then maybe it is a clientside problem. Perhaps the browser closes the websocket-connection after a long time. Your web app doesnt recognize the lack of connection and still shows "it's ready to start conversation". But browser doesnt send any messages to the server anymore.
Check this thread: howto reestablish connection
It would help a lot, if you show us some code.
Best regards

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