IIS Node on Arvixe Windows Server with Socket.io Tutorial

Okay guys, time for the Moment of truth.

I have been trying to get this working for over a week now, and I am super stoked to finally have gotten it working.

TABLE OF CONTENTS

What is Node? - Introduction

Step 1 - Installing Node.js and IIS Node on your local computer for development and uploading your application to Arvixe

Step 2 - Using Node Packaged Modules manager, Downloading Modules, and Fixing Socket.IO

Step 3 - Using Socket.IO on Arvixe (without Node.exe)

WHAT IS NODE?

Node is an “event-driven I/O server-side JavaScript environment based on V8.”

It is one of the newest additions to programming, but it is not a new language.  Rather, it is a new spin on the existing language: Javascript.

Javascript is a loosely typed language (like PHP), which means that Server-side scripts written for Node can be very flexible.  In addition, web browsers use Javascript fluently, so it is very easy for data to be communicated between the browser and the server.

Node is especially useful when creating applications requiring “real-time” client-server communication.  Some examples of these applications include chatrooms and multiplayer games.

LET’S GET STARTED

This tutorial is written for those of you with Windows Hosting Accounts.

  • IIS Node must be run in a 64 bit app pool.  If your app pool is running at 32 bits, you will need to submit a ticket to http://support.arvixe.com and ask the support team to change the settings on your app pool.
  • After your app pool has been changed to 64 bits, you will not be able to change the ASP.Net version of your website.  Doing so will revert it back to 32 bits.  Make any changes before submitting your ticket.

SOURCE CODE

Prebuilt modules for Step Two, and Source code for Step Three may be downloaded here:  http://code.snyco.net

Prebuilt modules for step two include:  Socket.IO and Express

Optional modules include: jQuery, and jQuery dependencies

If you download the prebuilt modules, you may skip step two.  You will not need to install Python, and you will not need to install Visual C++

REQUIREMENTS:

  • Windows Computer to use as a Development Environment
  • IIS – Internet Information Services – Your built in Windows Server.  On a PC, it is used mostly for development purposes.  Try to use IIS 8 if possible.
  • Python 2.7.3 (do not install 3.x.x) – Python is a programming language, much like Perl and PHP.  The framework is relatively small and installation is quick (So is uninstallation). It is required by Node-gyp, Node Package Modules manager’s Build tool.  - note – Node-gyp is used to build Node Modules downloaded via NPM.  Node-gyp is required for Socket.IO and several other modules.  It is not required to run Node.
  • Microsoft Visual C++ or Visual Studio.  Visual Studio’s VCBuild is required to build Socket.IO.  -note, Visual Studio is not required to run Node, nor to create node applications.  It is a very powerful development environment, but be aware that it is a large program.  Visual C++ is much smaller, but still quite large.

STEP ONE
Installing Node.js, IIS Node  on your local computer for development

1.  If you have all of your required Software, then it is time to install Node and IISNode.

  1. First, Install Node. http://nodejs.org/download/
  2. Second, Install iisnode.  https://github.com/tjanczuk/iisnode
    Though there are downloads for iis through github, the easiest way to install iisnode on IIS is to use IIS’ Web Platform Installer.

2. Installation of both programs is relatively quick and painless.  Now, we will test out the node environment on IIS.  Open IIS.  IIS can be opened by going to the command line, the run line, or by using the start menu’s search bar.  Simply type:  inetmgr or inetmgr.exe.   It is a good idea to make a desktop shortcut.

3.  Next, we will add a new website for your node app.  Right Click sites and select Add Website.  Then create a name, and select the path.  IIS’ default website is in C:\inetpub, so it is a good idea to put your websites in this directory.  Finally, select a port.  80 is the default port, but I like to leave port 80 free, so you may choose a different port.

iis_add_website

iis_add_website_2

4.  To test out Node on this website, we will use IIS’ official IIS Node samples included in the installation directory.  This directory is normally located in C:\Program Files\iisnode\www

Copy the contents of this directory to the root of your created website, in my case:

C:\inetpub\myNode

5.  Your website folder should now look like this:

  • configuration
  • defaultdocument
  • express
  • helloworld
  • loggin
  • urlrewrire
  • index.html

