Skip to content
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

Postgres Changes don't closes connection on flutter web 'hot reload' #1088

Open
visign3d opened this issue Nov 26, 2024 · 3 comments
Open

Postgres Changes don't closes connection on flutter web 'hot reload' #1088

visign3d opened this issue Nov 26, 2024 · 3 comments
Labels
bug Something isn't working realtime This issue or pull request is related to realtime

Comments

@visign3d
Copy link

After initiating a postgres changes subscription at app start everything works fine.
After hot reload/hot restart seem like the old subscription wasn't 'disposed', and the listener function is called twice.
it happens only on flutter web , android works fine.
I guess its kind a WebSocket problem , but maybe somebody had a similar problem and had a workaround.
The only workaround i found is to refresh browser after hot restart/reload. Its disconnects all previous web sockets.

code example:
RealtimeChannel s = client .channel('public:$classId') .onPostgresChanges( event: PostgresChangeEvent.all, schema: 'public', table: classId, callback: (payload) { var m = payload.newRecord['model']; T cm = fromMap(m); listener(cm); }) .subscribe();

@visign3d visign3d added the bug Something isn't working label Nov 26, 2024
@vad-bal
Copy link

vad-bal commented Dec 10, 2024

Would love to see this fixed as it is quite annoying.

I have found a workaround for now. Basically, just store the instance of the channel in a global variable whenever you subscribe. and clear it when You unsubscribe. Also store a local instance of the channel. In the callback compare the two instances with identical, and if they are not, that means the listener is from the old instance, so just ignore it by returning.

Here is some code:

RealtimeChannel? _channelInstance;

class ChannelListener {
  late RealtimeChannel _channel;

  void subscribe() {
    _channel = SupabaseClient('url', 'anonKey')
        .channel('public.*')
        .onPostgresChanges(
          event: PostgresChangeEvent.update,
          schema: 'public',
          callback: (payload) {
            // Add this to every callback you have:
            if (!identical(_channel, _channelInstance)) {
              return;
            }
            // Your code here
          },
        )
        .subscribe(
      (status, error) {
        if (!identical(_channel, _channelInstance)) {
          return;
        }
      },
    );

    _channelInstance = _channel;
  }

  Future<void> unsubscribe() async {
    _channelInstance = null;
    await _channel.unsubscribe(Duration.zero);
  }
}

This is just a basic implementation, what I just scraped from my project. Maybe it could be done even simpler, but at least it works for me. You may also add some logging call to that if statement if you want to see that it was really catched.

But basically, this is it.

@dshukertjr dshukertjr added the realtime This issue or pull request is related to realtime label Dec 11, 2024
@Vinzent03
Copy link
Collaborator

I've experienced this as well while developing with this packager, but this seems more like a flutter issue. See flutter/flutter#69949 so I'm unsure how this should be handled.

@visign3d
Copy link
Author

visign3d commented Dec 13, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working realtime This issue or pull request is related to realtime
Projects
None yet
Development

No branches or pull requests

4 participants