Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 

News:



Author Topic: IIS Node on Arvixe Windows Server with Socket.io Tutorial  (Read 4165 times)

sjgillil

  • **
  • Posts: 14
  • Slow and Steady
    • View Profile
    • Snyco
IIS Node on Arvixe Windows Server with Socket.io Tutorial
« on: March 13, 2013, 05:13:57 PM »
Okay guys, Moment of truth.

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

Node.js is a javascript powered server.  It is slowly becoming one of my favorites because of its real-time capabilities.

IMPORTANT
IISNode requires a 64bit app pool.  You will get a 500 error: bad module if you are not running in 64 bits.  If your app pool is running in 32 bits, submit a ticket to http://support.arvixe.com and ask the support team to change the settings on your app pool.
NOTE After setting your app pool to 64 bits, it may revert to 32 bits if you change the ASP.NET version
(i.e. change it from ASP.NET 4 (classic) to ASP.NET 4(integrated pipeline) or vice versa),  so be cautious when changing your settings.

IMPORTANT
Some of Node's   Node Package Manager Modules may need to be built manually if an error occurs.  While this is a very simple process, it requires Visual Studio. 

TABLE OF CONTENTS
Step 1 - Install Node.js, IIS, and Socket.IO on your local computer for development
Step 2 - Deploying and Running your App on your Site
Step 3 - Using Socket.IO on Arvixe (without Node.exe)


Step 1
Before uploading Node apps, its important to test this out on your local computer. 
You should also have IIS running on your computer, because we will not be using Node.exe to
create a server. 

(This is to simulate the Hosted environment, where you will not be able to
execute Node.exe to start your server.)

1: Install Node - Windows version  http://nodejs.org
This installs an executable which will run your scripts.

2.  Install IISNode - https://github.com/tjanczuk/iisnode
ORR, you can do what I did and install it via
IIS's Web Platform Installer

3.  Test out Node to make sure it works.  There are several tutorials if you do not know how to use Node
already.

4.  Create a directory to host your app.  I created a directory in
C:\inetpub, because that is where all of my IIS websites are.

5.  In IIS, create a new website (on any port.  80 is usually taken, so go for 81+, or another port)
and map it to your app's directory.

6.  Using cmd.exe(command line) or better yet, Node's command line shell, cd to this directory : 
cd C:\inetpub\node

7.  Still in the directory on the command line, use Node's package manager to download socket.io:
npm install socket.io

8.  NPM installs node modules for an application.  There are tons of Node modules to download.  One
of my favorites is the jquery node module, which allows you to use jquery on the server side.

NOTE: Socket.IO may have some other module dependencies. 

NOTE: Some NPM modules throw errors during download when trying to build the module.
In these cases, check to see if all files have downloaded.  Read the error to find out
which part of the module could not be built.   Keep that Module in mind, we will come back to it in just a second

CONTINUING NOTE:   In order to build these modules, we will need a NPM tool called Node-gyp:  https://github.com/TooTallNate/node-gyp

Node-gyp can be installed using NPM:
npm install node-gyp

Now, follow the the instructions on Node-gyp's website.  It is very simple.
However, during build, you may still get an error that VCBuild.exe could not build the file, or could not
be found.  VCBuild is a Visual Studio program, and is included with Visual Studio.

The easiest solution to this VCBuild.exe error is to simply navigate to the module directory after
you have configured it with node-gyp and find the Visual Studio project file.

Visual Studio may prompt you to upgrade the properties of the build files.  This is fine.
Once you are ready to build, make sure to build a Release version, and not a Debug version.
If you press the brown start button, Visual Studio will still build the module, but it will display
an error message saying that it cannot run the program.  This error is fine.  It simply means
that this program is not an executable file.


You should now have everything you need to get started with development. 


Step 2
Deploying and running your app on your website

1.  First, we are going to test out a Node app on your localhost before
deploying it.  Instead of writing a whole app in this tutorial, we will
use the test app that came with IISNode.

2.  Navigate to your IISNode installation folder, usually
C:\Program Files\iisnode
Inside this directory is a subdirectory called 'www'.
Copy the contents of this directory into
Website directory we created earlier:
C:/inetpub/node

3.  Now, before we launch this in our browser, lets look at the web.config files.
These files are not correctly configured for our server.

Each subdirectory in your main directory has a web.config file. 
Open each of them.

4. Delete this block:
<iisnode     
      node_env="%node_env%"
      nodeProcessCountPerApplication="1"
      maxConcurrentRequestsPerProcess="1024"
      maxNamedPipeConnectionRetry="100"
      namedPipeConnectionRetryDelay="250"     
      maxNamedPipeConnectionPoolSize="512"
      maxNamedPipePooledConnectionAge="30000"
      asyncCompletionThreadCount="0"
      initialRequestBufferSize="4096"
      maxRequestBufferSize="65536"
      watchedFiles="*.js;iisnode.yml"
      uncFileChangesPollingInterval="5000"     
      gracefulShutdownTimeout="60000"
      loggingEnabled="true"
      logDirectory="iisnode"
      debuggingEnabled="true"
      debugHeaderEnabled="false"
      debuggerPortRange="5058-6058"
      debuggerPathSegment="debug"
      maxLogFileSizeInKB="128"
      maxTotalLogFileSizeInKB="1024"
      maxLogFiles="20"
      devErrorsEnabled="true"
      flushResponse="false"     
      enableXFF="false"
      promoteServerVars=""
      configOverrides="iisnode.yml"
     />


