Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird socket.io / EventEmitter issue #1136

Closed
drochag opened this issue Aug 6, 2015 · 5 comments
Closed

Weird socket.io / EventEmitter issue #1136

drochag opened this issue Aug 6, 2015 · 5 comments

Comments

@drochag
Copy link
Contributor

drochag commented Aug 6, 2015

While running my server, I might have a lot of objects listening on socket.io (not perfectly sure how it works, 2nd time implementing it) and I'm having this

// Terminal
 Waiting...
(node) warning: possible EventEmitter memory leak detected. 11 disconnect listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at Socket.addListener (events.js:179:15)
    at Object.exports.register (/Users/dan/Documents/projects/centro/server/api/saleProduct/saleProduct.socket.js:19:12)
    at onConnect (/Users/dan/Documents/projects/centro/server/config/socketio.js:25:52)
    at Namespace.<anonymous> (/Users/dan/Documents/projects/centro/server/config/socketio.js:71:5)
    at Namespace.emit (events.js:107:17)
    at Namespace.emit (/Users/dan/Documents/projects/centro/node_modules/socket.io/lib/namespace.js:205:10)
    at /Users/dan/Documents/projects/centro/node_modules/socket.io/lib/namespace.js:172:14
    at process._tickCallback (node.js:355:11)
[undefined:52218] CONNECTED

that line is the one having socket.on('disconnect' … here

// saleProduct.socket.js
exports.register = (socket) => {
  // Bind model events to socket events
  for (let i = 0, eventsLength = events.length; i < eventsLength; i++) {
    let event = events[i];
    let listener = createListener('saleProduct:' + event, socket);

    SaleProductEvents.on(event, listener);
    socket.on('disconnect', removeListener(event, listener));
  }
};

but the event emitter already has this SaleProductEvents.setMaxListeners(0); on saleProduct.events.js so not sure what could I be doing wrong.

@kingcody
Copy link
Member

kingcody commented Aug 6, 2015

So you're saying that this: saleProduct.socket.js:19:12 is this: socket.on('disconnect', removeListener(event, listener));?

If you could provide a link to your source, that would be really helpful.

@drochag
Copy link
Contributor Author

drochag commented Aug 7, 2015

@kingcody
Copy link
Member

kingcody commented Aug 7, 2015

@danmmx sorry for the delay. This issue is not with your saleProduct eventEmitter but the socket.io EE, which I'm sure you've realized. We only set the maxListeners(0) on our custom endpoint EE's, so you'll need to do the same for your socket.io EE.

A good place to do that might be here (server/config/socketio.js):

module.exports = function(socketio) {
  // socket.io (v1.x.x) is powered by debug.
...
  socketio.on('connection', function(socket) {
    socket.setMaxListeners(20);  // Set the client's socket.io EE to max listeners: 20
    // socket.setMaxListeners(0);  // Set the client's socket.io EE to max listeners: unlimited
    socket.address =
      socket.handshake.address !== null ?
      socket.handshake.address.address + ':' + socket.handshake.address.port :
      process.env.DOMAIN;
...
};

@drochag
Copy link
Contributor Author

drochag commented Aug 7, 2015

Thanks a lot @kingcody , I thought with the config on each model's EventEmitter was enough. This fixed it.

@drochag drochag closed this as completed Aug 7, 2015
@kingcody
Copy link
Member

kingcody commented Aug 7, 2015

No problem, glad to hear it (:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants