subscribe method
Subscribe to channel
RedisSubscriber subscriber = await redis.subscribe('channel');
RedisSubscriber subscriber = await redis.subscribe(['channel', 'chat']);
subscriber.onMessage = (String channel,String? message) {
}
Implementation
Future<RedisSubscriber> subscribe(Object channel) async {
if (redisClientType == RedisType.publisher) {
throw Exception('cannot subscribe and publish on same connection');
}
redisClientType = RedisType.subscriber;
RedisSubscriber cb = RedisSubscriber();
if (channel is String) {
await sendCommand(<String>['SUBSCRIBE', channel]);
connection.subscribeListeners[<String>[channel]] = cb;
} else if (channel is List<String>) {
await sendCommand(<String>['SUBSCRIBE', ...channel]);
connection.subscribeListeners[channel] = cb;
} else {
throw Exception('Invalid type for channel');
}
return cb;
}