After creating a new channel by sending message, it doesn't show up after subscribing to it immediately.
Closed this issue · 6 comments
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?
Hello! Can you share any example code?
Hi, I have used bloc pattern for the code.
What is happening is that ->
- I have a set of channels - {2c9a3aa4-0017-49a5-8f07-6c374*******, 10000****-10000****}
- 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.
- 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?
When you add a new channel, are you creating a new subscription? Or only adding one to the set?
Only adding one to the set
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:
- Once you want to add a new channel, first you need to dispose the existing subscription by calling
await subscription.cancel();
. - Create new subscription with modified channels.
Okay, thanks. I think it would be helpful if that was also specified in the docs.