Search code examples
htmlasp.net-mvc-4asp.net-web-apiserver-sent-events

WebAPI and HTML5 SSE


was trying to encapsulate a partial view to show feedback that i can push back to the client.

This Article shows a method of pushing back data using HTML5 Server-Sent events (SSE).

I noticed that if i opened up several browser tabs and then closed one i got exceptions as the logic didn't remove the respective stream from the ConcurrentQueue. I amended the code as below

    private static void TimerCallback(object state)
    {
        StreamWriter data;
        Random randNum = new Random();
        //      foreach (var data in _streammessage)
        for (int x = 0; x < _streammessage.Count; x++)
        {
            _streammessage.TryDequeue(out data);
            data.WriteLine("data:" + randNum.Next(30, 100) + "\n");
            try
            {
                data.Flush();
                _streammessage.Enqueue(data);
            }
            catch (Exception ex)
            {
                // dont re-add the stream as an error ocurred presumable the client has lost connection
            }
        }
        //To set timer with random interval
        _timer.Value.Change(TimeSpan.FromMilliseconds(randNum.Next(1, 3) * 500), TimeSpan.FromMilliseconds(-1));

    }

I also had to amend the OnStreamAvailable member as the framework syntax had changed to the second parameter being a HttpContent rather than HttpContentHeaders

 public static void OnStreamAvailable(Stream stream, HttpContent headers, TransportContext context)

The problem now is i am still getting inconsistant behaviour if i add or remove clients i.e it times out when trying to initialise a new client. Does anyone have any ideas or more examples of using SSE with WinAPI and the correct "framework of methods" to handle disconnected clients

Cheers Tim


Solution

  • This article is actually an adaptation of my original article from May - http://www.strathweb.com/2012/05/native-html5-push-notifications-with-asp-net-web-api-and-knockout-js/ (notice even variable names and port numbers are the same :-).

    It is a very valid point that you are raising, and detecting a broken connection is something that's not very easy with this setup. The main reason is that while ASP.NET (the host) allows you to check a broken connection, there is no notification mechanism between ASP.NET (host) and Web API informing about that.

    That is why in order to detect a broken connection (disconnected client) you should really try writing to the stream, and catch any error - this would mean the client has been disconnected.

    I asked the same question to Brad Wilson/Marcin Dobosz/Damien Edwards at aspconf, and Damien suggested using HttpContext.Current.Response.IsClientConnected - so basically bypassing Web API and obtaining the connectivity info from the underlying host directly (however there is still a race condition involved anyway). That is really .NET 4. He also pointed an interesting way in which this problem could be avoided in .NET 4.5 using an async cancellation token. Frankly, I have never got around to test it, but perhaps this is something you should explore.

    You can see their response to this problem in this video - http://channel9.msdn.com/Events/aspConf/aspConf/Ask-The-Experts - fast forward to 48:00