Rank 2: Process
Hello everyone. What is the correct way to dynamically add a channel to Realtime?
Right now I am calling runtime.subscribe([]) for each new channel I want to listen to.
This is working well when I have only one channel. Now I am trying to get this to work for a second channel. I am getting strange and confusing results. Sometimes it works, sometimes it doesn't.
Is the following approach fundamentally flawed?
Realtime-Object is Singleton and created once for the (Flutter) App.
@override
Future<void> subscribeRealtime(
final String channel,
final Function(Map<String, dynamic> data) handler,
) async {
// unsubscribe first is channel is known.
// happens when internet-connection is lost and reestablished.
if (channel2Subscription.containsKey(channel)) {
await unsubscribeRealtime(channel);
}
final RealtimeSubscription subscription = realtime.subscribe([channel]);
print('???# RealtimeRepositoryImpl subscribeRealtime($channel) #$hashCode with Realtime:#${realtime.hashCode}');
// save subscription so we can close it later.
channel2Subscription[channel] = subscription;
// listen to the channel and call handler
subscription.stream.listen(
(final RealtimeMessage message) {
print('???# RealtimeRepositoryImpl => event(): $channel');
final Map<String, dynamic> payload = message.payload;
handler(data);
},
onDone: () {
print('???# !! RealtimeRepositoryImpl => onDone(): $channel');
},
onError: (Object error, StackTrace s) {
print('???# !! RealtimeRepositoryImpl => onError(): $channel $error, $s');
},
);
print('???# => ${channel2Subscription.length}');
}