ra1u / redis-dart

fast redis protocol parser and client
MIT License
84 stars 35 forks source link

implement a way to automatically reconnect and resubscribe if the Redis server is restarted #67

Closed insinfo closed 2 years ago

insinfo commented 2 years ago

implement a way to automatically reconnect or resubscribe if the Redis server is restarted

void main() async {
    await initRedisSubscribe();
}
...
Future<void> initRedisSubscribe() async {
    var redisCommand = await getCurrentRedisConnection();

        redisPubSub = PubSub(redisCommand);

    redisPubSub.subscribe(['notification']);
    final stream = redisPubSub.getStream();

    var streamWithoutErrors = stream
        .handleError((e) => print('NotificationService redisPubSub error $e'));

    streamWithoutErrors.listen((msg) {
      var kind = msg[0];
      var data = msg[2];
      if (kind == 'message') {
        var map = jsonDecode(data);
        var notification = Notification.fromMap(map);
        sendNotifications(notification);
      }
      // else {
      //  print('received non-message ${msg}');
      //}
    });
  }