pubnub / dart

PubNub Dart SDK
Other
27 stars 15 forks source link

Multiple Channels subscribe not working. #56

Closed mehtahardikr closed 3 years ago

mehtahardikr commented 3 years ago

Trying to subscribe channels in list not working.

 var channelList = [];
 var pubnub;
 Subscription subscription;

 pubnub = PubNub(
       defaultKeyset:
       Keyset(subscribeKey: demo', publishKey: 'demo',));

 channelList.add('test');

       subscription = pubnub.subscribe(channels: {channelList.join(",")} ,  withPresence: true);
       debugPrint('${subscription.channels}');

      Future.delayed(Duration(seconds: 30), () async{
        channelList.add('test2');

        subscription = await pubnub.subscribe(
            channels: {channelList.join(",")} );

        debugPrint('${subscription.channels}');

        subscription?.messages?.listen((message) {
          print(message);
        });

      });

if I subscribe one by one then

await  pubnub.subscribe(channels: {'test'} ,  withPresence: true);
await  pubnub.subscribe(channels: {'test1'} ,  withPresence: true);

then it returns messages which are queued for 'test1' channel , seems to be weired issue.

 flutter doctor -v
[✓] Flutter (Channel stable, 2.2.2, on Linux, locale en_IN)
    • Flutter version 2.2.2 at /media/hardik/Storage/extra/flutterSdk/flutter
    • Framework revision d79295af24 (3 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.3)
    • Android SDK at /home/hardik/Android/Sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.8+0-b944-P17168821)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Android Studio (version 4.2)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 58.0.1
    • Dart plugin version 202.8531
    • Java version OpenJDK Runtime Environment (build 11.0.8+0-b944-P17168821)

[✓] VS Code (version 1.57.1)
    • VS Code at /usr/share/code
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
    • SM T380 (mobile) • 95a8b2cb35b62dd9 • android-arm    • Android 8.1.0 (API 27)
    • Chrome (web)     • chrome           • web-javascript • Google Chrome 91.0.4472.114

• No issues found!
are commented 3 years ago

Hi! Each pubnub.subscribe call returns a new subscription. After you create a subscription you cannot change it, so if you want to add a new channel to the subscription you either need to create a separate subscription, or .cancel() the previous one.

In each subscription you will only receive messages from the channels that were in the original subscription.

Additionally, you shouldn't do channelList.join(','). Instead you should pass in your channelList as such pubnub.subscribe(channels: channelList.toSet(), ...).

Edit: Here is a solution that you can evaluate for your use-case if you need to use multiple subscriptions: https://github.com/pubnub/dart/issues/44#issuecomment-825053295

mehtahardikr commented 3 years ago

@are : okay fine, let me check like this. thank you.