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

Cannot connect on stream_chat_flutter: ^2.0.0-nullsafety.8 #524

Closed
2 of 6 tasks
TramPamPam opened this issue Jul 6, 2021 · 20 comments · Fixed by #548
Closed
2 of 6 tasks

Cannot connect on stream_chat_flutter: ^2.0.0-nullsafety.8 #524

TramPamPam opened this issue Jul 6, 2021 · 20 comments · Fixed by #548
Labels

Comments

@TramPamPam
Copy link

Describe the bug
I receive an error after call connectUser(User user, String token)

What package are you using? What version?
stream_chat_flutter: ^2.0.0-nullsafety.8

What platform is it about?

  • Android
  • iOS
  • Web
  • Windows
  • MacOS
  • Linux

To Reproduce
Steps to reproduce the behaviour:

  1. connectUser(User user, String token)
  2. See error
    flutter: WebSocketError(message: You've called connect twice,
    can only attempt 1 connection at the time,
    )

Expected behaviour
Should connect to getstream

Screenshots
If applicable, add screenshots to help explain your problem.

**Logs **
#0 WebSocket.connect (package:stream_chat/src/ws/websocket.dart:175:7)
#1 StreamChatClient.openConnection (package:stream_chat/src/client/client.dart:330:24)
#2 StreamChatCoreState._onForeground (package:stream_chat_flutter_core/src/stream_chat_core.dart:178:14)
#3 StreamChatCoreState.didChangeAppLifecycleState (package:stream_chat_flutter_core/src/stream_chat_core.dart:165:9)
#4 WidgetsBinding.handleAppLifecycleStateChanged (package:flutter/src/widgets/binding.dart:692:16)
#5 ServicesBinding._handleLifecycleMessage (package:flutter/src/services/binding.dart:192:5)
#6 BasicMessageChannel.setMessageHandler. (package:flutter/src/services/platform_channel.dart:73:49)
#7 BasicMessageChannel.setMessageHandler. (package:flutter/src/services/platform_channel.dart:72:47)
#8 _DefaultBinaryMessenger.handlePlatformMessage (package:flutter/src/services/binding.dart:284:33)
#9 _invoke3. (dart:ui/hooks.dart:223:15)
#10 _rootRun (dart:async/zone.dart:1354:13)
#11 _CustomZone.run (dart:async/zone.dart:1258:19)
#12 _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
#13 _invoke3 (dart:ui/hooks.dart:222:10)
#14 PlatformDispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:520:7)
#15 _dispatchPlatformMessage (dart:ui/hooks.dart:90:31)

Paste the output of running flutter doctor -v here.

[✓] Flutter (Channel stable, 2.2.2, on Mac OS X 10.15.7 19H2 darwin-x64, locale en-UA)
• Flutter version 2.2.2 at /Users/user/flutter
• Framework revision d79295af24 (4 weeks ago), 2021-06-11 08:56:01 -0700
• Engine revision 91c9fc8fe0
• Dart version 2.13.3

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at /Users/user/Library/Android/sdk
• Platform android-30, build-tools 30.0.2
• ANDROID_HOME = /Users/user/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
✗ Android license status unknown.
Run flutter doctor --android-licenses to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.3, Build version 12C33
• CocoaPods version 1.10.1

[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)

