Coder Social home page Coder Social logo

hetian9288 / centrifuge-dart Goto Github PK

View Code? Open in Web Editor NEW

This project forked from centrifugal/centrifuge-dart

0.0 1.0 0.0 278 KB

WebSocket client for Centrifugo server and Centrifuge library in Dart language

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

License: MIT License

Dart 97.09% Java 0.25% Objective-C 0.54% Shell 1.01% Kotlin 0.08% Swift 0.27% HTML 0.76%

centrifuge-dart's Introduction

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. While this client uses binary Protobuf protocol internally nothing stops you from sending JSON-encoded data over it.

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 token (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
  • top-level publish method
  • top-level presence method
  • top-level presence stats method
  • top-level history method
  • top-level unsubscribe method
  • send asynchronous messages to server
  • handle asynchronous messages from server
  • send RPC commands
  • subscribe to private channels with token (JWT)
  • connection JWT refresh
  • private channel subscription token (JWT) refresh
  • handle connection expired error
  • handle subscription expired error
  • ping/pong to find broken connection
  • message recovery mechanism for client-side subscriptions
  • server-side subscriptions
  • message recovery mechanism for server-side subscriptions
  • history stream pagination

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, [email protected]

centrifuge-dart's People

Contributors

fzambia avatar holofox avatar mogol avatar synw avatar tiamo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.