0

i created a small CMS with php and i want to show logged in users if other users are online or offline.

Currently, i only create a query request but this will not update all the time. I want that user see the changes immediatley if something happend on the fly. I'm looking like a solution it's simular to facebook. If someone logged in, than the user is marked as offline.. if he come only, the user see it immediatley.

I'm sure, the best way is to use ajax but i don't know how is the best solution?

Should i create a site which only give a json back will all the status?

I need a little bit advise because i don't know what i have to google too.

Thanks a lot for help :).

  • It sounds like you want the server to send information to the page without requiring that the user refresh the page. If that's the case, the technology you want to research is "web sockets". – David Mar 05 '17 at 17:27
  • See [How to check if the user is online using javascript or any library?](http://stackoverflow.com/questions/29802403/how-to-check-if-the-user-is-online-using-javascript-or-any-library/29984356?s=1|0.8303#29984356) – guest271314 Mar 05 '17 at 17:58

4 Answers4

2

Thanks for the answers!

I found a solution that worked for me..

I create a "users.php" which output a json with all my user information.

via javascript i output the json objects into my html

function getAllUsers(){
  $.getJSON('online.php',function(data){
  $.each(data, function(i, data){
        if(data.user_status == 'online') {
          $("#online-user")
          .append("<tr><td>" + data.user_id + "</td><td>"
                  + data.user_name + "</td><td>"
                  + data.user_status + "</td></tr>)");
        }
   });
   })
}
1

Pooling using ajax requests will work for a few users, but as people use your CMS they will flood the webserver with many requests. You will then be forced to decrease the requests frequency or to implement something like gracefull degradation.

The right way to go is to implement a communication with websockets eventually with a follback on ajax long pooling. You could ckeck out socket.io or other web sockets libraries. Un fortunately php it's not websocket friendly, but that's not impossible.

Dan Ionescu
  • 3,135
  • 1
  • 12
  • 17
0

Does for your question is something like this enough?

http://www.plus2net.com/php_tutorial/whois-online.php

b2ok
  • 544
  • 6
  • 13
  • i created a php file which store all my necessary informations.. later in my javascript function i filter the online user.. i think it's better to do it this way and generate the user table with js so it update as soon some elements of the page refresh –  Mar 05 '17 at 20:34
0

From RFC2616 on HTTP/1.1:

HTTP is a generic, stateless, protocol...

So unfortunately, there's no way to get "real time" state information purely over HTTP, at least up until version 1.1. For this one would have to resolve to a stateful protocol like WebSockets or HTTP/2.

Sherif
  • 11,786
  • 3
  • 32
  • 57
  • sounds interesting as well.. i never worked with WebSockets so now the time is come! thanks for the link –  Mar 05 '17 at 20:35