6.  Now, go to the helloworld subdirectory and open the web.config.  This is all you need in order to open the hello world example.

helloconfig

7.  Open up hello.js in the helloworld subdirectory

helloworld

8. For the moment of truth, open up your browser and go the website.

http://localhost:[portnumber]

HelloworldMozilla

9.  At this point, if your app pool is running at 64 bits, you should be able to upload this example to your Arvixe site.

Errors 

500.19 – This error means there is a problem with your web.config file.

500.21 – This error means there is a bad module, and most likely means you are using a 32 bit app pool.

STEP TWO
Using Node Package Modules manager, Downloading Modules, and Fixing Socket.IO

1.  Modules are addons, or plugins, for Node.js applications.  There are hundreds of plugins available here:

https://npmjs.org/

However, the quickest way to install a module is to use Node.exe’s built in package manager, NPM.

NPM can be run from the command line.

2.  Npm installs modules to the Current Directory.  It does not install modules to the Node Installation Folder.  And if you install a node_module to the Node Installation folder, your website in C:\inetpub\myNode will not be able to find it.  All Modules for your website should be installed in your website directory, or one of its subdirectories.  All Modules are automatically installed to a folder named node_modules.

3.  That said, the first module we are installing will not install to the website’s node_modules directory:

https://github.com/TooTallNate/node-gyp

Node-gyp requires python 2.7.3 to be installed on your computer.  It is a relatively small program, and can be uninstalled very easily after you are done building Socket.IO

Go to your command line and type:

npm install -g node-gyp

node-gyp

3.  As I said in the requirements section, Node-gyp is a NPM build tool.  It is used to build some Node Modules.

4.  Now we install out first module.  Using the command line, change the directory to your website directory:

cd c:\inetpub\myNode

Using NPM, we will install a framework module called express, which is used in many tutorials:

npm install express

The Module should install itself automatically into c:\inetpub\myNode\node_modules.

5.  The last module we will install for this tutorial is Socket.IO, which is used for creating websockets.  Websockets can be used to create chatrooms.

Make sure you are still in c:\inetpub\myNode on the command line and type:

npm install socket.io

npm_error

The module will download fine, but there is currently an issue with this (and dozens of other modules) where it can’t find VCBuild.exe.  VCBuild is a Visual C++ Build tool that comes packaged with Visual C++ and Visual Studio.

6.  In this case, npm could not build the ws dependency for socket.io.  We can fix this in a flash.

From C:\inetpub\myNode In the explorer (not the command line), goto:

node_modules->socket.io->node_modules->socket.io-client->node_modules->ws->build

7.  You should see several files in this directory

build_socket

8.  Right click on either bufferutil.vcxproj or validation.vcxproj and open it in Visual Studio/Visual C++

build_socket2

9. You may go ahead and update the solution if you are using a newer version of Visual Studio

build_socket3

10.  Change the build mode from Debug to Release

build_socket4

