chr15m / bugout

Back end web app services over WebRTC.
https://chr15m.github.io/bugout
MIT License
603 stars 59 forks source link

"left" and "timeout" events never fire #31

Closed draeder closed 2 years ago

draeder commented 3 years ago

I have the following code:

    let b = new Bugout(identifier) //identifier passed in via code not shown here

    // Detect connected peers
    b.on("seen", function(address){
        console.log("Joined: " + address)
    })

    b.on("left", function(address){
        console.log("Left: " + address)
    })

I'm pretty sure I'm handling the "left" event correctly--please correct me if I'm not.

I'm wondering if the recent refactor affected it because I never receive the event. This is also true of the "timeout" event, when I have heartbeat enabled.

Using b.on("connections", function(c){ console.log("Connections: " + c) }) shows the correct connections count.

draeder commented 3 years ago

It looks like "timeout" and "left" events did eventually fire, but I'm not sure when they happened.

I've had an instance running since I posted this issue a couple days ago, and a client connection running against it since then also. I just checked the logging, and do see both events. First the "left" event, followed by the "timeout" event.

I was hoping to use it as a way to see if someone closed their browser tab. Since the remote peer addresses change every time a tab reloads, I expected to see the "left" event upon browser close / reload. Is this an incorrect assumption?

chr15m commented 3 years ago

@draeder if the timeout option is not passed, the "unseen peer" timeout defaults to 5 minutes.

If you want peers to know when the other peer has closed the browser tab then you should use the beforeunload event in the client and explicitly call b.destroy() when that happens. This should hopefully cause the client to send the "left" message as the browser tab is closed, but I haven't tested this myself. Code to try this:

window.addEventListener('beforeunload', function (e) {
  b.destroy();
  delete e['returnValue'];
});

In this code b should be your bugout instance. Let me know if it works for you.

PS you could also just shorten the timeout to e.g. 30 seconds = 30000.

draeder commented 3 years ago

I've been experimenting with the beforeunload event you suggested using, but it doesn't seem to work. I also set the timeout to 15 seconds b.heartbeat(15000), but did not get the "left" or timeout" events for about 50 minutes. If it matters, I'm testing this in Chrome.

chr15m commented 3 years ago

@draeder ok thanks for reporting this and for the detail. Seems like the "left" event doesn't make it out before the Bugout node is destroyed. Will investigate when I get some time.

draeder commented 3 years ago

Within the past couple of days, the "left" and "timeout" events appear to be occurring in a more timely fashion. But they still don't coincide with the passed timeout option.