goiisiis-expressdevopsevent-stream

How to config HTTPPlatformHandler of IIS for Server Sent Event (SSE, EventStream)


Currently I have program that provide SSE as a service, and I have to deploy on IIS. But its does not work correctly, Here is the result when I run .exe without IIS.

data: Hello, world

But when its run behind IIS, Browser was stuck on loading. I have to flush event Hello, world thousand times to make IIS flush result to browser and it's flush instantly instead of incremental update like SSE use to be.

Here is my web.config

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <handlers>
          <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
        </handlers>
        <httpPlatform processPath=".\sse_server.exe" 
          arguments="-port=%HTTP_PLATFORM_PORT% -environment development" 
          stdoutLogEnabled="false" 
          requestTimeout="00:05:00" 
          stdoutLogFile=".\sse_server_log">
        </httpPlatform>
        <urlCompression doStaticCompression="true" doDynamicCompression="false" />
        <caching enabled="false" enableKernelCache="false" />
      </system.webServer>
    </configuration>

Here is my go code

func SSEHello(rw http.ResponseWriter, flusher http.Flusher) {
    rw.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
    rw.Header().Set("Cache-Control", "no-cache")
    rw.Header().Set("Connection", "keep-alive")
    rw.WriteHeader(http.StatusOK)
    for i := 0; i < 1000; i++ {
        rw.Write([]byte("data:Hello, world\n\n"))
        flusher.Flush()
        time.Sleep(time.Millisecond * 100)
    }
}

Solution

  • Actually HttpPlatformHandler has 8kb output buffer , so my message is not sent out immediately.

    I have to change HttpPlatformHandler to ASP.NET Core Module, so web.config must update to this.

        <?xml version="1.0" encoding="utf-8"?>
        <configuration>
          <system.webServer>
            <handlers>
              <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath=".\sse_server.exe"  />
          </system.webServer>
        </configuration>
    

    And to start go 's application as aspNetCore on iis, the application need to get environment variable name ASPNETCORE_PORT then start http service on that port.

    port := os.Getenv("ASPNETCORE_PORT")
    http.ListenAndServe(":"+port, nil)
    

    That's all!