centrifuge 0.5.0

  • Readme
  • Changelog
  • Installing
  • 72

Build Status Coverage Status

Example #


  • example\flutter_app simple chat application
  • example\console simple console application

Usage #

Create client:

import 'package:centrifuge/centrifuge.dart' as centrifuge;

final client = centrifuge.createClient("ws://localhost:8000/connection/websocket?format=protobuf");

Note that ?format=protobuf is required because this library only works with Protobuf protocol.

Connect to server:


Note that .connect() method is asynchronous. This means that client will be properly connected and authenticated on server at some point in future. To handle connect and disconnect events you can listen to connectStream and disconnectStream:


Connect and disconnect events can happen many times throughout client lifetime.

Subscribe to channel:

final subscription = client.getSubscription(channel);




final output = jsonEncode({'input': message});
final data = utf8.encode(output);
await subscription.publish(data);

Feature matrix #

  • [ ] connect to server using JSON protocol format
  • [x] connect to server using Protobuf protocol format
  • [x] connect with JWT
  • [x] connect with custom header
  • [x] automatic reconnect in case of errors, network problems etc
  • [x] exponential backoff for reconnect
  • [x] connect and disconnect events
  • [x] handle disconnect reason
  • [x] subscribe on channel and handle asynchronous Publications
  • [x] handle Join and Leave messages
  • [x] handle Unsubscribe notifications
  • [ ] reconnect on subscribe timeout
  • [x] publish method of Subscription
  • [x] unsubscribe method of Subscription
  • [ ] presence method of Subscription
  • [ ] presence stats method of Subscription
  • [x] history method of Subscription
  • [ ] send asynchronous messages to server
  • [x] handle asynchronous messages from server
  • [x] send RPC commands
  • [x] publish to channel without being subscribed
  • [x] subscribe to private channels with JWT
  • [ ] connection JWT refresh
  • [ ] private channel subscription JWT refresh
  • [ ] handle connection expired error
  • [ ] handle subscription expired error
  • [x] ping/pong to find broken connection
  • [ ] server-side subscriptions
  • [ ] message recovery mechanism

Instructions to update protobuf #

  1. Install protoc compiler
  2. Install protoc_plugin https://pub.dev/packages/protoc_plugin
  3. cd lib/src/proto and run protoc --dart_out=. -I . client.proto
  4. cd to root and run dartfmt -w lib/ test/

Instructions to release #

  1. Update changelog
  2. Bump version in pubspec.yaml, push, create new tag
  3. pub publish

Author #

German Saprykin, saprykin.h@gmail.com

[0.5.0] #

  • Update protobuf dependency to ^1.0.1, thanks @Holofox

[0.4.1] #

  • Implement removeSubscription method, thanks @tiamo

[0.4.0] #

  • Breaking changes connected property removed

[0.3.0] #

  • Breaking changes Changed API to avoid returning futures where they do not make sense – in connect and subscribe methods.
  • Client now uses Websocket Ping/Pong frames to find broken connection
  • Support for private channel subscription using onPrivateSub configuration callback function
  • Fix several null pointer dereferences in edge cases, throw ClientDisconnectedError when there is an attempt to send protocol request over non-connected client.
  • Fix examples where subscribe could happen before stream handlers set

[0.2.0] #

  • Breaking changes Replaced subscribe method with getSubscription in Client.
  • Added reconnection.
  • Added history method to Subscription. Thanks vanelizarov.
  • Added rpc method.

[0.1.0] #

  • Added authorization with JWT
  • Added subscription to private channel with JWT

[0.0.1] #

  • Initial release

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  centrifuge: ^0.5.0

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:centrifuge/centrifuge.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.7

Health suggestions

Fix lib/src/proto/client.pbjson.dart. (-25 points)

Analysis of lib/src/proto/client.pbjson.dart reported 177 hints, including:

line 8 col 25: Avoid const keyword.

line 10 col 8: Avoid const keyword.

line 11 col 5: Avoid const keyword.

line 12 col 5: Avoid const keyword.

line 13 col 5: Avoid const keyword.

Fix lib/src/client.dart. (-0.50 points)

Analysis of lib/src/client.dart reported 1 hint:

line 205 col 7: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Maintenance suggestions

Homepage URL is insecure. (-5 points)

Update the homepage field and use a secure (https) URL.

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and centrifuge.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.1.5 1.1.8
protobuf ^1.0.1 1.0.1
Transitive dependencies
fixnum 0.10.11
Dev dependencies
mockito ^3.0.0
test ^1.3.0