pubnub / dart

PubNub Dart SDK
Other
27 stars 15 forks source link

PubNub getting recent old messages when subscribe to channel #57

Closed mehtahardikr closed 2 years ago

mehtahardikr commented 3 years ago

Steps to replicate :

  1. Subscribe channelA
  2. From pubnub console send messages to channelB
  3. Subscribe channelB from App.
  4. After subscribe to channelB , its getting old recent messages.

Here are the logs of flutter side.

I/flutter (10330): subscribe to  channelA
I/flutter (10330):  channel  : {channelA}
I/flutter (10330): [2021-07-08 01:15:31.660278] (silly) myLogger.pubnub.subscribe.subscribe_loop: State has been updated.
I/flutter (10330): [2021-07-08 01:15:31.716721] (verbose) myLogger.pubnub.subscription.subscription: Resuming subscription.
I/flutter (10330): [2021-07-08 01:15:31.722084] (silly) myLogger.pubnub.subscribe.subscribe_loop: State has been updated.
I/flutter (10330): [2021-07-08 01:15:32.570059] (silly) myLogger.pubnub.subscribe.subscribe_loop: Starting new loop iteration.
I/flutter (10330):  _subscription  : Instance of 'Subscription'
I/flutter (10330): [2021-07-08 01:15:32.680998] (info) myLogger.pubnub.networking.request_handler: (0) Preparing request.
I/flutter (10330): [2021-07-08 01:15:32.723082] (info) myLogger.pubnub.networking.request_handler: (0) Starting request to "https://ps.pndsn.com/v2/subscribe/demo/channelA,channelA-pnpres/0?pnsdk=PubNub-Dart%2F3.2.0&tt=0"...
I/flutter (10330): [2021-07-08 01:15:34.482467] (info) myLogger.pubnub.networking.request_handler: (0) Request succeed!
I/flutter (10330): [2021-07-08 01:15:34.531426] (info) myLogger.pubnub.networking.request_handler: (0) Resource released...
I/flutter (10330): [2021-07-08 01:15:34.557186] (silly) myLogger.pubnub.subscribe.subscribe_loop: Result: timetoken 16257213343005347, new messages: 0
I/flutter (10330): [2021-07-08 01:15:34.578142] (silly) myLogger.pubnub.subscribe.subscribe_loop: Updating the state...
I/flutter (10330): [2021-07-08 01:15:34.580191] (silly) myLogger.pubnub.subscribe.subscribe_loop: State has been updated.
I/flutter (10330): [2021-07-08 01:15:34.601126] (silly) myLogger.pubnub.subscribe.subscribe_loop: Starting new loop iteration.
I/flutter (10330): [2021-07-08 01:15:34.603654] (info) myLogger.pubnub.networking.request_handler: (1) Preparing request.
I/flutter (10330): [2021-07-08 01:15:34.606063] (info) myLogger.pubnub.networking.request_handler: (1) Starting request to "https://ps.pndsn.com/v2/subscribe/demo/channelA,channelA-pnpres/0?pnsdk=PubNub-Dart%2F3.2.0&tt=16257213343005347&tr=7"...
D/ViewRootImpl@936a6be[MainActivity](10330): ViewPostIme pointer 0
D/ViewRootImpl@936a6be[MainActivity](10330): ViewPostIme pointer 1
I/flutter (10330): subscribe to  channelB
I/flutter (10330):  channel  : {channelB}
I/flutter (10330): [2021-07-08 01:17:14.545740] (silly) myLogger.pubnub.subscribe.subscribe_loop: State has been updated.
I/flutter (10330): [2021-07-08 01:17:14.547052] (verbose) myLogger.pubnub.subscription.subscription: Resuming subscription.
I/flutter (10330):  _subscription  : Instance of 'Subscription'
I/flutter (10330): [2021-07-08 01:17:14.565145] (silly) myLogger.pubnub.subscribe.subscribe_loop: Cancelling the handler...
I/flutter (10330): [2021-07-08 01:17:14.568322] (info) myLogger.pubnub.networking.request_handler: (1) Request has been cancelled (reason: Null).
I/flutter (10330): [2021-07-08 01:17:14.579590] (info) myLogger.pubnub.networking.request_handler: (1) Resource released...
I/flutter (10330): [2021-07-08 01:17:14.584224] (silly) myLogger.pubnub.subscribe.subscribe_loop: Starting new loop iteration.
I/flutter (10330): [2021-07-08 01:17:14.586552] (info) myLogger.pubnub.networking.request_handler: (2) Preparing request.
I/flutter (10330): [2021-07-08 01:17:14.588725] (info) myLogger.pubnub.networking.request_handler: (2) Starting request to "https://ps.pndsn.com/v2/subscribe/demo/channelA,channelA-pnpres,channelB,channelB-pnpres/0?pnsdk=PubNub-Dart%2F3.2.0&tt=16257213343005347&tr=7"...
I/flutter (10330): [2021-07-08 01:17:15.551845] (info) myLogger.pubnub.networking.request_handler: (2) Request succeed!
I/flutter (10330): [2021-07-08 01:17:15.554977] (info) myLogger.pubnub.networking.request_handler: (2) Resource released...
I/flutter (10330): [2021-07-08 01:17:15.563970] (silly) myLogger.pubnub.subscribe.subscribe_loop: Result: timetoken 16257213981317760, new messages: 9
I/flutter (10330): {text: PubNub is awesome!}
I/flutter (10330): {text: PubNub is awesome!}
I/flutter (10330): {text: PubNub is awesome!}
I/chatty  (10330): uid=10155(com.example.flutter_app1) 1.ui identical 2 lines
I/flutter (10330): {text: PubNub is awesome!}
I/flutter (10330): [2021-07-08 01:17:15.606317] (silly) myLogger.pubnub.subscribe.subscribe_loop: Updating the state...
I/flutter (10330): [2021-07-08 01:17:15.607364] (silly) myLogger.pubnub.subscribe.subscribe_loop: State has been updated.
I/flutter (10330): [2021-07-08 01:17:15.609440] (silly) myLogger.pubnub.subscribe.subscribe_loop: Starting new loop iteration.
I/flutter (10330): [2021-07-08 01:17:15.611656] (info) myLogger.pubnub.networking.request_handler: (3) Preparing request.
I/flutter (10330): [2021-07-08 01:17:15.613799] (info) myLogger.pubnub.networking.request_handler: (3) Starting request to "https://ps.pndsn.com/v2/subscribe/demo/channelA,channelA-pnpres,channelB,channelB-pnpres/0?pnsdk=PubNub-Dart%2F3.2.0&tt=16257213981317760&tr=7"...

