Skip to content

WebSocket client for Centrifugo server and Centrifuge library in Dart language

License

Notifications You must be signed in to change notification settings

tiamo/centrifuge-dart

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

Example

Examples:

  • 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:

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

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

Author

German Saprykin, saprykin.h@gmail.com

About

WebSocket client for Centrifugo server and Centrifuge library in Dart language

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dart 97.9%
  • Shell 1.2%
  • Other 0.9%