Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Keith Monaghan
Keith Monaghan
9,494 Points

Display a List of Users who are Viewing a Specific Page Concurrently

I'm working on a family social network and I want to be able to display a list of which other family members are currently viewing the same page. When a family member navigates away from the page I want the page to reflect that.

Are there ways for doing this with PHP and/or JavaScript? I'm prototyping in CodeIgniter but I would be happy to dig into any implementation.

To be clear, this question does not deal with showing when a user is logged on. Rather when a user is actually viewing a specific page (after they have logged on).

3 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Something like this can be done. Let's say I am looking at a page and then you look it shortly afterwards.

(1) When I first make a request for the page is first made, the site could log in the database that I'm viewing the page. Then when you request the page, the site could show you that I was looking at it as the page loads. That's the easy part.

(2) You probably want it to update my browser to show me that you just joined the page. This would have to be done with JavaScript because you would want it to happen without me refreshing the page. JavaScript code would have to ask the server regularly if anyone new had joined the page.

(3) You won't always be able to track that someone left a page. You'll be able to do that easily if I go to another page on your site: you can assume I left the first one. But if I manually type in a different web address in my browser or if my laptop battery dies (or if I fall asleep at the keyboard!), nothing will tell your server that I'm not looking at the page any more. You could use the JavaScript request to the server for #2 above to also tell the server that I'm still on the page. The site would have to monitor regularly if my browser had checked in: if not, it would have to remove the fact that I was on the page after a while.

Yes, it's possible. That overview should describe the architecture you'd need in place to make it work. It's not a small task, but it's doable.

Keith Monaghan
Keith Monaghan
9,494 Points

Ok, I'm gonna recap so I can make sure I understand this.

  1. Record the timestamp for when a user visits a specific page.
  2. Record the timestamp for when a user leaves the page (discovering this through various means including JS window.onbeforeunload, session timeout or logout).
  3. The "Users Viewing Page" section would get updated automatically using AJAX to pull information from the database every...5 seconds or so.

What about setting the table so that it added or removed a user to a page based on whether they had requested the page or had vacated the page (through whatever means as explained in 3, above).

UserTracking_Table
    user_id
    page_id
    timestamp of last activity on that page

If a user had a record in that table than the app would show them as viewing the respective page.

Is there a better way to do this or should I dig in?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It sounds like that would work. Good luck!