IIS Node on Arvixe Windows Server with Socket.io Tutorial
Written by Sean Gilliland-Sauer Friday, 15 March 2013
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 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.
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++
- 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.
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.
- First, Install Node. http://nodejs.org/download/
- 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.
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:
5. Your website folder should now look like this:
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.
7. Open up hello.js in the helloworld subdirectory
8. For the moment of truth, open up your browser and go the website.
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.
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.
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:
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:
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
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:
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
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:
7. You should see several files in this directory
8. Right click on either bufferutil.vcxproj or validation.vcxproj and open it in Visual Studio/Visual C++
9. You may go ahead and update the solution if you are using a newer version of Visual Studio
10. Change the build mode from Debug to Release
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:
12. Now, your folder should look like this:
13. If your folder looks like the last step, then you have successfully built Socket.IO!!
Using Socket.IO on Arvixe (Without Node.exe)
With the Socket.IO module built, we can now create a simple WebSocket chatroom.
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:
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
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
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.
8. If you tried to open your application now, you would probably get several 404 errors when trying to open the websocket.
This is because server.js is not running. Most tutorials tell us to use the commandline:
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:
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:
it instead maps to our server.js file
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]
11. Finally you can upload the contents of your website directory (including node_modules) to your Arvixe website.
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:
Looking for quality web hosting? Look no further than Arvixe Web Hosting!