5. ALSO, delete this block:
<iisnode
      nodeProcessCommandLine="&quot;%programfiles%\nodejs\node.exe&quot;"
      interceptor="&quot;%programfiles%\iisnode\interceptor.js&quot;" />


6. Now, create a web.config file in the root directory:

<configuration>
     <system.webServer>
           <iisnode nodeProcessCommandLine="C:\progra~2\nodejs\node.exe"
      interceptor="C:\progra~1\iisnode\interceptor.js" />
     </system.webServer>
</configuration>


NOTE C:\progra~2 is shorthand for Program Files(x86) and C:\Progra~1 is shorthand for Program Files.
This configuration works on my Arvixe server, but it may not work on everyone's, depending on where the programs are installed.
(in this example, node.exe is 32 bits, and iisnode is 64bits)

NOTEPlacing this configuration in the root directory means that it does not have to be placed again in the subdirectory

7.  Open the helloworld directory's configuration file. 
This is all that you need to run the hello.js app:

<configuration>
  <system.webServer>

    <handlers>
      <add name="iisnode" path="hello.js" verb="*" modules="iisnode" />
    </handlers>
   
  </system.webServer>
</configuration>


8.  Do the same for the other examples, except leave the rewrite rules, etc.

9.  Now, open your app in the browser by going to the port mapped to your website
on the local server:  http://localhost:[port]/

10.  Go to the hello world example, and click on the link to hello.js

NOTE If you still get an error, open up hello.js, and look towards the end
of the file where it says:

.listen(1234); 

Change it to this:

.listen(process.env.PORT);

Now, it will listen on the current port, instead of some random port that might be blocked by a firewall.

11.  Hopefully, all of this worked.  Now, upload the contents of your website directory to the
website on your server that has a 64 bit app pool.

12.  Navigate to your application, and run it. 

Part 3
Using Socket.io on Arvixe (without Node.exe);

1.  Most Socket.io tutorials tell you that you need to launch a node server using the command:
node server.js

There is a simple workaround.

1.  When a websocket connection is made on the client side via socket.io, the browser attempts
to connect to the domain root like this:
www.domain.com/socket.io/1/t=123456789

Without executing a server via Node.exe, this socket is never opened, and so the server.js is never listening
for this connection.

2.  Using a clever rewrite technique, we can map all requests to this socket.io websocket directly to the server.js file.

In the root directory, add this configuration to the <system.webServer> section:

<rewrite>
   <rules>
      <rule name="LogFile" patternSyntax="ECMAScript">
         <match url="socket.io"/>
         <action type="Rewrite" url="server.js"/>
      </rule>
   </rules>
</rewrite>


NOTE  This configuration must be in the root directory.  However, the url can map to a subdirectory.

3.  This rewrite unfortunately affects the client side socket.io.js file. 

In order to fix this, rename the socket.io.js file to something like: socketio.js  or socket.js

4.  Now, the last configuration setting is to disable websockets.  I am not sure why we include this setting,
it is certainly contrary to logic, but this is how I learned it.  Add the following configuration setting to your root directory's web.config:

<webSocket enabled="false"/>

5.  Don't forget to add a handler mapping to your server.js file:

<handlers>
   <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
</handlers>


6.  Now, your config file should look like this:


<configuration>
    <system.webServer>
        <directoryBrowse enabled="false" />
      <rewrite>
         <rules>
            <rule name="LogFile" patternSyntax="ECMAScript">
               <match url="socket.io"/>
               <action type="Rewrite" url="server.js"/>
            </rule>
         </rules>
      </rewrite>
      <webSocket enabled="false"/>
         
   <iisnode nodeProcessCommandLine="C:\progra~2\nodejs\node.exe"
      interceptor="C:\progra~1\iisnode\interceptor.js" />
    </system.webServer>
</configuration>


7.  If you aren't sure how to create a Node server.js file, or a client html file,
I have included an attachment.

NOTE My example requires the 'express' module,
npm install express

NOTE My example does not include any of the required modules,
only the script.


QUICK TIPS
1.  node_modules may be accessed by including the "requires('[module_name]')" only if
the node_modules directory is in the same directory as the node script.
If the node_modules directory is in a subdirectory, you can use "requires('dir_name/module_name')"

2.  While my attachment includes an html client, clients can also be written into an ASP.NET page,
or any other languages that you use in your web application.

Attachments
I have included source code for a simple Socket.io chat server and client.
« Last Edit: March 13, 2013, 08:26:33 PM by sjgillil »
Snycopate your life

