IISNode, Express & Socket.IO Chat application reconnecting every ~2 minutes

1k views Asked by At

I have created a simple chat application with Express and Socket.IO which I've now set up on a hosting provider which uses IISNode. The problem I'm having is that the socket connection keeps reconnecting approximately every two minutes. This happens regardless of whether there is activity over the socket or not.

Tech support from the host told me that my application always uses lots of memory. This seems highly unlikely to me. As far as I am aware, each domain is allocated 512MB of memory. They told me that the application pools are recycled when the memory is used up, which is why my app keeps reconnecting. I have tested this code endlessly. Even when running this on my dev machine, my node.exe process never uses more than 32MB of memory.

So I need a sanity check; below is my code. Am I really doing something stupid here? Is there a way for me to profile the application to find out how much memory it is actually using?

the server-side:

var express = require('express');
var http = require('http');

var app = express();
var server = http.createServer(app);

var io = require('socket.io').listen(server);

app.use(express.static(__dirname + '/public'));

io.configure(function() {
    io.set('resource', '/public/socket.io');
});

io.sockets.on('connection', function (socket) {
    socket.on('fromClient', function (data) {
    io.sockets.emit('fromServer', { who: data.who, message: data.message });
    });
});

server.listen(process.env.PORT || 8080);

The client-side:

window.addEventListener("load", function () {

    var msgContainer = document.getElementById("msgContainer");
    var btnSend = document.getElementById("sendMessage");

    //this is my localhost, of course, substituted by my domain name on live
    var socket = io.connect('http://localhost:8080/', {
        resource: 'public/socket.io'
    });

    var theWho = document.getElementById("theWho");
    var newMessage = document.getElementById("newMessage");

    if (newMessage.addEventListener) {
        newMessage.addEventListener('keyup', newMessage_keyUp, false);
    } else if (btnSend.attachEvent) {
        newMessage.attachEvent('onkeyup', newMessage_keyUp);
    }

    function newMessage_keyUp(e) {
        if (e.keyCode == 13) {
            messageToServer();
        }
    }

    function messageToServer() {
        var who = theWho.value;
        var message = newMessage.value;
        if (who == "") {
            who = "Anon";
        }
        socket.emit("fromClient", {
            who: who,
            message: message
        });
        newMessage.value = "";
        newMessage.focus();
    }

    if (btnSend.addEventListener) {
        btnSend.addEventListener('click', messageToServer, false);
    } else if (btnSend.attachEvent) {
        btnSend.attachEvent('onclick', messageToServer);
    }

    socket.on('fromServer', function (data) {
        addMessage(data.who, data.message);
    });

    //THIS is what keeps firing ever ~2 minutes
    socket.on('connect', function () {
        socket.emit("fromClient", {
            who: "Server",
            message: "New user connected.."
        })
    })

    function addMessage(who, message) {
        var m = document.createElement("div");
        m.innerHTML = "<strong>" + who + ":</strong>&nbsp;" + message;
        m.setAttribute("class", "msg");
        msgContainer.appendChild(m);
        m.scrollIntoView();
    }
});
1

There are 1 answers

1
Tomasz Janczuk On BEST ANSWER

You can get a lot of diagnostics information about a Node.js application deployed with iisnode using the iisnode-debug HTTP response header. Here are the details: http://tomasz.janczuk.org/2012/11/diagnose-nodejs-apps-hosted-in-iis-with.html

In particular, using iisnode-debug header will enable you to check the memory consumption of both the IIS worker process (where iisnode itself runs) as well as node.exe process (where your Node.js application runs). It also reports the PIDs of the IIS worker process and node.exe, which will help you determine if any of the processes is indeed recycled.