User counter

May 25, 2011 at 1:53 AM

Looking for any hint someone can provide for:

Displaying a user counter on the player, meaning how many users are currently watching the stream. If someone has already done complete solution and happy to share, that would be great. If not, can anyone provide a hint?



May 25, 2011 at 7:30 AM


You would to store this on a server somewhere, and store the count in a database. E.g:

The player calls with a GUID generated in the player every 20 seconds

activePlayer.aspx then adds or updates a database row with the GUID from the player and a timestamp

To get the active count of users the player then calls which returns the number

Behind the scenes activePlayers.aspx does a database lookup of all rows with a unique GUID with timestamps less than 20 seconds old.

That would provide an almost accurate number of current users in the player

- Loke

May 26, 2011 at 4:09 PM

Thanks for the note above. Didn't know what exactly you are stating above in terms of the flow. We have done following.

OnMediaOpened - Added a counter in the database against the stream event ID

OnMediaEnded - Reduced the counter in the database against the stream event ID

Do you see any drawback of using this?


May 27, 2011 at 8:02 AM

Well, you could indeed add a counter for OnMediaOpened.

But with regards to OnMediaEnded, users might terminate the application before OnMediaEnded occurs, so you would need a timeout for the OnMediaOpened counts, so they are removed after some time, if not closed.

- Loke

May 29, 2011 at 2:48 PM

I do some similar processing in my use of SMF. Loke is right about problems using OnMediaEnded because most people do not wait until the end of the video to leave the page. Some may start the video and while it's playing, close the browser. Others may just use the back button of the browser to leave the page before the video has been completed. You can handle this using javascript. The technique also uses the javascript bridge for the SMF player. The key is to catch the "beforeunload" event from the browser and send off a call to your web application that your user is no longer watching the video. In the page that hosts your silverlight player, add the following javascript then I will explain:

var unloaded = false;

function PostStopMedia(id, user) {
    try {
            type: "POST",
            async: false,
            url: "@Url.Action("RecordStopMedia", "Video")",
            data: { id: id, user: user }
    } catch (e) { }}

function GetVideoInformation() {
    var control = $('#SilverlightPlugin').get(0);
    if (control != null) {
        try {
        } catch (e) { } }}

$(document).ready(function () {
    $(window).bind("beforeunload", function () {
        if (!unloaded) {
            unloaded = true;


Starting at the bottom in the document ready you can see I bind to the window's "beforeunload" function and ultimately call GetVideoInformation. GetVideoInformation is just above document ready and goes out to the SilverlightPlugin that is on the page and calls GetVideoInformation() on the player. You can find out more on how to do this by looking at the javascript bridge examples on this site. The GetVideoInformation() call on the Silverlight Player (not shown here) sends a call back to javacsript with any information you need to tell your application that the user has stopped watching. In this case I want to know what Id the video was and what user was watching it so that I can say that user is no longer watching the video. The Silverlight method gathers that information and sends a call to the javascript function on the page called PostVideoInformation (shown above). This javascript method creates a synchronous call to your web application sending the information you need to add/remove the current user from watching the current video. My sample is from an ASP.NET MVC 3 application.

It is important that you indicate the POST is "async: false" because if the user closed the browser and sends an async call that call will never complete because the host is gone. You may also be wondering what the "unloaded" variable is. From my experience using "beforeunload" the method is called twice in some browsers for whatever reason, but you only want to do the processing once. The boolean value indicating you have already done the "beforeunload" processing does the trick.

Jun 2, 2011 at 6:41 PM

Thoughts from both of you are really helpful. After looking through more options, here is something that i have been thinking about:

During the live broadcast of an event, IIS shows the number of concurrent requests per stream (meaning if you have three streams, it shows how much concurrent requests are coming in, including audio requests).

Is there a way that this being exposed somehow through an API and can be used from the SMF Player ?, as this will give an exact count as determined by the IIS Media plugin sitting on the server.



Jun 2, 2011 at 9:24 PM

Yes, you can definitely get the requests pr second, and use that.

Here's a wscript sample for getting that information:

And heres a sample of using the RSCA API in C#

- Loke

Jun 4, 2011 at 1:06 AM

Thank you both for sharing the ideas.