connection piles up on each refresh

Go To


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.

2012-04-03 20:58
by dzm


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 !

2012-04-03 21:40
by stewe
Ok, so I've done that and it works, but I need to pass data from within my route code to the socket connect handler, if my connect handler is defined first outside of the route, then how I would pass an object from my route, back to my connect handler - dzm 2012-04-03 22:26
You could use an array to store objects that are comming from the http-request and read them inside of your connection handler. But i assume you want to send some data to the connected clients using a http-request, in this case you would just use something like'myEvent',{msg:'hello world'}); inside your request handler - stewe 2012-04-03 22:50
Perfect, yeah this should be fine. Thanks a lo - dzm 2012-04-03 22:55
@stewe Could you give an example? I am new to node.js so I don't quite understand how to do this - Automatico 2013-03-29 14:33