victor.gebhardt

  • Shift Manager
  • *****
  • Posts: 2034
    • View Profile
    • Email
Re: IIS Node on Arvixe Windows Server with Socket.io Tutorial
« Reply #1 on: March 13, 2013, 05:16:42 PM »
Thanks for the tutorial!
Victor Gebhardt
Shift Manager
victor.gebhardt@arvixe.com
1-888-927-8493 [Ext.990]

David Martines

Re: IIS Node on Arvixe Windows Server with Socket.io Tutorial
« Reply #2 on: December 10, 2013, 02:36:23 PM »
First, thanks for this great info!   I'm trying to get this going, starting out with your iisnode-tutorial-v2.zip.  I had Arvixe convert my app pool to 64bit.  I am getting 500 errors, though, when it gets to making the first socket io call to /socket.io/1/?t=1386696510899.

Here is what I have in the web.config:
Code: [Select]
<configuration>
    <system.webServer>
      <directoryBrowse enabled="false" />
  <rewrite>
  <rules>
  <rule name="LogFile" patternSyntax="ECMAScript">
  <match url="socket.io"/>
  <action type="Rewrite" url="server.js"/>
  </rule>
  </rules>
  </rewrite>
      <!-- <webSocket enabled="false"/>-->
      <iisnode nodeProcessCommandLine="C:\progra~2\nodejs\node.exe" interceptor="C:\progra~1\iisnode\interceptor.js" />
      <handlers>
        <add name="iisnode" path="server.js" verb="*" modules="iisnode"  />
      </handlers>
    </system.webServer>
<location path="node_modules">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
</configuration>

Some questions:
  • I had to comment out the webSocket element, to avoid getting a 500 error on the site in general.  Could this indicate a more insidious problem?
  • I assume it's required to include the iisnode element, as well as the handler, so that the server.js is processed by iisnode.   Is this correct?  It's not immediately clear in the tutorial.
  • Is there some way I can verify if the app pool is 32 or 64 bit, as well as the exact paths to node.exe and interceptor.js?

Thanks for ANY help from anyone on this!

victor.gebhardt

  • Shift Manager
  • *****
  • Posts: 2034
    • View Profile
    • Email
Re: IIS Node on Arvixe Windows Server with Socket.io Tutorial
« Reply #3 on: December 10, 2013, 02:43:26 PM »
Hello,

If you open up a ticket and provide us with your ticket id here in this thread, I can definitely check to see if your application pool is set to 32 or 64-bit. I think this would be the best place to start and then we can go from there.
Victor Gebhardt
Shift Manager
victor.gebhardt@arvixe.com
1-888-927-8493 [Ext.990]

David Martines

Re: IIS Node on Arvixe Windows Server with Socket.io Tutorial
« Reply #4 on: December 10, 2013, 06:25:25 PM »
Thanks, Victor.  My Ticket ID is UJZ-538-36357

victor.gebhardt

  • Shift Manager
  • *****
  • Posts: 2034
    • View Profile
    • Email
Re: IIS Node on Arvixe Windows Server with Socket.io Tutorial
« Reply #5 on: December 10, 2013, 06:36:28 PM »
Hello,

I am looking into this now and will update you via your ticket.
Victor Gebhardt
Shift Manager
victor.gebhardt@arvixe.com
1-888-927-8493 [Ext.990]

victor.gebhardt

  • Shift Manager
  • *****
  • Posts: 2034
    • View Profile
    • Email
Re: IIS Node on Arvixe Windows Server with Socket.io Tutorial
« Reply #6 on: December 10, 2013, 08:22:17 PM »
Your ticket has been updated.
Victor Gebhardt
Shift Manager
victor.gebhardt@arvixe.com
1-888-927-8493 [Ext.990]

 

Web Hosting

Linux Hosting
ASP .NET Hosting
Business Hosting
Reseller Hosting
Dedicated Servers

Server Alerts
Acceptable Use Policy
Terms of Service
Privacy Policy

Affiliate Program
Site Map

Blog  Hosting

WordPress Hosting
ExpressionEngine Hosting
Movable Type Hosting
Habari Hosting
Textpattern Hosting


CMS  Hosting

Joomla Hosting
Drupal Hosting
MODX Hosting
Concrete5 Hosting
Xoops Hosting
Ecommerce  Hosting

nopCommerce Hosting
OpenCart Hosting
Magento Hosting
osCommerce Hosting
Tomato Cart Hosting


Video Sharing Software  Hosting

ClipBucket Hosting
PHPMotion Hosting
ClipShare Hosting
osTube Hosting
vShare Hosting
Forum  Hosting

XenForo Hosting
vBulletin Hosting
MyBB Hosting
phpBB Hosting
Simple Machines Hosting


Social Network and Community Website  Hosting

SocialEngine Hosting
Elgg Hosting
Dolphin Hosting
phpfox Hosting
OxWall Hosting
Copyright © 2003-2014 Arvixe, LLC. All trademarks are property of their legal owner.