t4t5 / nostr-react

React Hooks for Nostr 🦤
MIT License
85 stars 14 forks source link

Auto reconnect #13

Closed wds4 closed 1 year ago

wds4 commented 1 year ago

Trigger an automatic reconnection attempt whenever a relay disconnects. By default, this functionality is off. Activate it using autoReconnect like this:

<NostrProvider relayUrls={relayUrls} debug={true} autoReconnect={true} >
  <App />
</NostrProvider>

I'm wondering whether I should move the logic for triggering the reconnection attempt to OnDisconnectFunc. Let me know if that would make more sense. We might want to make the logic more complex, e.g. stop attempting after N failed attempts. See this issue for my longer discussion.

t4t5 commented 1 year ago

This is great, thanks for the PR! 🙌

We probably want to have a default max number of retries to prevent infinite loops too.

One thing I'm not sure about is whether onDisconnect should be called every time a disconnect happens (even if it successfully reconnects shortly after), or if it should only be called once the max retries have failed (so that the client can show an error message).

I think I'm leaning towards the latter, what are your thoughts?

wds4 commented 1 year ago

Are you wondering when to call onDisconnectCallback within the relay.on("disconnect", () => { block? Perhaps we could call onDisconnectCallback after each individual disconnect (as it does now) but make a new function called something like onMaxDisconnectsCallback after the max retries have been reached?

(I'm still new with typescript so bear with me if I seem to be speaking gibberish at any point! lol)

wds4 commented 1 year ago

We could include a parameter like this:

<NostrProvider relayUrls={relayUrls} debug={true} autoReconnect={true} reconnectAttemptLimit={reconnectAttemptLimit} >
  <App />
</NostrProvider>

btw we will need a way to keep track of disconnects and other relay stats, along the lines of this.

wds4 commented 1 year ago

I'm closing this pull request, as I find that onDisconnectCallback as described by @aussedatlo here solves the issue of auto reconnect, and has the added benefit that it will be straightforward for relay-specific stats like number of disconnects to be managed completely by the client without relying on nostr-react to do it.