unsetbit / p

Peer-to-peer networking with browsers
http://unsetbit.com/p
Other
411 stars 27 forks source link

Docs say `close` but code says `disconnection` #8

Closed cvan closed 10 years ago

cvan commented 10 years ago

Per the API in the README:

anyNode.on('close', function(){}); // emitted when this connection is closed

Per P.js:

    this.peers.onRemove = function(peer){
        emitter.emit('disconnection', peer);
    };

Should the docs say disconnection - or am I mistaken?

unsetbit commented 10 years ago

My memory is a bit fuzzy, but I think the docs are correct. The 'close' event is fired by peer nodes themselves, whereas the 'disconnection' event is fired by the P object itself (so you can listen for disconnection of nodes even if you don't have a handle on the objects).

https://github.com/oztu/p/blob/master/lib/P.js#L44 :

peer.on('close', function(){
    peers.remove(peer);
});

The close event is literally the webrtc datachannel close event, so it gets that event object as the callback argument for listeners, whereas the 'disconnection' event provides the peer object as the callback argument for listeners.

cvan commented 10 years ago

The close event on the peer was never firing. Turns out this is why:

It looks like your code is doing the right thing. Now that I know this is why, it might be good to make mention of it in P's docs. Thanks for your help.