Thursday, 18 May 2017

NodeJS warning: possible event emitter leak. 11 open listeners added

I'm using NodeJS and WS to test out websockets. To offload some work from the server, I want to send pings from the client to the server instead of the other way around. However, whenever i run my code i get this error:

> (node) warning: possible EventEmitter memory leak detected. 11 pong listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at WebSocket.addListener (events.js:239:17)
    at Object.<anonymous> (/home/ubuntu/NodeJS-Runtime/websockets/client.js:40:12)
    at Module._compile (module.js:410:26)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)
    at startup (node.js:136:18)
    at node.js:966:3

here is my code:

for(var i=0; i<20; i++) {
    const clientNum = i;
    const ws = new WebSocket('ws://localhost:8080', {
    perMessageDeflate: false
        });

    ws.onerror = function(error) {
        if(!hasFailed && clientNum != 0)
            console.log('failed on: ' + clientNum + error);
        hasFailed = true;
    }

    ws.on('open', function() {
        // Send keep alive messages. Close if no response.
        ws.keepAlive = false;
        var interval = setInterval(function() {
            if (ws.keepAlive) {
                ws.close();
            } else {
                ws.ping(null, null, true);
                ws.keepAlive = true;
            }
        }, 25*1000); // milliseconds between pings

    });
    ws.on("pong", function() { 
            ws.keepAlive = false; 
    });

I get this error for both the pong and 'on' function.



via dalanb

No comments:

Post a Comment