0

For my android app I'm looking for an elegant way of detecting client connectivity status. The server needs to know if client is online or offline.

I've read this answer,

... Server should send ping after some time interval to the device and device should reply. If device is not replying this means user is offline ...

but I feel constant pinging is lame and quite heavy on resources both client and server side. Is there a more elegant, preferably event driven, approach to detect if a client is offline?

While my situation applies to Android and a Google App Engine in java, I plan on extending to web and ios clients so a platform in depended solution is preferred.

Update: The same answer also mentions socket connections:

...Basically you need to create socket connection with server and exchange ping

To my knowledge, having an always on socket connection with the server is good for multi player gaming, but is it suitable for an always on, but only occasionally sending/receiving data like service?

Community
  • 1
  • 1
Tom Bevelander
  • 1,686
  • 1
  • 22
  • 31
  • "The server needs to know if client is online or offline" -- why? Perhaps the way to avoid the "constant pinging" is to avoid the need to determine client connectivity in the first place. – CommonsWare Jan 01 '15 at 15:46
  • I like your suggestion CommonsWare. Early in the design process this could help a lot. However, when established that the information is needed, this is not a solution. – Tom Bevelander Jan 02 '15 at 17:14

1 Answers1

5

Unfortunately, the pinging methodology is the best there is. The event-driven solution that you are proposing might seem elegant at the first glance, however, when theorizing further, it induces several problems. For instance, say your client suddenly looses all his batterypower and his phone suddenly dies. The client will therefore not be able to send a request to your server that he is no longer online. Simultaneosly, on the server-side, he is still flagged as "online".

The pinging mechanism alleviates this problem by proposing a premise namely "If a ping is not received in a timely manner -> flag the user as offline".

Good luck!

Ahmed Elmir
  • 163
  • 6
  • 1
    I'd add that instead of having the server ping client, it's better to have the client send a "heartbeat" to the server. – GreyBeardedGeek Jan 01 '15 at 16:04
  • @Ahmed Elmir as far as I know, when client device runs out of battery or for example, the app is "force-closed", all client sockets are explicitly closed and hence the server gets a specific "socket closed" message and might handle it appropriately – mangusta Jan 03 '15 at 21:33
  • another story is, if client goes too far from access point or 3G signal becomes too low etc., client sockets are not closed in this case and hence server is unaware of client being disconnected. right now I'm trying to deal with this problem – mangusta Jan 03 '15 at 21:36
  • if we assume some download server and server-to-client ping or alternatively client-to-server heartbeat, client needs to establish two consecutive connections with server, which means that the server needs to accept all incoming connections "in pairs", one for actual download, another for ping. – mangusta Jan 03 '15 at 22:05
  • @mangusta: What kind of socket connection are you talking about? – Tom Bevelander Jan 04 '15 at 09:52
  • @TomBrinkkemper just a usual TCP connection – mangusta Jan 04 '15 at 10:15