IIS and Websockets

In IIS8 we introduced a new interface and associated API's to handle Websocket requests. IWebSocketContext.

Setup: To get this functionality working, Web Socket Protocol module should be enabled in the IIS features.

Runtime: If a incoming request is to be accepted as a Websocket request and subsequently upgraded, the handler must set the response status as 101. It should initiate a IHttpResponse->Flush, which will trigger the IIS Websocket module to do the necessary work to send out the 101 response to the client.

Once the response is sent, the handler can get a pointer to the IWebSocketContext through the IHttpContext3's GetNamedContext API.

The IWebSocketContext API exposes the necessary API's to read / write Websocket data.


Sample Code to get pointer to IWebSocketContext:

    // Get Pointer to the IHttpContext3

    hr = HttpGetExtendedInterface( g_pServerInfo, pHttpContext, &pHttpContext3 );
    if ( FAILED ( hr ) )
        goto Finished;

    // Get Pointer to IWebSocketContext

    _pWebSocketContext = (IWebSocketContext *)pHttpContext3->GetNamedContextContainer()->GetNamedContext("websockets");

    if ( _pWebSocketContext == NULL )
        goto Finished;




RFC http://www.rfc-editor.org/rfc/rfc6455.txt

IWebSocketContext:: http://msdn.microsoft.com/en-us/library/hh852804(v=vs.90).aspx

IHttpContext3: http://msdn.microsoft.com/en-us/library/hh852784(v=vs.90



  • Thanks for the sample code.  I'm adapting an existing IIS Native Module to handle the WebSocket protocol, but cannot find the essential header: Iiswebsocket.h.  I have the latest environment - Windows 8 professional (6.2.9200) and Visual Studio 2012 (v11.0.50727 with .NET Framework v4.5.50709).  I have downloaded both the Windows 8 and Windows 2012 SDKs but it doesn't appear to be included in these either.
    Any ideas?

  • Hi, Thanks for the nice article.
    I really want to implement native websocket module.
    Since I couldn't find that, I wrote "Iiswebsocket.h" by myself.
    I referrd to this document for that.
    Now I can compile it, but it seems to not work.
    On the same environment, managed version works fine.
    Are there any working examples ?

  • I'm working with Websockets and everything is working perfectly!
    But my server does not accept more than 200 simultaneous connections.
    There is a section in the web.config where I set it?
    Sorry for my English, I am using a translator.

  • @viniciussandin
    I would recommend enabling failed request tracing to see where the limit is coming from.
    AFAIK, by default, there is no concurrent connection limit of 200.

Comments have been disabled for this content.