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?

5
  • Are you running this loop in php or in javascript? How do you ping a user using php? Commented Jan 26, 2017 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. Commented Jan 26, 2017 at 13:02
  • Why is it so important for you to know that a user has buzzed off? Commented Jan 26, 2017 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 Commented Jan 26, 2017 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. Commented Jan 26, 2017 at 13:12

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
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: stackoverflow.com/questions/41942025/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.