Hurricane Development Hurricane Development - 1 year ago 153
Node.js Question remove listener after being executed

I want to create a listener on a socket. Then once the listener has been executed, I want the listener to stop listening. I used this SO: Removing specific listener

But I cannot figure out how to have a listener destroy itself. This is what I am thinking:

var ListenEventFunc = function() {
/* Do some important stuff */


What is the correct/best way to do this? Is there anyway I can use the
keyword to specify the function.

Answer Source

You can use this:

socket.once('ListenEvent', ListenEventFunc);

This works because Socket inherits from EventEmitter. This doesn't seem to be documented, but the code references it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download