0

I´m building an app where a check will be made every x number of seconds to see if a user is not reachable in order to determine if the user is no longer online. The chance of that the user is not going to close the app before disconnecting from the network, sending an "I´m offline" message is high, so this is why. The users are in a mySQL database, and I know there´s an event that can be fired there. Unfortunately my current hosting plan does not allow me to do this, so I´m wondering if there´s any point in setting it up in php by:

Making an infinite loop inside which is checked every x number of seconds for recent activity of users who are marked as online. Users automatically ping the server frequently, which will then store latest time of ping.

I got the feeling that this is not a good approach, and can the running loop actually just be stopped by calling it with sending some if value = blahblah then exit loop command?

Corey Hart
  • 173
  • 1
  • 2
  • 15
  • Are you running this loop in php or in javascript? How do you ping a user using php? – Jerodev Jan 26 '17 at 12:59
  • You should create a PHP script (or JS, or Python or whatever you want), but it shouldn't be an infinite loop. Instead, it would be better to use a cron and execute it every N minutes. – roberto06 Jan 26 '17 at 13:02
  • Why is it so important for you to know that a user has buzzed off? – RiggsFolly Jan 26 '17 at 13:03
  • Surely if the user closes the App you get an event of some sort saying I am closing. Can you not fire off a `ByeBye` messager to the server at that point – RiggsFolly Jan 26 '17 at 13:05
  • Jerodev: In php. I would not ping the user, but check the last time the user pinged the server which is showing in mysql. roberto06: Thanks, I will check if my host allows me to do this. RiggsFolly: For the other users to know that the user is offline. If the network is not reachable, there´s no way to transmit a byebye. – Corey Hart Jan 26 '17 at 13:12

1 Answers1

2

Two idea's, without involving loops ;)

  1. you could let the users send a ping via javascript (setInterval). This will send a request from the browser to the server. On the server a php script/route will just update a 'date_last_ping' on the user table. Whenever you query on the user table you can determine if they are online by checking how long ago the ping was.

  2. Use websockets, this could be a more scalable way to approach this

Wouter de Winter
  • 701
  • 7
  • 11
  • Hmmm, not such a bad idea actually. But wouldn´t it be alot of extra work for the server to not only query for the status cell but also check the latest "uptime" of the user cell, which needs to compared to current time? – Corey Hart Jan 26 '17 at 14:22
  • As long as the query can use an index it will be efficient. Use EXPLAIN to check if that's the case for mysql. And you could always use a cron job to update a dedicated status column, say every minute. – Wouter de Winter Jan 27 '17 at 14:48
  • So I´ve played around with this, where most recent time of usage - users check for new messages every 5 seconds and other stuff - is inserted for each user, to be used for determining timeout whenever another user has his 5 seconds event fired. So for this I´d be using TIMESTAMPDIFF to query on all active contacts of the logged in user, and set those of them who have a timeout to offline. I can´t get any result at all though with TIMESTAMPDIFF. Details here: http://stackoverflow.com/questions/41942025/php-mysql-trying-to-get-timestampdiff-to-output-a-result – Corey Hart Jan 31 '17 at 16:20