Hosting WebSocket Apps in IIS using Iisnode

Source: Internet
Author: User
Tags hosting

Transferred from: Medium=referral

In this post I explain what to configure a node. JS application to use the WebSockets when hosting it in IIS 8 usin G Iisnode. This complements a recent post in which I showed how to host node. js WebSocket applications in IIS on Windows using Iisnod E and Faye-websocket module.

The complete code of the sample for Self-hosting and IIS hosting of and Faye-websocket in IIS using Iisnode is a vailable At

From Zero to Divine in Seven Seconds

You need Windows 8 or Windows 8 and Iisnode installed.

Clone the Dante sample, install dependencies, and set up IIS virtual directory pointing to the code of the sample:

git clone installdante\setup.bat

Then navigate to the, sample at Http://localhost/dante/server-socketio.js . You should see Dante's Divine comedy streamed to your over websockets from a application hosted in IIS 8 using II Snode, one stanza every 2 seconds:

You can see four HTTP requests being made. The first one targets the node. JS application Server-socketio.js and returns the HTML page with the client side JavaScript. The page requests the client library from the server, which are the second HTTP call. Next, the client side JavaScript on the page performs the handshake (3rd HTTP requests). Finally, a WebSocket connection is established and the Dante's Divine Commedy is streamed from the server as discrete WebS Ocket messages over this single connection.

Under the hood

Hosting node. js apps in IIS using Iisnode requires some extra steps compared to self-hosting such apps. Unlike in a typical self-hosting case, node. JS apps hosted in a IIS virtual directory own only a subset of the URL space, And must is adequately configured to that effect. In addition, IIS must is told which requests constitute traffic and must be handled by Iisnode as opposed to oth ER built-in IIS handlers (e.g. static file handlers).

This explanation assumes the node. JS application is hosted in a IIS virtual directory named ' Dante ' as opposed to the root of an IIS website. The latter case was simpler to configure, with a required changes being the changes in Web. config described below.

Below is the key components of the configuration. Full source code of the sample is at Https://

Web. config

There is three aspects that must being configured in Web.config:handler registration, URL rewrite rules, and IIS wesocket m Odule Configuraiton.

First, you must inform IIS that the file is a node. js application and must be handled by Iisnode. Without this, IIS would try to serve the file as a client side JavaScript using the static file handler:

 <  handlers    >  <  add  name  = "Iisnode-socketio"   path  = "Server-socketio.js"   verb  = "*"   modules  = "Iisnode"  />  </ handlers  >  

Then, the URL rewrite module must is informed that all HTTP requests this start with the ' ' segment constitute no De.js traffic and should is redirected to the Server-socketio.js as the entry point of the node. JS application. Without this, IIS would attempt to map these requests to other handlers, and most likely respond with a failure code:

<rewrite>     <rules>          <Rulename= "LogFile"Patternsyntax= "ECMAScript">               <MatchURL= ""/>               <Actiontype= "Rewrite"URL= "Server-socketio.js"/>          </Rule>     </rules></rewrite>

Lastly, the built-in WebSocket module, IIS 8 ships with must is turned off, since otherwise it would conflict with the WebSocket implementation provided by on top of the raw HTTP Upgrade mechanism node. JS and Iisnode Support:

<enabled= "false"/>

The complete Web. configis at Https:// .

The server

The server code must configure to inform it is that the node. JS application owns just a subset of the URL space as A result of being hosted in IIS virtual directory. This means, traffic, the server normally listens to on The/ path are going to arrive at/dante/

Io.configure (function() {    io.set (' transports ', [' WebSocket ' ]);     if (Process.env.IISNODE_VERSION) {        io.set (' resource ', '/dante/ ');    }});

Notice This configuration change in was only made when the application was hosted in IIS; The same code base of the sample can also was self-hosted, in which case the configuration is the left unmodified.

The full code of the server is at

The client

The client code must contain configuration change corresponding to the server, otherwise client library would by Default assume the traffic should is sent to The/ path on the server:

var address = Window.location.protocol + '//' +; var details = {    resource: (Window.location.pathname.split ('/'). Slice (0,-1). Join ('/') + '/ '). SUBSTRING (1)}; var client = Io.connect (address, details);

Notice that this-client code works correctly both when the server was self-hosted or hosted in a IIS virtual directory. This was because the configuration sets the URL paths relative to the pathname of the original request that Rende Red the HTML page. In the self-hosted case, the original page is rendered fromhttp://localhost:8888/, and consequently ' s resource Property was set to ' '. In the IIS hosted case, the original request is rendered Fromhttp://localhost/dante/server-socketio.js, and as a result t He resource property is set to ' Dante/ '. This allows the client code to being agnostic to how the server is hosted.

The full code of the client is at


Hosting WebSocket Apps in IIS using Iisnode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.