Welcome Guest, Not a member yet? Register   Sign In
How to know when user is "online"
#1

[eluser]robnecciai[/eluser]
I'm writing a membership-based web site using CI. I need to be able to show a list of all members who are "online" - that is, all members who are currently logged in. Easy enough to put a field in the database, however, most users won't log out so I'll have no way to update the field to show that the user is not logged in.

I'm using ci sessions but, I'm not sure that making the session expiration value very short is the right answer.

1. Is there any (straight-forward) way to detect when a user has closed the browser or navigated away from the site?
2. Has anyone done this with a small piece of Ajax code, perhaps, (on every page) letting the server know the "client" is alive?

Thoughts?

Thanks.
#2

[eluser]BrianJM[/eluser]
Here's a little jQuery snippet that pings a controller named in every 5 minutes. Include this on every page.

Your would need to code your controller/model to do the appropriate processing.

Code:
var timerId, timerInProgress, ping, ms;

timerInProgress = false;

$(function() {
    // AJAX defaults
    $.ajaxSetup({
        cache        : false,
        dataType     : 'text',
        timeout      : '15000',
        type         : 'POST'
    });
    
    ms =  300000;    // in milliseconds
    
    /**
     * Check session is valid. Will not run if another check
     * is in progress.
     */
    ping = function() {
        if (timerInProgress === false) {
            timerInProgress = true;
            
            $.ajax({
                url      : 'in',
                complete: function() {
                    timerInProgress = false;
                }
            });
        }
    };
    
    // Run session check every x milliseconds.
    timerId = setInterval(ping, ms);
});
#3

[eluser]AlexJ[/eluser]
Use the "Saving Session data to a Database" (you will find it in the user guide), and check the last_activity field, set the sess_time_to_update to about 120 seconds and find out a good tresshold to give a user the "is online mark"
#4

[eluser]robnecciai[/eluser]
Thanks for the replies. I'm going to try both methods that were suggested to see what I can come up with.

Brian:
That's exactly what I was hoping/looking for. Do you happen to know if this is particularly taxing on the connection and/or the server? I would think that the overhead is negligible.

Alex:
I thought about doing something like this but, in the "server" code (possibly a cron script), I would have to decrypt the session data (to get the user id) for all sessions who's last_update is > 120, then update a user table with the online/offline status. Is that correct? As far as I can tell, there's no other way to relate a user id/member id to a session without looking at the stored session data.

Thanks again.
#5

[eluser]AlexJ[/eluser]
No there is indeed no direct way to couple a user to a session, but you can make a MY_Session class that overwrites the default Session behaviour and adds a column user_id to the session table, then you can overwrite the set_userdata method so it saves the user_id with the session.
#6

[eluser]BrianJM[/eluser]
[quote author="robnecciai" date="1296250938"]
Brian:
That's exactly what I was hoping/looking for. Do you happen to know if this is particularly taxing on the connection and/or the server? I would think that the overhead is negligible.
[/quote]

In my experience the load is negligible.

The possibility of creating a high degree of server load depends greatly on the number of users simultaneously connected (pinging the server), the frequency of the ping, and the efficiency of the model that manipulates the underlying records.

Including the response and request headers, we are talking about less than 1kb of round trip data transfer (per ping). So if you had 100 simultaneous users 24 hours per day, and each user pinged the server every minute, you would accumulate less than 43 MB/month round trip bandwidth ((60*24*30) / 1024). Every minute may be a little excessive, but you get an idea of the numbers involved.




Theme © iAndrew 2016 - Forum software by © MyBB