GetStream / stream-video-flutter

Flutter Video SDK - Build your own video app experience using Dart, Flutter and the Stream Video Messaging API.
https://getstream.io/video/docs/flutter/
Other
37 stars 11 forks source link

Websocket connection stays alive when StreamVideo client is created in a background isolate #647

Closed TinhHuynh closed 4 weeks ago

TinhHuynh commented 1 month ago

Hi,

We create a StreamVideo client in a background isolate and let it handle RemoteMessage from FCM then call StreamVideo.reset(). However, StreamVideo.reset() resets the singleton instance to null without clearing any resource: WebSocket connection, pushNotificationManager..... This will lead to the case that the associating WebSocket is still alive, and the user will have multiple connections simultaneously.

Do these connections count toward the Maximum Concurrent Connection limit in our plan? If so, could you provide any guidance to close the WebSocket connection before resetting the singleton client?

StreamChatClient has the method dispose() which I am using to dispose of the chat client when it finishes handling FCM messages, it would be helpful if StreamVideo had such a method.

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  // Initialise Firebase
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  // Initialise the app.

  try {
    // Check if the user is not logged in.
   ....

    // Initialise the video client.
     final streamVideoClient = StreamVideo(
      ...
  );
    // Handle the message.
    await _handleRemoteMessage(message);
  } catch (e, stk) {
    debugPrint('Error handling remote message: $e');
    debugPrint(stk.toString());
  }

  return StreamVideo.reset();
}

Thank you in advance!

Brazol commented 1 month ago

Hey @TinhHuynh, thanks for reaching out.

StreamVideo.reset(disconnect: true) would do most of what you are asking for. But we were indeed missing a proper dispose method that would also dispose PushNotificationManager. Added it here

TinhHuynh commented 1 month ago

Hi @Brazol StreamVideo.reset(disconnect: true) will unregister the user's push token by calling pushNotificationManager?.unregisterDevice(); in _disconnect(). Since that, the user cannot receive further push notifications from FCM.

Brazol commented 1 month ago

You are of course right, dispose makes even more sense now

TinhHuynh commented 1 month ago

Thanks for your support, the PR looks good!

TinhHuynh commented 1 month ago

Hi @Brazol May I know when the PR will be merged and released in the next version? This is crucial for us.

Brazol commented 4 weeks ago

Closed with https://github.com/GetStream/stream-video-flutter/pull/648, Release in v0.3.7