Using HTML5 Server-Sent Events with JSON and ASP.NET MVC

April 10, 2012

Using HTML5 Server-Sent Events with JSON and ASP.NET MVC

I’m getting ready for a session that I’m delivering at the end of this month. I wanted to create a sample for using HTML5 Server-Sent Events and decided to share that sample with my Blog’s readers. But before I write about the sample, lets get to know the Server-Sent Events HTML5 API.

Server-Sent Events

Server-Sent Events is an acronym to the HTML5 EventSource JavaScript API. The Server-Sent Events enable servers to push data over HTTP using push protocols. The data that is sent to the client-side must have the text/event-stream MIME type and must be sent in the following form:

data: This is the first message.

Pay attention that the format has to include the data: prefix and end with line break to end the message.

Another way to send data is using a dedicated event by using the event type format. The following example shows two different events that the client-side can listen for:

event: event1
data: event1 data
event: event2
data: event2 data

In order to listen to an event in the client-side all you have to do is to create an EventSource object and give it a URL to listen. Also, you need to wire the message event handler in order to handle the data arriving from the server. Here is a simple example of creating an EventSource object:

var source = new EventSource(url);
source.onmessage = function (e) {

The default event source is the message event as shown in the previous example. If you want to listen for a specific event type you will need to wire a handler for that event type like in the following example which listen on the event1 event:

var source = new EventSource(url);
source.addEventListener('event1', function (e) {
}, false);

Server-Sent Events Using JSON and ASP.NET MVC

After we learned what are Server-Sent Events lets dive into a much more robust example. In the example, I use ASP.NET MVC as my server side technology.

The Client-Side

We start in the client-side implementation. In the Index view of the Home controller I’ll add an messages unordered list and the script that creates the EventSource object.

    ViewBag.Title = "Home Page";
   2:     function contentLoaded() {
   3:         var source = new EventSource('home/message');
   4:         var ul = document.getElementById("messages");
   5:         source.onmessage = function (e) {
   6:             var li = document.createElement("li");
   7:             var returnedItem = JSON.parse(
   8:             li.textContent = returnedItem.message + ' ' + returnedItem.item;
   9:             ul.appendChild(li);
  10:         }
  11:     }
  13:     window.addEventListener("DOMContentLoaded", contentLoaded, false);


    To learn more about ASP.NET MVC visit <a href="" title="ASP.NET MVC Website"></a>.
<ul id="messages">

As you can see I use the DOMContentLoaded event to start the script when all the DOM content finished to load. I’m creating an EventSource with the name source to listen to the home/message endpoint. Since I expect that the data returned from the server is in JSON format, I parse it using the JSON.parse function and use its content to create a new list item.

The Server-Side

Writing the client-side was easy. The server side is also easy but there are some things to notice. First of all, I use a BlockingCollection object which is a wrapper on top of a IProducerConsumerCollection collection. Another object that I use is the JavaScriptSerializer that will serialize an object on the server to JSON representation.

Here is the HomeController code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Concurrent;
using System.Text;
using System.Threading;
using System.Web.Script.Serialization;
namespace ServerSentEvents.Controllers
  public class HomeController : Controller
    private static BlockingCollection<string> _data = new BlockingCollection<string>();
    static HomeController()
      for (int i = 0; i < 10; i++)
        _data.Add("item" + i.ToString());
    public ActionResult Index()
      ViewBag.Message = "Welcome to ASP.NET MVC!";
      return View();
    public ActionResult About()
      return View();
    public ActionResult Message() 
      var result = string.Empty;
      var sb = new StringBuilder();
      if (_data.TryTake(out result, TimeSpan.FromMilliseconds(1000)))
        JavaScriptSerializer ser = new JavaScriptSerializer();
        var serializedObject = ser.Serialize(new { item = result, message = "hello" });
        sb.AppendFormat("data: {0}\n\n", serializedObject);
      return Content(sb.ToString(), "text/event-stream");

Pay attention that in the Message action I’m returning the text/event-stream content type.

The following figure shows the output of running the above code:

Using HTML5 Server-Sent Events with JSON Objects


Server-Sent Events is a good mechanism to push data from the server to the client without the need server polling. The API handles only one side communication (server to client). If you want to use a bidirectional communication channel you should look at the HTML5 Web Sockets API. Using Server-Sent Events is appropriate for listening for changes in feeds such as stocks feed or RSS feed as long as they implement the push protocol.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. HaiderJune 6, 2012 ב 7:20

    It is possible to send data to specific clients

  2. Gil FinkJune 6, 2012 ב 9:23


    Server-sent events is a client-side JavaScript API. Sending messages to the client is the responsibility of the server-side and is implemented inside of it. If you want that the server will send messages only to a specific client, you will have to implement the behavior in the server.

  3. EddoJune 8, 2012 ב 8:02

    using these scripts, the last message which is sent to the client generates the following error:

    EventSource’s response has a MIME type (“text/plain”) that is not “text/event-stream”. Aborting the connection.

    which makes not possible to receive further messages from the server. Is there a way to fix this?

  4. LijoJuly 5, 2012 ב 8:05

    In this case Who is requesting?

    Here requset is initiating by client. right?

  5. Gil FinkJuly 8, 2012 ב 13:37

    @Lijo and @Eddo,
    Server-sent events are pushed from the server to the client. You wire a listener to an event source on the client side and that handler will listen to events coming from the server.

    Chrome is the only browser currently supporting them and it uses polling for that (instead of waiting for a push notification). This is the cause for the error you see @Eddo.

  6. Harvey TrianaJanuary 6, 2015 ב 23:05

    hi. it works for me, replacing:
    var source = new EventSource(‘@Href(“~/home/message”)’);
    instead of
    var source = new EventSource(‘home/message’);

  7. Harvey TrianaJanuary 7, 2015 ב 1:28

    it works due to reconnection and _data is static. Run each 3 seconds while page exists… To close connection can run something like:

    if (returnedItem.item.toString().indexOf(‘end’) != -1) {
    alert(‘Close Connection’);

  8. Zev SpitzFebruary 29, 2016 ב 19:01

    This is ignoring the point of server-side events, as the connection is closed once the Message method exits. (This can be seen by attaching a function to source.onerror; the function will be called with each closing and reopening of the connection.) Instead of returning the data, it needs to loop continuously and write the data to the Response object. See this —

  9. Pingback: ReBlogged: Jagan Peri : Low latency Notification Techniques. | Bharadwaj Turlapati

  10. Night OwlMarch 22, 2017 ב 7:49

    In my opinion, this is even worser than polling at regular intervals. This is because the push even occurs even when the list is empty. This will effect the performance of the application server. Better option is to use a comet based server just like facebook. It doesn’t push at regular intervals like this. It will push only when an event occurs.

  11. rachit guptaAugust 9, 2017 ב 21:17

    what you are doing is exactly similar to polling. You can check this in the network tab of your browser. You will have requests logged into it. While try the same for facebook. There will not be any request logs. And that is called server side events. This is just fooling the user.