fix(realtime_client): more stable reconnect on web#1471
Open
Vinzent03 wants to merge 9 commits into
Open
Conversation
spydon
reviewed
Jul 2, 2026
| await conn.sink.close(code, reason ?? ''); | ||
| // Add a timeout to close the sink to avoid hanging in case something | ||
| // is wrong with the connection. | ||
| // This safeguard was suggested here: https://github.com/dart-lang/http/issues/1693#issuecomment-2651080004 |
Contributor
There was a problem hiding this comment.
Suggested change
| // This safeguard was suggested here: https://github.com/dart-lang/http/issues/1693#issuecomment-2651080004 |
I think we can skip where it came from
| // The Dart SDK has a timeout of 5 seconds for closing the WebSocket connection, so we set a timeout of 6 seconds here to avoid hanging indefinitely. | ||
| await conn.sink | ||
| .close(code, reason ?? '') | ||
| .timeout(const Duration(seconds: 6), onTimeout: onTimeout); |
Contributor
There was a problem hiding this comment.
Are we sure that they'll always have 5 seconds? 🤔
| await conn.sink.close(); | ||
| await conn.sink | ||
| .close() | ||
| .timeout(const Duration(seconds: 6), onTimeout: onTimeout); |
Contributor
There was a problem hiding this comment.
If we want to keep the 6 seconds, can you create a constant for it so that we don't duplicate it?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Faster disconnect
The previous
disconnectfirst waited for the connection to get stable by waiting forconn.ready. Despite the comment I added on it, I could not find any issues by removing that wait and just close the connection immediately. I tested this manually with the websocket channel package by closing the connection instantaneously and with delays without waiting forconn.readyon web, android and linux. This comment seems to experience the same.During testing on web, I found that by removing the wait for
readythe close is still not instantaneously, but can take several seconds. I think it depends on the exact platform and state of the connection whether this can save time or not.Timeout for connection close
During testing I experienced a real network issue at home, but no reconnect was scheduled. I lost the exact logs, but when researching other issues I came along this comment: dart-lang/http#1693 (comment) which could be the source of the issue. The disconnect hang indefinitely which is why no connect was scheduled. So I added a timeout to the
conn.sink.close. This may cause the disconnect to not wait for a connection that would indeed close after like 10 seconds successfully, but I don't think there is a real issue in handling this via a timeout.Faster Channel rejoin
In particular, I tested a Flutter web app on mobile by switching from the browser to other apps and back. Only
inactiveandhiddenevents are fired on web when switching to another app, which are currently not handled by the app state handler in supabase_flutter. But those events are also called when switching to another tab. But in the case of switching to another tab the websocket connection is kept. When switching to another app that is not the case and the connection is aborted. So the kind of events that are currently handled is correct. There is no way for the app to know by the events whether the connection will stay stable or not. This means we cannot schedule a disconnect as we do for mobile. Due to the errored connection a re-connect is scheduled, which tries to connect for the whole time until the app is reopened.This causes the channel to hang it a loop of rejoining. Depending on the time of a reconnect it can take a lot of time for the next rejoin to kick in. As seen by the following snippet where it took 10s for the channel to start rejoining after the connection is already established.
The new implementation rejoins all errored channels manually on connection open. This can improve the timings a LOT.