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>