0

I built an online chat application using NodeJS, Express, and Socket.IO - runs fine locally, but cant get it working on the server. I'm running Windows 2012/IIS 8.5/IISNode. My site is not running as a virtual, it's running as an application in this format: http://[domainname]/subfolder/chatserver.js.

When I run the app, I have a route setup so if you go to '/', it serves up a regular html chat window. If you go to a second url, it serves up an admin chat window. Both windows should connect to the server in order to send out messages, however I cant get it to connect.

I'm referencing the socketio client like so:

<script   src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.7/socket.io.min.js">    </script>

When I run it locally, I actually dont have to reference it like this, I can do this instead

<script src="socket.io/socket.io.js"></script>

This way, if I understand correctly, it should download the script from the server. However, I get 404 no matter what I try using this method.

In the client code, I'm executing this call: socket = io.connect(address, details); console.log(socket);

After the connect method, the socket.connected is false. Does anyone know if it's not connecting because I'm downloading the js from a different location than the nodejs app running on the server?

My second question is, why wouldnt I be able to reference the script properly and have it automatically downloaded? Is it because I'm running as an application, not as a virtual? I need to be able to run as an application so that I can use the domain name.

Any help would be appreciated, I've been working through this problem for going on 4 days now! :(

Here is my web.config file:

<configuration>
  <system.webServer>

    <!-- indicates that the server-faye.js and server-socketio.js files are node.js applications 
    to be handled by the iisnode module -->

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

    <!-- indicate that all strafic the URL paths beginning with 'socket.io' should be 
    redirected to the server-socketio.js node.js application to avoid IIS attempting to 
    serve that content using other handlers (e.g. static file handlers)
    -->

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

    <!-- disable the IIS websocket module to allow node.js to provide its own 
    WebSocket implementation -->
    <webSocket enabled="false" />

  </system.webServer>
</configuration>

Here is the server code:

var app = require('express')();
var httpServer = require('http').Server(app);
var io = require('socket.io')(httpServer);

var port = process.env.PORT || 3000;

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/chat-index.html');
});

app.get('/chat-admin', function (req, res) {
    res.sendFile(__dirname + '/chat-admin.html');
});

httpServer.listen(port, function () {
    console.log('listening on port: ' + port);
});

// Fires when a client connects to the server
io.sockets.on('connection', function (socket) {
    console.log('a user connected');
});

As you can see it's very basic. When I hit the url, I should get two messages. The first saying it's listening on a port, and the second saying a user connected. Locally, I get both. On the server, I just get the listening on a port message.

Here's the client code:

<!doctype html>
<html>
<head>
    <title>Chat</title>
</head>
<body class="chat-body">

    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>

    <script type="text/javascript">
        var socket

        $(document).ready(function () {
            // Connect to the chat server
            socket = io();
            console.log(socket.connected);
        });
    </script>

</body>
</html>
Randy
  • 71
  • 1
  • 8

1 Answers1

0

This script tag (note the slightly different leading slash from what you have):

<script src="/socket.io/socket.io.js"></script>

will work if you have a web server that will serve up socket.io.js when it receives a request for this path.

If you are running node.js server-side and initializing the socket.io library properly on node.js and you have socket.io installed properly on the server, then socket.io will intercept that above request and will serve socket.io.js back to the browser. Since you're getting a 404, you apparently don't have this configured correctly. Since you haven't provided any of your node.js code, we can't really advise any more specifically.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I've finally narrowed this down a bit. I setup the dante example as a virtual in IIS. I then setup a site called dante.com pointed to the exact same folder as the virtual and added a host entry. The virtual folder works, the site doesnt. The diff is when i hit the virtual folder url it makes this call: http://localhost/dante/socket.io/socket.io.js which returns the js. Calling dante.com makes the same call like: http://dante.com/socket.io/socket.io.js and returns the client.html, not the js. The rewrite must be what's causing the issue somehow but I cant see anything wrong with it. – Randy Nov 12 '15 at 17:25