title: Server Sent Events not sent ASP.Net 4
body: <p>My server-sent events are not sending anymore.  This use to work perfectly and now something has changed and it might be that I am running this as an ASP.Net 4 application.  I'm using a pretty standard iHTTPRequest Handler with a loop, a little threading timeout, and around I go, writing messages like this: </p>
  
  <p>A Message - Note:  the reason for parsing the message the way I do below, is I wasn't sure if there was a max message length, so in order to elminate this as a potential problem I broke it out.  Well forget this bit anyways, because I'm testing now with the most basic message "alive" so it never hits this.</p>
  
  <pre><code>public static void WriteClientMessage(this HttpResponse Response, string Message)
  {
      if (Message.Length &gt; 50)
      {
          int start = 0;
          int length = 50;
          while (length &gt; 0)
          {
              Response.Write("data: {0}\n", Message.Substring(start, length));
              start = start + length;
              length = Message.Substring(start, (Message.Substring(start).Length &gt;= length) ? length : Message.Substring(start).Length).Length;
          }
      }
      else
      {
          Response.Write("data: " + Message);
          Response.Write("\n");
      }
      Response.Write("\n");
      Response.Flush();
  }
  </code></pre>
  
  <p>An Event</p>
  
  <pre><code>public static void WriteClientEvent(this HttpResponse Response, string EventName)
  {
      Response.Write("event: {0}\n", EventName.TrimNewLine());
      Response.Write("data: \n\n");
      Response.Flush();
  }
  </code></pre>
  
  <p>Something else to note and I think this is really important is that <em>eventually</em> the message do come, but they come all at once, as if they are buffered.  So I've played with this, both enable and disable buffering.  I've even investigated to make sure that my dynamic content isn't being compressed, although I'm not confident I have this right, but pretty sure that it isn't.  I will investigate this further if someone says that for sure it is and this is my problem</p>
  
  <p>So what is it?  Is this a .Net 4ism?  </p>
  
  <p>**UPDATE - Insert Minimal Sample here - **</p>
  
  <p>I've added a sample of the script here:  <a href="http://live.meetscoresonline.com/test-sse.aspx" rel="nofollow">http://live.meetscoresonline.com/test-sse.aspx</a>
  This app loops 10 times and then breaks out of the loop.  This is the actual SSE service, so you should get the result directly to your browser, at least in chrome, FF tries to download the file, and haven't tested the other browsers.  What I expect to see is data: alive to apear in the browser as it happens, instead nothing appears until the script terminates and then all data: alive events appear.  I've tested SSE this way before so I know this works (or use to work).  Here is this test code.  Am I missing something?  </p>
  
  <p>UPDATE:  I commented out the break to demonstrate a new behavior.  If you wait about 20 or 30 loops, the real-time streaming begins and continues as expected.  So what is this initial pause?</p>
  
  <pre><code>public class TestSSE : IHttpHandler
  {
  
      public bool IsReusable
      {
          get { return true; }
      }
  
      public void ProcessRequest(HttpContext context)
      {
          context.Response.ContentType = "text/event-stream";
          var itt = 0; ;
          while (true)
          {
              if (!context.Response.IsClientConnected)
                  break;
  
  /* **** UPDATE:  REMOVED THE BREAK. STREAMING BEGINS AFTER ABOUT THE 20TH OR 30TH LOOP ****/
  
              // if (itt++ &gt; 10)
              //   break;
  
              context.Response.Write("data: alive {0}\n\n", DateTime.Now.ToLongTimeString());
              context.Response.Flush();
              System.Threading.Thread.Sleep(2000);
  
          }
      }
  }
  </code></pre>