Tracking how many people are currently watching a video.

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 ?
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
If the video is a youtube video you can use the api to get accurate information
Rohit BajajAuthor Commented:
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
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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 FavorLinux/LXD/WordPress/Hosting SavantCommented:
Search - php pseudo streaming 206 - for a list of libraries for this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.