2
votes

I'm node.js beginner. Trying to create simple chat app, but have problem. When refreshed page fast 10+ times I have warning:

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

When I disabled socket.io, this warning disapears. So, problem with socket.io. How to increase max listeners for socket.io? Or maybe another solution?

Code:

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

var server = http.createServer(function(req, response) {
    req.setMaxListeners(0);
    var pathname = __dirname + '/index.html'

    fs.readFile(pathname, "binary", function(err, file) {
        if (err) {
            response.writeHead(500, {'Content-Type': 'text/plain'});
            response.end(err);
        } else {
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write(file, "binary");
            response.end();
       }
    });
});

io = require('socket.io').listen(server);
server.listen(4567);
2
Why do you set maxListener in the first place? Maybe GC is to slow. Also I am not sure if socket.io is fine in this quite incomplete state you are using it. - TheHippo
I put just little example here. I'm going to use websockets on client and server. But now I have to fix memory problem. - Yurii Kravchuk

2 Answers

0
votes

I can't reproduce the problem, but try this:

io.setMaxListeners(0);
0
votes

Try this at the top of your code to increase the emitter limit globally

require('events').EventEmitter.prototype._maxListeners = 100;