-
-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Better stream and access token management #1019
Conversation
Hey @Vinzent03 This PR looks nice and maybe could also be the solution of our problem #674 . Thanks 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this. This looks awesome. I'm a bit swamped today and maybe tomorrow, but let me take a look whenever I can.
@Vinzent03 BTW, have you actually run this code on iOS and Android? I tried to fix some of the realtime issues and found that some behaviors differ on the web sockets between those two OSs. I don't remember what exactly those differences were, but wanted to ask if you have tested on actual apps. |
I have no way to test this on any Apple device. I tested this on my android device and as a linux application. The stopping of refreshing the access token was especially an issue in the linux application, because the realtime connection keeps being open, when the app is in background, because there is no throttling after some time. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
I started the Application on macOS, closed my MacBook and went home. When I opened it at home I saw:
This indicates a loss of internet connection, as expected. Also the StreamBuilder printed out This is an improvement! If it could auto reconnect would be even better. But that's something I could work with. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The core issue with the realtime currently is the fact that the SDK isn't able to reconnect in the following scenario:
- app is connected to realtime
- realtime is disconnected (bad network or app goes to the background) long enough for the JWT to expire
- app comes back online
At step 3 above, the connection should be restored, and that is the behavior of the js SDK. I have spend some time trying to fix the issue, but have had no success on this SDK, but if we could fix that, all the other issues should be fixed fairly straight forward.
980f368
to
7dc9dcb
Compare
7dc9dcb
to
7a018dd
Compare
I've done now quite more changes.
Your comment should be working now as expected. I've been testing this on android, linux and web. |
I consistently see the SDK fail to reconnect when the above happens well within the JWT's expiration time, is there another explanation for that addressed here? |
Thanks @Vinzent03 for your amazing work. I made a few tests where I had the application open for hours during the day or over night. The stream still received the changes after that, every time. This is huge! Nevertheless, I got quite a lot of Exceptions in the log. Mostly:
What I haven't tested yet is what happens when changes occur while the application is paused. Whether they are reliably loaded when resuming. |
@themightychris Are you sure you've used the latest state of this pr? For me and looks like for @maxfornacon it works fine. @maxfornacon These errors are actually expected, because the websocket connection fails when loosing internet or the app is in background. I think it's better to add them to the |
@Vinzent03 I have not tested this PR in a real environment yet, I was just responding to the description of the problem that seemed to suggest JWT expiration being the root of it, which would make #674 a separate issue—I came to this PR from experiencing #674 looking if the "better stream management" end of this work could solve for that too |
@themightychris Depends on the exact issue you are facing, but hopefully this pr should address many cases, and I think #674 as well. |
It works for a long time, but eventually the stream gets closed.
and
Yes, but there are a lot of the exceptions, but the stream seams to work fine despite of them. Wouldn't handling them cause more harm than good? Handling the above mentioned exceptions would be fine, because then the stream is really closed and it makes sense to show it to the user to manually reload or implement some kind of auto refresh. |
The heartbeat exception is thrown from the client side, because the server doesn't answer or similar. But this shouldn't cause the stream to be closed. Adding exceptions to the stream doesn't mean it's closed. The connection of the realtime client may be interrupted, but the stream shouldn't be closed yet. You can determine when a stream is closed by the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just had a little comment, but looks awesome!
What kind of change does this PR introduce?
Bug fix
What is the current behavior?
The behavior of the app in background depends on the platform, but by always stopping the access token refreshing, the realtime connection may keep an invalid access token and therefore the connection get closed by server. When the realtime connection gets interrupted, it gets automatically reconnected as long as it never sent some expired access token.
What is the new behavior?
When a realtime channel is open, the access token keeps refreshing while the app is in background.In addition, I did more management to keep realtiem and postgrest in sync for the stream method. If the postgrest request fails, I unsubscribe the realtime connection and close the stream as I see no benefit in leaving it open, as it would just be a realtime connection by then. The dev using the stream method should handle such cases. One could also argue about doing a few retries as well, though.
When the realtime connection gets a channel error, but gets resubscribed, I reload the postgrest data to ensure the data is up to date and didn't miss any updates due to the interrupted realtime connection.
Additional context
#1012 #705 #579