pubnub / dart

PubNub Dart SDK
Other
27 stars 15 forks source link

After creating a new channel by sending message, it doesn't show up after subscribing to it immediately. #59

Closed rahulrmishra closed 3 years ago

rahulrmishra commented 3 years ago

I messaged "Hey" to a channel id - "AB" which created the channel, and I subscribed to it immediately, but the message doesn't show up. Any workaround for this?

are commented 3 years ago

Hello! Can you share any example code?

rahulrmishra commented 3 years ago

Hi, I have used bloc pattern for the code.

What is happening is that ->

1) I have a set of channels - {2c9a3aa4-0017-49a5-8f07-6c374***, 10000-10000} 2) I listen to their subscription in the constructor of my bloc

ChatBloc({ @required this.pubnub, @required this.directChanneId, @required this.subscription, }) : super(ChatMessagesInitialState()) { subscription.messages.listen((message) { // CODE TO HANDLE THE MESSAGES }); }

At this point everything is fine.

3) When I add a new channel, {2c9a3aa4--49a5--6c374***, 10000-10000, 1000-100}

The listener doesn't listen to the messages. Instead whenever I add a new channel, I again have to set the listener again for it to work.

subscription.messages.listen((message) { // CODE TO HANDLE THE MESSAGES });

I'm not sure if this is how it should work?

are commented 3 years ago

When you add a new channel, are you creating a new subscription? Or only adding one to the set?

rahulrmishra commented 3 years ago

Only adding one to the set

are commented 3 years ago

Because Subscription objects are immutable, you cannot modify channels after its created. Each time you want to change the subscription, you should recreate it instead:

  1. Once you want to add a new channel, first you need to dispose the existing subscription by calling await subscription.cancel();.
  2. Create new subscription with modified channels.
rahulrmishra commented 3 years ago

Okay, thanks. I think it would be helpful if that was also specified in the docs.