centrifugal / centrifuge-dart

Dart (Flutter) client SDK for bidirectional communication with Centrifugo and Centrifuge-based server over WebSocket

Home Page:https://pub.dartlang.org/packages/centrifuge

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Higher level api

synw opened this issue · comments

commented

Hi. Trying to learn this library I ended up making a helper class that reduces the api surface and the boilerplate. Commented code and an example are available: https://github.com/synw/centrifuge-dart/tree/connection_manager/example/manager

It looks like this:

final cent = CentrifugoConnectionManager(
      serverUrl: conf.centrifugoUrl, userToken: token)
    ..connect();
  await cent.onConnected;
  final chan = cent.addChannel("chat");
  await chan.subscribe(
      onMessage: (Map<String, dynamic> payload) => print("MSG $payload"));
  await chan.onJoin;
  unawaited(chan.publish(<String, dynamic>{"message": "Foo"}));

This approach is based on callbacks and completers: this way the user does not have to manage half a dozen stream subscriptions. I added a CentrifugoChannel model as a paradigm for the library user

@synw hello!

My question about API like this is how to handle reconnect and resubscription events. For example as soon as await chan.subscribe(...) finished it's work we don't have a callback that will be called after reconnect. The same for connect/disconnect during connection lifetime. Or am I missing sth?

commented

Good point. I'll work on adding an autoreconnect/resubscribe mechanism to this experimental api. And better error management. I need to dig more in this library's code and use it in a few apps to get a better understanding and eventually produce something realistic