How to detect if a user is actually online?

I have a general question. Just need to be put on the right direction.

I made that little game I put on a server. Now I wish that 2 persons could play against each other online. So I made a signup/login and I can list all the users against which you would like to play. Now how could I know who is actually online and send him/her an alert when it is his/her turn to play?

My first idea was to send emails every time it’s a player’s turn to make a move; reload the updated page… But there must be something more elegant to do in-game?

I’m having a lot of fun coding that thing and any help would be great :slightly_smiling_face:

This is very hard to do depending on your webpage and server setup. Since Client-Sides do not keep active connections, meaning they can time-out and just pause if the user is not doing anything, you need to do this yourself.

One simple way is to make a JS timer that polls the server every 30 seconds or so and updates a field in the user database of the current time. But, you would also need to run some sort of process on the server to check these times to see if they are recent or not.

I will assume you already have a spot in your game where you want to check if the if the user is online or not. You might be able to do this with a HTTP header call, but, that is extremely complicated. easier to just keep a database field names something like “online” or “last_active” and fill it every 30 seconds with the current time. Then, check if it is recent or not in the game. Should work…

Thanks for the useful infos. Seems a good way to proceed; I know where to go now. :+1:

Sponsor our Newsletter | Privacy Policy | Terms of Service