Tracking how many people are currently watching a video.

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
The problem is to design a solution for finding the number of users who are watching a particular video eg. on youtube etc.
Here is what i thought about solving it.
If the video is playing than every 5th minute from GMT it will send a play event
I will maintain a table containing  (userId, movieId, timeStamp)
So to count how many viewers playing a video i will simply have to count the number of entries in the table with the movieID and timeStamp not later than 5 minutes. Because if a play event is not recieved for 6 minutes than the video must not be playing...
And each time a play event is received the timeStamp will be updated to the current time.

Also i will run a cronJob that will be clearing entries that are not getting updated.
Will maintain a cache containing the count for each movie and will update it at a regular interval from the table.

Each video that is being played will send a https request for the number of current viewers every 6th minute and the number of viewers will be updated.
Please suggest if there is any flaw in this approach. or any better approach ?
Any pros and cons ?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

If the video is a youtube video you can use the api to get accurate information


No its not about using an api.
The question is for testing ones ability to design such a thing.

Its a challenge question. Nothing to do with any website in particular
Developer & EE Moderator
Fellow 2018
Most Valuable Expert 2013
Ok, you said YouTube.  To me that means tracking something you don't have access to. If the goal is to track videos from YouTube or Vimeo, then you will want to take advantage of their api's.

If you are talking about videos you host, that is something different.  In that case, use a video player that has analytics built in such as flowplayer or you can use Google Analytics too.

If you mean starting from complete scratch, then you will want to do this all client side and send ajax requests to your db.  Start with   Assuming every video starts and plays straight through until the end, you can use the Play and Ended methods are called where you send an ajax request with the timestamp, session id (or user id if they are logged in) and action (Play, Ended etc).

You will want to consider the definition of what is a view and viewer.   In other words, if a user starts the video and replays it 10 times in x minutes, do you count that as 10 views?  Or do you have a rule in place that if a video starts x times in x amount of minutes it is counted as one view?  

Next, take into consideration pausing, scrubbing ahead (seek).

The key is to do all of this on the client side and update your database via ajax requests.  If a row of data is missing something, don't delete it because that will be a clue for an issue.

To get the count of current people watching a video, you can run a query of the db that shows videos that have started (Play) and not Ended or perhaps use Progress.
David FavorFractional CTO
Distinguished Expert 2018
Pull one of the many PHP pseudo streaming libraries off GitHub.

If you serve the video yourself, you can simply setup Apache logging to log every 206 request, so you'll know...

1) To the second how far people have gotten in your videos.

2) When people leave the page, fire some Javascript to either tell you via AJAX or by another Apache request a person has left the page.

This provides you with exact start + stop time of every video, so you can accurately count current active viewers.
David FavorFractional CTO
Distinguished Expert 2018

Search - php pseudo streaming 206 - for a list of libraries for this.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial