php - Detect Live Server Side changes (Long Polling, Short Polling or Web Sockets) -
justification , research
i have site requires users login in order view. whilst users logged in, keep eye on user session. this, mean know whether or not user session has expired, , therefore redirect them.
each user's session lasts 1 hour (or whatever set to), , reset if visit different page (like login systems).
at present, have following algorithm:
- user arrives @ private page (javascript method called
isuserauthorized()
executed) - the
isuserauthorized()
javascript method makes ajax request page 'ajax.example.net/authorized' - this page returns json object indicating current status of user so:
{ authorized: true, timeout: 3600000 }
- the javascript method sets timeout call method again in
timeout
milliseconds, assuming session have ended then. - if session has ended redirect user, otherwise recall in method in
timeout
milliseconds.
there 2 reasons not current method:
- i have had issues time syncing between client , server clocks, weird causes issue...
- it leaves timeout in background of webpage, , site javascript heavy, rather not have additional timeout in order keep site smooth possible.
my question
my question therefore, can think of better way achieve this? have thought of long polling or websockets, not 100% sure how use either of these , tutorials on websockets found not good! these better solution?
i workaround time syncing issues before do, want ensure there not better ways achieve this...
in case helps, here current code:
// set authorized timeout mn.authorizedtimeout = settimeout(function(){mn.isuserauthorized}); /** * user authorized * checks see if current user authorized , * makes sure session still active */ mn.isuserauthorized = isuserauthorized; function isuserauthorized(){ // temporary console.log('authorising'); // set authorized var var authorized = false; // clear current timeout cleartimeout(mn.authorizedtimeout); // send request determine whether user authorized $.ajax({ url: "//ajax.example.net/authorized", type: "get", datatype: "json", cache: false, async: false, success: function(data){ console.log(data); if(data.authorized){ // if user authorized check again in timeout milliseconds mn.authorizedtimeout = settimeout(mn.isuserauthorized,data.timeout_milliseconds); // set authorized true authorized = true; }else{ // if session has expired proceed informing user mn.usersessionexpired(); // set authorized false authorized = false; } } }); // return session status boolean return authorized; }
updated answer:
nevertheless i'd consider better practice calculate online status serverside. can make sure there's no inconsistency time. have servertime.
for getting online status can go long polling approach. i've made example:
(function checkloginstatus(){ $.ajax({ type: 'post', url: 'loginstatus.php', data: {userid : 25}, success: function(data){ if(data.logged_in !== true){ //log user out } }, datatype: "json", complete: checkloginstatus, timeout: 15000 }); })();
this make sure new request made when 15 seconds passed , when request complete.
old answer:
if concern watch on logged in users don't need poll. i'd keep whole thing serverside. add "last_active" field users table.
whenever user interacts (visits site) update timestamp current timestamp.
to detect whether user online take current timestamp , subtract "last_active" timestamp it. if difference bigger 1 hour, know user inactive.
that's how handle it. more efficient (regarding ressources) doing ajax.
Comments
Post a Comment