node.jseventemitternode-apn

Warning : occur node apn emitter.setMaxListeners()


I send push notications using node apn.

occur problem of emitter.setMaxListeners() about sending push on 10,000 users.

but not occur problem about sending push on 1,000 users.

how fix this problem.

warning message :

(node:17804) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 wakeup listeners added. Use emitter.setMaxListeners() to increase limit

code

const server = new Hapi.Server();
server.connection({port: 3000});
...
server.register([{
...
}], (err) => {
    if (err) {
        throw err;
    }

    server.start((err) => {
        if (err) {
            throw err;
        }
        console.log(`Server running at : ${server.info.uri}`);
    });
});

server.route({
    method: 'POST',
    path: '/push',
    handler: function(request, reply) {
        ...
        var notification = new apn.Notification() ;
        notification.badge = count ;
        notification.sound = "default" ;
        notification.alert = message ;
        notification.topic = toping ;
        ...
        //here tokens 10,000 counts
        apn_sender.send(notification, tokens).then((result) => {

            var res = {
                success : result.sent.length,
                failed : result.failed.length
            } ;

            return reply(res) ;

        }) ;
        ...
    }
});

Solution

  • Try setting setMaxListeners as,

    process.setMaxListeners(0);
    

    The EventEmitters tends to print a warning (by default) when more than 10 listeners are getting added for a particular event. This enables us to help finding memory leaks. However, not all events should be limited to just 10 listeners. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.

    Hope this helps!.