Removing an event listener on the client

11,512 views
Skip to first unread message

Jeff Zimmerlin

unread,
Jul 27, 2011, 6:31:30 PM7/27/11
to Socket.IO
What's the proper way to clear a Socket.IO event listener on the
client?

I used...

//Connect
Socket=io.connect(...);

//Bind
Socket.on("forceUpdate", function(){
...
});

//Unbind
Socket.$events.forceUpdate=null;

...but I don't know if that adheres to best practices. Thoughts?

Thanks so much!

Arnout Kazemier

unread,
Jul 28, 2011, 3:21:28 AM7/28/11
to sock...@googlegroups.com
Socket.IO uses event emitters for events so you can just use: http://nodejs.org/docs/v0.4.10/api/events.html#emitter.removeListener to remove listeners

Jeff Zimmerlin

unread,
Jul 28, 2011, 11:48:27 AM7/28/11
to Socket.IO
I'm talking about removing event listeners on the client, not the
server.

On Jul 28, 12:21 am, Arnout Kazemier <i...@3rd-eden.com> wrote:
> Socket.IO uses event emitters for events so you can just use:http://nodejs.org/docs/v0.4.10/api/events.html#emitter.removeListenerto remove listeners

Jeff Zimmerlin

unread,
Jul 31, 2011, 8:39:59 PM7/31/11
to Socket.IO
Anybody know?

On Jul 28, 8:48 am, Jeff Zimmerlin <jeffzimmer...@gmail.com> wrote:
> I'm talking about removing event listeners on the client, not the
> server.
>
> On Jul 28, 12:21 am, Arnout Kazemier <i...@3rd-eden.com> wrote:
>
>
>
>
>
>
>
> > Socket.IO uses event emitters for events so you can just use:http://nodejs.org/docs/v0.4.10/api/events.html#emitter.removeListenertoremove listeners

Arnout Kazemier

unread,
Aug 1, 2011, 3:00:57 AM8/1/11
to sock...@googlegroups.com
@Jeff

We also ported the EventEmitter to the browser, so it uses the same API there.
See https://github.com/LearnBoost/socket.io-client/blob/master/lib/events.js for the code

Jeff Zimmerlin

unread,
Aug 1, 2011, 8:03:17 PM8/1/11
to Socket.IO
@Arnout

Ah I see. Thank you!

On Aug 1, 12:00 am, Arnout Kazemier <i...@3rd-eden.com> wrote:
>  @Jeff
>
> We also ported the EventEmitter to the browser, so it uses the same API there.
> Seehttps://github.com/LearnBoost/socket.io-client/blob/master/lib/events.jsfor the code
>
> On Aug 1, 2011, at 2:39 AM, Jeff Zimmerlin wrote:
>
>
>
>
>
>
>
> > Anybody know?
>
> > On Jul 28, 8:48 am, Jeff Zimmerlin <jeffzimmer...@gmail.com> wrote:
> >> I'm talking about removing event listeners on the client, not the
> >> server.
>
> >> On Jul 28, 12:21 am, Arnout Kazemier <i...@3rd-eden.com> wrote:
>
> >>> Socket.IO uses event emitters for events so you can just use:http://nodejs.org/docs/v0.4.10/api/events.html#emitter.removeListener...listeners

Brian Carr

unread,
Aug 12, 2011, 5:02:41 PM8/12/11
to sock...@googlegroups.com
I'm using socket.io 0.7.7 and removeListener() on a client-side socket instance doesn't appear to be working for me.  The following adds a listener which responds properly to events emitted from the server, as expected;

socket.addListener('test', function(data) { 
  console.dir(data); 
}); 

However any attempt to remove the listener doesn't appear to work;

socket.removeListener('test', function(data) { 
  console.dir(data);
});

It doesn't produce an error and the client will continue to respond (according to the listener callback) to server-side events of the same name.  Am I using it incorrectly?

Arnout Kazemier

unread,
Aug 12, 2011, 5:09:04 PM8/12/11
to sock...@googlegroups.com
Make sure you the function that you added like this:

function test (data){
console.dir(data);
}


// add
socket.on('test', test);

// remove
socket.removeListener('test', test);

Brian Carr

unread,
Aug 12, 2011, 5:17:44 PM8/12/11
to sock...@googlegroups.com
Excellent, thanks!
Reply all
Reply to author
Forward
0 new messages