Hosting Socket.io WebSocket Apps in IIS using Iisnode

Source: Internet
Author: User
Tags hosting

Transferred from: http://tomasz.janczuk.org/2013/01/hosting-socketio-websocket-apps-in-iis.html?utm_source=tuicool&utm_ Medium=referral

In this post I explain what to configure a Socket.io 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 Socket.io and Faye-websocket in IIS using Iisnode is a vailable Athttps://github.com/tjanczuk/dante.

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 https://github.com/tanczuk/dante.gitnpm installdante\setup.bat

Then navigate to the Socket.io, sample at Http://localhost/dante/server-socketio.js . You should see Dante's Divine comedy streamed to your over websockets from a Socket.io 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 Socket.io.js client library from the server, which are the second HTTP call. Next, the client side JavaScript on the page performs the Socket.io 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 socket.io 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 Socket.io must is adequately configured to that effect. In addition, IIS must is told which requests constitute socket.io 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://github.com/tjanczuk/dante.

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 Server-socket.io.js 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 ' Socket.io ' 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= "Socket.io"/>               <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 Socket.io on top of the raw HTTP Upgrade mechanism node. JS and Iisnode Support:

<enabled= "false"/>

The complete Web. configis at Https://github.com/tjanczuk/dante/blob/master/web.config .

The server

The server code must configure Socket.io 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, Socket.io traffic, the server normally listens to on The/socket.io path are going to arrive at/dante/ Socket.io:

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

Notice This configuration change in Socket.io 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 athttps://github.com/tjanczuk/dante/blob/master/server-socketio.js.

The client

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

var address = Window.location.protocol + '//' + window.location.host; var details = {    resource: (Window.location.pathname.split ('/'). Slice (0,-1). Join ('/') + '/ Socket.io '). 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 Socket.io 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 Socket.io ' s resource Property was set to ' Socket.io '. In the IIS hosted case, the original request is rendered Fromhttp://localhost/dante/server-socketio.js, and as a result t He Socket.io resource property is set to ' Dante/socket.io '. This allows the client code to being agnostic to how the server is hosted.

The full code of the client is athttps://github.com/tjanczuk/dante/blob/master/index-socketio.html.

enjoy!

Hosting Socket.io 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: info-contact@alibabacloud.com 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.