0

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:

socket.io 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 */ socket.removeListener('ListenEvent',ListenEventFunc); } socket.on('ListenEvent',ListenEventFunc); 

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.