Example
Examples:
example\flutter_app
simple chat applicationexample\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:
client.connect();
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
:
client.connectStream.listen(onEvent);
client.disconnectStream.listen(onEvent);
client.connect();
Connect and disconnect events can happen many times throughout client lifetime.
Subscribe to channel:
final subscription = client.getSubscription(channel);
subscription.publishStream.listen(onEvent);
subscription.joinStream.listen(onEvent);
subscription.leaveStream.listen(onEvent);
subscription.subscribeSuccessStream.listen(onEvent);
subscription.subscribeErrorStream.listen(onEvent);
subscription.unsubscribeStream.listen(onEvent);
subscription.subscribe();
Publish:
final output = jsonEncode({'input': message});
final data = utf8.encode(output);
await subscription.publish(data);
Feature matrix
x
connect to server using Protobuf protocol formatx
connect with JWTx
connect with custom headerx
automatic reconnect in case of errors, network problems etcx
exponential backoff for reconnectx
connect and disconnect eventsx
handle disconnect reasonx
subscribe on channel and handle asynchronous Publicationsx
handle Join and Leave messagesx
handle Unsubscribe notificationsx
publish method of Subscriptionx
unsubscribe method of Subscriptionx
history method of Subscriptionx
handle asynchronous messages from serverx
send RPC commandsx
publish to channel without being subscribedx
subscribe to private channels with JWTx
ping/pong to find broken connection
Instructions to update protobuf
- Install
protoc
compiler - Install
protoc_plugin
https://pub.dev/packages/protoc_plugin - cd
lib/src/proto
and runprotoc --dart_out=. -I . client.proto
- cd to root and run
dartfmt -w lib/ test/
Instructions to release
- Update changelog
- Bump version in
pubspec.yaml
, push, create new tag pub publish
Author
German Saprykin, saprykin.h@gmail.com