Saturday, March 12, 2011

Facebook type getting a list of all logged in active users in realtime

Hi,

If you have used social networks such as facebook. They have a feature of showing all your friends who are online. I have made a custom code to achieve the same. But in my case instead of seeing online friends, I am checking for all online users. But of course you can change that logic to serve your purpose. You will find the code at the bottom of this page, but first let me explain what is happening.

By default i have created the following users:


To demonstrate this, I need to open atleast 2 different browsers. I am using Chrome for hasan/hasan login to see what happens:
In this the user sees 3 inactive users. Now I will login using user/user credentials in Safari. Following is what happened without page refresh in the above screen:
Now if the second user instead of logout, just closes the window, or tab then again in few seconds the first user will get the following:

"Sam Miller", has not logged out but has closed its window, so making him an inactive user. Again when he visits the URL without logging in, since he was never logged out in the first place. The first user will again see the following:

The entire process is not instant, which means it takes few seconds every time its checking for active user. This is necessary, to make the system accurate. This is a basic code. You can change it according to your need. Click here to download the code. This code also includes auto logout mechanism similiar to Gmail. To see details of that click here. The database schema has been included in the package.

If you have queries, most in your comments. I would be more than happy to reply.


No comments:

Post a Comment