[✓] VS Code (version 1.49.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.16.0

[✓] Connected device (2 available)
• iPhone 8 (mobile) • 36DDD3C7-8191-46B9-BC88-6FC909DEA1F5 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
• Chrome (web) • chrome • web-javascript • Google Chrome 91.0.4472.114

Additional context
Add any other context about the problem here.

@TramPamPam TramPamPam added the bug Something isn't working label Jul 6, 2021
@imtoori
Copy link
Contributor

imtoori commented Jul 7, 2021

Hi @TramPamPam
when are you calling connectUser?
It looks like the StreamChat widget is calling connectUser after the app goes in foreground from background and you're calling connectUser too. That's why there are two calls to connectUser

@TramPamPam
Copy link
Author

Hi @imtoori

We have system popup where we ask for some permissions that are required by our app logic while we connecting user to GetStream.

It's right after we retrieved a token for user, that we use for connect method.

Why does StreamChat tries to connect before we use connect user manually?

@imtoori
Copy link
Contributor

imtoori commented Jul 7, 2021

ok apparently the system pop up makes the StreamChat widget think it's in background and when you close it it comes back to foreground
we check that the client.wsConnectionStatus is disconnected before opening the connection (you can see this here)

Can you enable the client logs with info level and paste it here?

@TramPamPam
Copy link
Author

TramPamPam commented Jul 13, 2021

@imtoori
here are the logs

Launching lib/main.dart on iPhone 12 in debug mode...
Running pod install...
Running Xcode build...
Xcode build done.                                           138.5s
Debug service listening on ws://127.0.0.1:58451/UDJeil3MOB0=/ws
Syncing files to device iPhone 12...
flutter: 2021-07-13 12:44:39.860158 ℹ️ 📡 Initiating new StreamChatClient
flutter: *** Request ***
flutter: uri: https://api/v1/user/auth/phone-login
flutter: method: POST
flutter: responseType: ResponseType.json
flutter: followRedirects: true
flutter: connectTimeout: 10000
flutter: sendTimeout: 0
flutter: receiveTimeout: 0
flutter: receiveDataWhenStatusError: true
flutter: extra: {}
flutter: headers:
flutter:  content-type: application/json; charset=utf-8
flutter: data:
flutter: {phoneNumber: +************}
flutter:
flutter: *** Response ***
flutter: uri: https://api/v1/user/auth/phone-login
flutter: statusCode: 204
flutter: headers:
flutter:  x-cloud-trace-context: 047d1d94299542aef2a7ae5530e57a69;o=1
flutter:  content-type: text/html
flutter:  x-powered-by: Express
flutter:  date: Tue, 13 Jul 2021 09:44:53 GMT
flutter:  content-length: 0
flutter:  server: Google Frontend
flutter: Response Text:
flutter:
flutter:
flutter: 204 -
flutter: *** Request ***
flutter: uri: https://api/v1/user/auth/phone-verify
flutter: method: POST
flutter: responseType: ResponseType.json
flutter: followRedirects: true
flutter: connectTimeout: 10000
flutter: sendTimeout: 0
flutter: receiveTimeout: 0
flutter: receiveDataWhenStatusError: true
flutter: extra: {}
flutter: headers:
flutter:  content-type: application/json; charset=utf-8
flutter: data:
flutter: {phoneNumber: +*********, code: ****}
flutter:
flutter: *** Response ***
flutter: uri: https://api/v1/user/auth/phone-verify
flutter: statusCode: 201
flutter: headers:
flutter:  x-cloud-trace-context: 3713fae6530d0e8c256063110de795bc;o=1
flutter:  x-powered-by: Express
flutter:  date: Tue, 13 Jul 2021 09:46:09 GMT
flutter:  content-length: 868
flutter:  etag: W/"364-SuCF1azUAcwraJ0Em+qMFbWrKJg"
flutter:  content-type: application/json; charset=utf-8
flutter:  server: Google Frontend
flutter: Response Text:
flutter: {"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwaG9uZU51bWJlciI6IiszODA5MzI3OTcwNzUiLCJpYXQiOjE2MjYxNjk1Njl9.6nwMFR3LKC9QJS5LZbNT7IVjg3XzgeJjXpltEpXvPT8"}
flutter:
flutter: 2021-07-13 12:46:09.619953 ℹ️ 📡 connecting user : 26007486-9067-43dd-a950-8143c91133c4
flutter: 2021-07-13 12:46:09.638537 ℹ️ 📡 Opening web-socket connection for 26007486-9067-43dd-a950-8143c91133c4
flutter: 2021-07-13 12:46:09.657738 ℹ️ 🔌 Initiating connection with chat.stream-io-api.com
flutter: 2021-07-13 12:46:09.666279 ℹ️ 🔌 Started listening to chat.stream-io-api.com
flutter: 2021-07-13 12:46:09.673810 ℹ️ 📡 Closing web-socket connection for 26007486-9067-43dd-a950-8143c91133c4
flutter: 2021-07-13 12:46:09.676239 ℹ️ 🔌 Disconnecting web-socket connection
flutter: 2021-07-13 12:46:09.676705 ℹ️ 🔌 Stopped monitoring events
flutter: 2021-07-13 12:46:09.678176 ℹ️ 🔌 Closing connection with chat.stream-io-api.com
flutter: 2021-07-13 12:46:09.678768 ℹ️ 🔌 Stopped listening to chat.stream-io-api.com
flutter: *** Request ***
flutter: uri: https://api/v1/device
flutter: method: POST
flutter: responseType: ResponseType.json
flutter: followRedirects: true
flutter: connectTimeout: 10000
flutter: sendTimeout: 0
flutter: receiveTimeout: 0
flutter: receiveDataWhenStatusError: true
flutter: extra: {}
flutter: headers:
flutter:  content-type: application/json; charset=utf-8
flutter:  Authorization: Bearer ***
flutter: data:
flutter: {}
flutter:
flutter: 2021-07-13 12:46:11.522373 ℹ️ 📡 Opening web-socket connection for 26007486-9067-43dd-a950-8143c91133c4
flutter: 2021-07-13 12:46:11.525574 🚨 📡 error connecting ws
flutter: WebSocketError(message:         You've called connect twice,
        can only attempt 1 connection at the time,
        )
flutter: #0      WebSocket.connect (package:stream_chat/src/ws/websocket.dart:175:7)
#1      StreamChatClient.openConnection (package:stream_chat/src/client/client.dart:330:24)
#2      StreamChatCoreState._onForeground (package:stream_chat_flutter_core/src/stream_chat_core.dart:178:14)
#3      StreamChatCoreState.didChangeAppLifecycleState (package:stream_chat_flutter_core/src/stream_chat_core.dart:165:9)
#4      WidgetsBinding.handleAppLifecycleStateChanged (package:flutter/src/widgets/binding.dart:692:16)
#5      ServicesBinding._handleLifecycleMessage (package:flutter/src/services/binding.dart:192:5)
#6      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:73:49)
#7      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:72:47)
#8      _DefaultBinaryMessenger.handlePlatformMessage (package:flutter/src/services/binding.dart:284:33)
#9      _invoke3.<anonymous closure> (dart:<…>
[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: WebSocketError(message:         You've called connect twice,
        can only attempt 1 connection at the time,
        )
#0      WebSocket.connect (package:stream_chat/src/ws/websocket.dart:175:7)
#1      StreamChatClient.openConnection (package:stream_chat/src/client/client.dart:330:24)
#2      StreamChatCoreState._onForeground (package:stream_chat_flutter_core/src/stream_chat_core.dart:178:14)
#3      StreamChatCoreState.didChangeAppLifecycleState (package:stream_chat_flutter_core/src/stream_chat_core.dart:165:9)
#4      WidgetsBinding.handleAppLifecycleStateChanged (package:flutter/src/widgets/binding.dart:692:16)
#5      ServicesBinding._handleLifecycleMessage (package:flutter/src/services/binding.dart:192:5)
#6      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:73:49)
#7      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channe<…>
flutter: *** Response ***
flutter: uri: https://api/v1/device
flutter: statusCode: 201
flutter: headers:
flutter:  x-cloud-trace-context: 1f032dcff7168300bef2fd95d47a608f
flutter:  x-powered-by: Express
flutter:  date: Tue, 13 Jul 2021 09:46:11 GMT
flutter:  content-length: 334
flutter:  etag: W/"14e-2S/tk2AJ/iHzInm1q5hAjXYVSqc"
flutter:  content-type: application/json; charset=utf-8
flutter:  server: Google Frontend
flutter: Response Text:
flutter: {}
flutter:
flutter: platform: ios

@imtoori
Copy link
Contributor

imtoori commented Jul 13, 2021

Hi @TramPamPam do you have the StreamChat widget on top of the Navigator/MaterialApp?
How are you using it?

@TramPamPam
Copy link
Author

TramPamPam commented Jul 13, 2021

Hi @imtoori

Like this:

 static late StreamChatClient client = StreamChatClient(
    EnvironmentConfig.GETSTREAM_PROJECT_ID,
    baseURL:
        'chat.stream-io-api.com', 
    logLevel: Level.INFO,
  )..chatPersistenceClient = StreamChatPersistenceClient(
    logLevel: Level.INFO,
    connectionMode: ConnectionMode.regular,
  );

MaterialApp(   
      builder: (context, child) => StreamChat(
        streamChatThemeData: StreamChatThemeData.fromTheme(baseTheme),
        client: ChatService.client,
        child: appOverlayBuilder(context, child), //TransitionBuilder
      ),
    );

@imtoori
Copy link
Contributor

imtoori commented Jul 16, 2021

the merge pr should fix the issue
let us know if that works
you'll find it in the next release

@akul1994
Copy link

This issue seems to have resurfaced in 3.3.2,
Can you please check?

@imtoori
Copy link
Contributor

imtoori commented Dec 17, 2021

Hi @akul1994 can you be more specific? I tested this behaviour and it's working fine for me

@akul1994
Copy link

In my app, when the user logs out, I call stream disconnect, and post login and make another call to connectUser with new user credentials.

I put up a listener wsConnectionStatusStream and noticed that after I disconnect User and take him to the login page where my Google Login popup is shown, I'm getting connecting as a status in the connectionStatusStream even though I have not made any calls to connectUser,

As a result, after login when I make a call to connectUser I get an exception saying connect called twice.

@akul1994
Copy link

As a workaround, if after logout I reinstantiate the StreamClient object, I don't face this issue, but doing that is causing a few other things to break

@imtoori
Copy link
Contributor

imtoori commented Dec 17, 2021

@akul1994 I'm testing this using our sample app and it's working fine
How do you disconnect the user?

@akul1994
Copy link

akul1994 commented Dec 17, 2021

    if (client == null) return;
    try {
      await client?.disconnectUser(flushChatPersistence: true);
      client?.removeDevice(PrefRetreiver.fcmToken);
    } catch (e, s) {}
    return;
  }

Like this.

So here are my findings,

  1. After logout if I don't open google popup it doesn't move to a connecting state, as soon as I do it shows a connecting state
  2. In my previous implementation I was awaiting on Stream Connect to proceed from the Splash Screen, However in my current implementation I let it happen async and let the user proceed on the app, I hide the UI components that need StreamConnect to happen till I get a succesful connectUser callback.
    In my previous implementation where I was awaiting connectUser I'm not facing this issue, but in my new flow I am. Could this be related to a context in any way?

@akul1994
Copy link

@imtoori Any update on this?

@imtoori
Copy link
Contributor

imtoori commented Dec 20, 2021

hey @akul1994
can you show me how do you're using the StreamChat widget?
Sorry, I'm trying to understand what's going on, this is very weird

@akul1994
Copy link

akul1994 commented Dec 20, 2021

In m main.dart ,
Like this:

StreamChat(
        streamChatThemeData: StreamThemesHelper.getStreamTheme(context),
        client: StreamHelper.client!,
        child: MaterialApp(
          navigatorKey: navigatorKey,
          title: 'threedots',
          theme: AppThemes.appTheme,
          onGenerateRoute: NavigationHelper.generateRoute,
          onGenerateInitialRoutes: (String initialRouteName) {
            return [MaterialPageRoute(builder: (_) => LoginScreen())];
          },
          navigatorObservers: [
            EventsHelper.observer!,
          ],
        ),
      ),
      ```

@akul1994
Copy link

akul1994 commented Jan 7, 2022

Hey @imtoori any update on this, this is happening on every login, logout of the app,
socket moves to disconnected state after logout and as soon as a google popup is shown it moves to a connecting state even without calling connect causing a failure when I actually call connect

@imtoori
Copy link
Contributor

imtoori commented Jan 10, 2022

Hi @akul1994 I'm sorry but I was out sick
Could you create a sample app for this issue?
I tried reproducing this and I failed. Having a common code could help solve the issue, what do you think?

@imtoori imtoori reopened this Jan 10, 2022
@akul1994
Copy link

Sure, I'll get something setup and get back to you.

As a workaround,
If I reinitialise the stream client after logout, it does not go to connecting state once the google popup shows,

@no-response
Copy link

no-response bot commented Feb 25, 2022

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away! Thanks for your contribution.

@no-response no-response bot closed this as completed Feb 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants