connection piles up on each refresh

I'm using with node.js and inside one of my routes, I have the connection event handler.

  'connection', function(socket) {

                    socket.on('disconnect', function(){

Each time I refresh the page that's calling this route, it seems to keep adding up, for example, if I refresh 5 times, I'll end up having 5 'connect' messages, on a single refresh, in the log.

Is there a good way to limit this so it doesn't do this?



When I refresh repeatedly, I get this error:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit

I can increase the limit, but I have a feeling that's no the correct solution.

You have to place your code outside of your request handler (that gets called on each request). This code is supposed to handle incoming connections for all clients (not for each request). In your example it creates a new event listener on every request, you only want one !