Punub console screenshot

https://pasteboard.co/KaaWttL.png

pubnub lib version : 3.2.0

Following is pseudo code

/// pubnub config

var keySet = Keyset(
      subscribeKey: 'demo',
      publishKey: 'demo'
    );

var _pubnub = PubNub(networking: NetworkingModule(retryPolicy: RetryPolicy.exponential(maxRetries: 10)), defaultKeyset: keySet);

Step 1 :
var _subscription = await _pubnub.subscribe(channels: {'channelA'}, withPresence: true);

Step2 : send message from pubnub console to channelB

Step 3: subscribe to channelB

var _subscription = await _pubnub.subscribe(channels: {'channelB'}, withPresence: true);

/// listen for channel messages
  _subscription.messages.listen((envelope) {
       debugPrint('${envelope.payload}');
      });
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 (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.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!

Let me know if anything missed or any changes require for this.

are commented 3 years ago

Hi, can you let me know what do you mean by "old recent messages"? PubNub stores the messages in a queue for some time if they are not received, how old are the messages that you are receiving?

Also, can you check if the problem persists in the latest 4.0.0-beta.1 version?

mehtahardikr commented 3 years ago

@are : as per check with 4.0.0-beta.1 and its looks good as per github latest commit it solves timetoken related issue but problem is that can you make it available without null safety as with null safety of beta version its breaking my other dependencies as well.

The issue is: the channel B has 2 messages before i join it and upon join that channel B it sends that 2 messages which was sent before joining of channel B.

are commented 3 years ago

Null Safety is backwards compatible, so you can update your Dart version but disable sound Null Safety - this will allow you to use newer dependencies but still have unsound code. Unfortunately we do not plan to backport fixes into the non-null safe versions of Dart.

As with the timing issue it all depends on the PubNub network - all actions are asynchronous so you cannot guarantee the correct order of subscribing and publishing because of many variables like network latency, PoP processing, network congestion and so on.

mehtahardikr commented 3 years ago

@are : As i see that commits, timetoken added as fix for this as with other client sdk has same. and you directly migrated it to nullsafety without releasing its stable version which is currently working. Right now its creating issue with other dependencies also. if you can do that then it will realy helpful to me.