1

I am trying to show the list of online users in my application. Let me explain my requirement. I have a Mysql DB table where list of username and their status mode(either 1 or 0) are storing. I have two php pages. One is for listing down all user's name and the status mode. Second page is for editing the mode of users from 1 to 0 and vice versa. Now I open these two pages from different system. If I change the status of one user(edit page) from one system then automatically it will reflect to the another system, where the listing page is opened, with the updated record and obviously this should happen before refresh the listing page. The same like gtalk chat users.

I am not asking the code, but please help me how to proceed to resolve the issue. Obviously, cronjob is one of the solutions, please provide another solution.

Thanks in advance.

Mausumi
  • 431
  • 2
  • 6
  • 20

2 Answers2

1

Well cronjobs are in fact not the thing you need.

With cronjobs you can schedule a task. What you want is client side refresh when new info is found. While cronjobs are server side and always on an interval.

What you need is polling or commet

The first, polling, you use your client side to execute a script every x seconds and look if there is new info (waste of resources in my opinion).

Commet, is now a days a better solution. But often hard to implement. I used pusher for this type of stuff. You can push messages to (all) clients connected and say there is new info. And then they will update or with the message comes also the new info

Community
  • 1
  • 1
MKroeders
  • 7,562
  • 4
  • 24
  • 39
  • 1
    Polling is what I was going to suggest. Javascript/AJAX to save the form onchange. Then, providing the record in JSON, JS/AJAX on system 2 can fetch and update the records, then sleep for n seconds. – Christian May 30 '13 at 06:28
  • 1
    True easy to implement, but when more users come not so fine any more. With the pusher solution you could push a notification it should update. This way users only update when needed – MKroeders May 30 '13 at 06:31
  • 1
    I agree, not sure how many concurrent users, or how many rows in the db. Pusher (or equiv) would be best option if lots of users. – Christian May 30 '13 at 06:43
  • never used pusher but sounds interesting. Is it any more resource-light than polling? Hard to find real-world info on their site. – Robert Seddon-Smith May 30 '13 at 07:02
  • 1
    Well yes, polling you check every x seconds if there is an update. With commet (I used pusher for the the easy use) you send to the client when an update is (PHP is not really good for commet because PHP can't put connections to the background). With pusher you can say like send update and to all the connected users there is send an notification – MKroeders May 30 '13 at 07:10
1

To achieve something like this, you should use JavaScript and Ajax in the clientside. Give the XMLHttpRequest a try. To make it easier you could use something like jQuery. On the serverside you could use json to transmit the data. Read the data from the table and put it into an array, let's call it users, the keys are the names and the values are their mode(1 or 0).

Then use json_encode(ARRAY):

//Echo the results in json format
echo(json_encode($users));

Let's say, the users 'Frank', 'Susan' and 'George' are online and 'Isabell' and 'John' are offline. Then the script would result in an output similar to this:

{"Frank":1,"Susan":1,"George":1,"Isabell":0,"John":0}

Of course you need to put this and the loading into another php-script, maybe refresh.php.

And, to read the data from the script, add some JavaScript to your view page. Use the XMLHttpRequest to request data from the script you just added. Or, if you use jQuery, you can simply use $.getJSON("NameOfTheScriptYouJustWrote") which returns an already parsed object.

Then use the returned data to update the list of users. And refresh it every 5-20 seconds.

And keep in mind that this is not an efficient way at all and that this will not work well if there are many clients using your service.

potato25
  • 186
  • 5
  • Sorry to vote you down, but thats a pretty pissy answer - you might want to provide more info. Some links, some code, anything. – Christian May 30 '13 at 06:41