StormTrooper
StormTrooper

Reputation: 1573

(node:6261) MaxListenersExceededWarning: Possible EventEmitter memory leak detected

I am getting this error on nodejs app: Node version: v8.11.1

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

I have a very simple code of pushing notification to IOS device using node-apn. The code is working fine, I am receiving push notifications on device as well

var express               = require('express')
  , serverPort            = 6900
  , app                   = express()
  , http                  = require('http')
  , https                 = require('https')
  , fs                    = require('fs')
  , apn                   = require('apn')
;

var server = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.write('Hello!');
  res.end();
});

server.listen(serverPort, (err) => {

  if(err) { return console.log("Something bad happened", err); }
});

var apnOptions = {

  token: {
  },
  production: false 
};

var apnProvider = new apn.Provider(apnOptions);
var note        = new apn.Notification();

note.expiry     = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge      = 3;
note.sound      = "ping.aiff";
note.alert      = "\uD83D\uDCE7 \u2709 You have a new message";
note.payload    = { 'messageFrom': 'John Appleseed' };
note.topic      = "com.example.test";

apnProvider.send(note, '6A752501A5DAFF9DCSFRE56C5B0E699385CD14B586CEF4B9C5012DDA4').then( (result) => {

  console.log(result); 
});

The issue here is with APN code, because as soon as I comment APN code, I dont face this error anymore. Can anyone have any idea what is wrong with my code? I feel like I have done everything right here but still I am facing this issue.

Upvotes: 1

Views: 9235

Answers (2)

Eray Balkanli
Eray Balkanli

Reputation: 7960

There are two solutions. Both are related to updating maximum number of listeners and both should be added at the beginning of your code.

1- You may set 0 for event listeners like below, risky because you are basically removing the limit:

const emitter = new EventEmitter()
emitter.setMaxListeners(0)

2- Globally fixes the error, set a high value as maximum like

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

Reference 1: https://nodejs.org/docs/v0.4.7/api/events.html#emitter.setMaxListenershttps://nodejs.org/docs/v0.4.7/api/events.html#emitter.setMaxListeners

Reference 2: possible EventEmitter memory leak detected

Upvotes: 3

yeya
yeya

Reputation: 2204

node-apn has several issues, try to use node-apn-http2.

npm install node-apn-http2

Replace , apn = require('apn')

with , apn = require('node-apn-http2')

Upvotes: 0

Related Questions