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

StreamChat dispose() tears down the Client connection #4

Closed
isaiahtaylorhh opened this issue Mar 6, 2020 · 1 comment
Closed

StreamChat dispose() tears down the Client connection #4

isaiahtaylorhh opened this issue Mar 6, 2020 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@isaiahtaylorhh
Copy link

Hello, my team is evaluating Stream and I am very happy with the Flutter and React UI Kits so far. The flutter kit particularly was the big differentiator for us. Kudos!

One problem I was having was that tabbing away from the chat window using BottomNavigationBar tabs caused the Client object to get torn down and the connection to get disconnected, so that when I navigate back to it again I get errors that it has been disconnected.

This is despite the fact that my Client is initialized before runApp .

Root Cause

I found that StreamChat.dispose() calls widget.client.dispose(). This effectively ties the lifecycle of the Client object to the lifecycle of a widget that uses it. From a memory perspective this is not saving anything; in the Stream documentation it is recommended that the Client be created at the top and persist through the lifecycle of the entire app, so it doesn't make sense to dispose of that top level Client object when disposing of a widget that makes use of it.

Workaround

If anyone is experiencing a similar issue, I worked around it by extending the Client class and overriding the dispose method to do nothing:

import 'package:stream_chat_flutter/stream_chat_flutter.dart';
import 'package:dio/dio.dart';

class CustomClient extends Client {
  // Get this from the actual Client source code
  static const _defaultBaseURL = "chat-us-east-1.stream-io-api.com";

  CustomClient(
    apiKey, {
    tokenProvider,
    baseURL = _defaultBaseURL,
    logLevel = Level.WARNING,
    logHandlerFunction,
    Duration connectTimeout = const Duration(seconds: 6),
    Duration receiveTimeout = const Duration(seconds: 6),
    Dio httpClient,
  }) : super(apiKey,
            tokenProvider: tokenProvider,
            baseURL: baseURL,
            logLevel: logLevel,
            logHandlerFunction: logHandlerFunction,
            connectTimeout: connectTimeout,
            receiveTimeout: receiveTimeout,
            httpClient: httpClient);

  @override
  void dispose() {
    // Don't do anything so the connection doesn't get torn down
  }
}

Then, simply use this class rather than Client at the top of your app:

final client = CustomClient(
    'XXX',
    logLevel: Level.INFO,
  );

After this, the StreamChat widget will get torn down and restored as you tab back and forth, but the Client will survive it.

@imtoori
Copy link
Contributor

imtoori commented Mar 13, 2020

Hi @isaiahtaylor
You are right, that is not the right behaviour.
I'll update the StreamChat component not to dispose the Client automatically

@imtoori imtoori added the bug Something isn't working label Mar 13, 2020
@imtoori imtoori self-assigned this Mar 13, 2020
@imtoori imtoori closed this as completed Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants