Back

Real Time Subscription

  • 0
  • Flutter
waheedoof
24 Dec, 2023, 11:01

when my internet is cut and reconnect, the subscription is changed and i do not get the updates how can i reconnect to the subscription or how can i close it

TL;DR
The user is asking how to reconnect or close a subscription when their internet connection is lost and then reestablished. One solution is to add an EventListener on the window object for the 'offline' event to detect when the connection is lost. When this event triggers, the user can call the `unsubscribe()` function to close the subscription. To reconnect, the user can listen for the 'online' event and either reload the page or recreate the subscription.
ideclon
24 Dec, 2023, 17:54

You could add an EventListener on window for offline to detect when you lose connection. You can then call the unsubscribe() function to close the connection.

ideclon
24 Dec, 2023, 17:55

Then you can listen for online again, and on reconnect, either reload the page, or recreate the subscribe.

Mosh Ontong
25 Dec, 2023, 02:12

I think he using flutter for this issue because base on the tagged its flutter indication

Mosh Ontong
25 Dec, 2023, 02:15

You can still adapt the procedure of @ideclon in flutter but that depends on what state management your app used. In instance, if riverpod you can actually use the AutoDisposeAsyncNotifier here. and call the ref.invalidate to refresh the provider

ideclon
25 Dec, 2023, 02:16

Whoops, sorry! Should have checked the tags!

Mosh Ontong
25 Dec, 2023, 02:17
TypeScript

final consultationRolesControllerProvider =
    AutoDisposeAsyncNotifierProvider<ConsultationRolesController, List<Role>>(
        () => throw UnimplementedError());

class ConsultationRolesController extends AutoDisposeAsyncNotifier<List<Role>> {
  @override
  FutureOr<List<Role>> build() async {
    state = const AsyncValue.loading();

    ref.onDispose(() {
      _subscription?.cancel();
    });

    final consultId = ref.read(consultationdIdProvider);

    final roles = await ref.read(consultationRepoProvider).getRoles(queries: [
      Query.equal("consultationRef", consultId),
    ]);

    roles.removeWhere((element) => element.name == "owner");

    subscribe();

    return roles;
  }

  StreamSubscription<ConsultationRealtime<ConsultationRole>>? _subscription;

  void subscribe() {
    _subscription = ref
        .read(consultationRepoProvider)
        .getConsultationRoleStream(
          Realtime(AppwriteClient.instance.client),
        )
        .listen((roles) => _rolesListener(roles),
            onError: _rolesErrorListener, cancelOnError: true);
  }

  void _rolesErrorListener(Object error, StackTrace stackTrace) {
    state = AsyncValue.error(error, stackTrace);
  }

  void _rolesListener(ConsultationRealtime<ConsultationRole> roles) {
    if (roles.type == ConsultationRealtimeType.create) {
      final merge = [...state.value!, roles.data];
      state = AsyncData(merge);
    } else if (roles.type == ConsultationRealtimeType.delete) {
      final merge = [...state.value!];
      merge.removeWhere((element) => element.id == roles.data.id);
      state = AsyncData(merge);
    } else if (roles.type == ConsultationRealtimeType.update) {
      final merge = [...state.value!];
      final index = merge.indexWhere((element) => element.id == roles.data.id);
      merge[index] = roles.data;
      state = AsyncData(merge);
    }
  }
Mosh Ontong
25 Dec, 2023, 02:18

As you can see in build method we have

TypeScript
    ref.onDispose(() {
      _subscription?.cancel();
    });

Now if you trigger this ref.invalidate(consultationRolesControllerProvider) it will refresh your provider and that ref.onDispose will also be triggered. And that Happen your provider will be refreshed

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more