11.  You can use the Build Command from the menu bar, or you can click the Green Start Button.  If you click the green start button, Ignore the follow message (it simply means that you tried to execute a non executable file.  But the module was still built:

build_socket5

12.  Now, your folder should look like this:

build_socket6

13.  If your folder looks like the last step, then you have successfully built Socket.IO!!

STEP THREE
Using Socket.IO on Arvixe (Without Node.exe)

With the Socket.IO module built, we can now create a simple WebSocket chatroom.

1.  In the root folder of your website, create a subdirectory called “scripts”.  This folder will hold all of your client side javascript files to keep them separate from your server side Node javascript files.

2.  The first script I am including is a jQuery.js file.  If you don’t know what jQuery is, it is a very popular javascript framework that makes accessing the DOM a hundred times easier.  http://jquery.com/

3.  The second script I am including is the client side socket.io.js file.  This file will be used by the client to connect to the websocket.  It can be found in your node_modules directory:

C:\inetpub\myNode\node_modules\socket.io\node_modules\socket.io-client\dist\

socket.io.min.js is a minified version of socket.io.js with all of the whitespace removed, allowing it to be downloaded twice as fast, but making it impossible to read by developers.

Copy it into your C:\inetpub\myNode\scripts directory.  But rename it to socket.js or socketio.js.  You will see why after a few steps, but it cannot be named socket.io.

4.  Create a new file, server.js in the root of your website:  C:\inetpub\myNode

Server

This simple script is going to allow multiple users to talk to each other by broadcasting to all clients connected to the websocket it creates.

5.  Now create a second file in the root directory, client.html.  OR, if you want to erase the examples from earlier, you can name it index.html

html

6. Create a css sheet.  This will give the chatbox a scrollbar and will make it a fixed size, as well as some other styling.

css

7. Finally, create the Client Side javascript code.

clientjs

To non-jQuery users, this javascript might seem confusing.  It might help to see it in action first.

8.  If you tried to open your application now, you would probably get several 404 errors when trying to open the websocket.

socketnotfound

This is because server.js is not running.  Most tutorials tell us to use the commandline:
node server.js
to start the the server.  However, we cannot do this on Arvixe without a dedicated server.

Server.js should be able to start on its own if the client tries to connect a websocket to it.  But here is our problem:  The websocket automatically tries to connect to:

http://domain.com/socket.io/1/?t=1234567890

Even if we tried to assign the websocket to connect to our server.js file,
it would still append socket.io to the location, and it would throw a 404 error.

9.   There is a very clever workaround however:  Rewrite.

We are going to modify the web.config file so that every time a request is made to:

http://domain.com/socket.io,

it instead maps to our server.js file

webconfig

This rewrite rule is why we changed the name of the client side socket.io.js, because it, too would get rewritten.

Also, note that we are blocking all client-side access to our node_modules directory.

10.  Now open up your browser and go to your website:  http://localhost:[port]

Result

11.  Finally you can upload the contents of your website directory (including node_modules) to your Arvixe website.

FINAL WORDS
Any websocket or node server you create will stay open indefinitely. This can be good if you have a dozen people chatting in your chatroom.

But what happens when everybody leaves and your websocket is still open?
Or what happens if you aren’t using sockets?

In the background, Node.exe is opening up new instances and it continues to listen for requests on the port.
In order to close the server when it is not being used, add the following code:

process.exit();

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under DotNet/Windows Hosting | RSS 2.0

Author Spotlight

Sean Gilliland-Sauer

Sean Gilliland-Sauer

I am a Fine Arts Major at IUPUI in Indianapolis, Indiana. I am also working towards a certificate in Applied Computer Science and maybe a minor in Electrical and Computer Engineering Technology.

One Comment on IIS Node on Arvixe Windows Server with Socket.io Tutorial

  1. Michael Stokesbary says:

    Thank you very much for this post. I just want to point out a couple of things that tripped me up when I first tried to get this working on arvixe hosting (by the way, this is not a knock at all on the hosting that is being provided, just some notes that I took while trying to do some troubleshooting with my hosting environment). As of the posting of this comment, the version of node.js being run on the hosting server is 0.6.20 and it is the 32-bit version. I point this out because when setting up my development environment, I originally installed the latest version of node.js and also installed the 64-bit version, which caused some issues for me. Also, you will want to make sure you install the version of iisnode that is installed through the web platform installer because the newer version that is out on github seems to have some behavior changes that won’t work with the version installed on arvixe (currently they are running version 0.1.19 but the current version on github is 0.2.6). After getting these versions installed and following the directions in step 1 above (except for some reason when installing iisnode through the web platform installer I didn’t get the wwwroot directory, so you will want to get that from github).

    One thing I do want to point out is you also seemed to post a thread in the forums (http://forum.arvixe.com/smf/programming-questions-tutorials/iis-node-on-arvixe-windows-server-with-socket-io-tutorial/) that is a little different than the directions you posted above. In particular there seems to be a step you mentioned in the forum post, but not here, where you mention creating a web.config file at the root of the site that has an iisnode configuration section in it. I noticed on my local install that iisnode 0.1.19 does not appear to have an inteceptor.js file.

    Again, thanks for both write ups, though, and thanks for providing such wonderful hosting.

Leave a Reply

Your email address will not be published. Required fields are marked *


1 + = 7

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>