aspose file tools*
The moose likes Java in General and the fly likes Online database needs to keep track of who is online periodically, how? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Online database needs to keep track of who is online periodically, how?" Watch "Online database needs to keep track of who is online periodically, how?" New topic
Author

Online database needs to keep track of who is online periodically, how?

Tarek Khojah
Ranch Hand

Joined: Jul 21, 2007
Posts: 58
Here's the setup, please stay with me

Users log into a server which stores their information (IP address and UDP port), the connection is then closed.

Whenever someone logs in, he's added to the table of "Online_Users".
When a person wants to log out, he'll connect to the server to request logging out.

But what if a client isn't really available anymore and failed to notify the server (Because his connection failed or OS crashed? He'll remain in the table "forever", so I need a method to periodically check who's information is REALLY up-to-date.

Here's the problem, there's no permanent connection between the client and the server!

There are two approaches I can think of:

The server could poll all the "Online_Users" periodically, deleting the clients who don't reply.

Each client can alert the server periodically to avoid being deleted from the table.

So, first question, what's the preferred method to do this? TCP or UDP? And who should take the first step, the server or the client?
Please note that I'm keeping the UDP port exclusively for communication between clients, so I'd rather not use it to be contacted from the server.

I don't really need an answer (though I'd love one ), I just need to know the pros and cons of each approach, and what's usually the protocol in these types of applications.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

1) Whenever anybody hits the server, their "online" record is inserted/updated in the database with a timestamp.

2) When someone explicitly logs out, their record is removed.

3) Every X minutes, run a query against the database which deletes records more than Y minutes old (choose X and Y by considering the tradeoff between accuracy and performance.)


[Jess in Action][AskingGoodQuestions]
Tarek Khojah
Ranch Hand

Joined: Jul 21, 2007
Posts: 58
But what if someone logs into the server and doesn't hit it again?

It's a voice-chat application, which is why the user submits his current contact information in the database.

So someone is expecting a call, logs in, and waits for other users to look him up and contact him.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You've laid out the options pretty well.

0) Do nothing. A client may appear to be logged on forever. How bad is that?

1) Client sends a keep-alive every n seconds. Any client that hasn't sent a keep-alive in n seconds is presumed dead.

2) The server polls all the clients. Any client that doesn't answer (maybe twice) is presumed dead.

3) The user has to do something to stay alive. If they do nothing after n seconds, the session is dead but we don't know if the client program is still alive. If the user does something after that, they'll have to log in again.

1 & 2 both work and may be necessary if you want fine grained information about how long people use the application. They can introduce lots of tiny network messages and some server overhead, maybe a problem, maybe not. Most web apps I use work like #3.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
 
subject: Online database needs to keep track of who is online periodically, how?