angel_sync 2.0.0

sync #

Pub build status

Easily synchronize and scale WebSockets using package:pub_sub.

Usage #

This package exposes PubSubSynchronizationChannel, which can simply be dropped into any AngelWebSocket constructor.

Once you've set that up, instances of your application will automatically fire events in-sync. That's all you have to do to scale a real-time application with Angel!

await app.configure(new AngelWebSocket(
    synchronizationChannel: new PubSubSynchronizationChannel(
        new pub_sub.IsolateClient('<client-id>', adapter.receivePort.sendPort),

2.0.0 #

  • Dart 2 + Angel 2 updates.
  • Extend StreamChannel, instead of the defunct WebSocketSynchronizer.


import 'dart:isolate';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';
import 'package:angel_sync/angel_sync.dart';
import 'package:angel_test/angel_test.dart';
import 'package:angel_websocket/io.dart' as client;
import 'package:angel_websocket/server.dart';
import 'package:pub_sub/isolate.dart' as pub_sub;
import 'package:pub_sub/pub_sub.dart' as pub_sub;
import 'package:test/test.dart';

main() {
  Angel app1, app2;
  TestClient app1Client;
  client.WebSockets app2Client;
  pub_sub.Server server;
  ReceivePort app1Port, app2Port;

  setUp(() async {
    var adapter = new pub_sub.IsolateAdapter();

    server = new pub_sub.Server([
      ..registerClient(const pub_sub.ClientInfo('angel_sync1'))
      ..registerClient(const pub_sub.ClientInfo('angel_sync2'))

    app1 = new Angel();
    app2 = new Angel();'/message', (req, res) async {
      // Manually broadcast. Even though app1 has no clients, it *should*
      // propagate to app2.
      var ws = req.container.make<AngelWebSocket>();
      var body = await req.parseBody();
      ws.batchEvent(new WebSocketEvent(
        eventName: 'message',
        data: body['message'],
      return 'Sent: ${body['message']}';

    app1Port = new ReceivePort();
    var ws1 = new AngelWebSocket(
      synchronizationChannel: new PubSubSynchronizationChannel(
        new pub_sub.IsolateClient('angel_sync1', adapter.receivePort.sendPort),
    await app1.configure(ws1.configureServer);
    app1.get('/ws', ws1.handleRequest);
    app1Client = await connectTo(app1);

    app2Port = new ReceivePort();
    var ws2 = new AngelWebSocket(
      synchronizationChannel: new PubSubSynchronizationChannel(
        new pub_sub.IsolateClient('angel_sync2', adapter.receivePort.sendPort),
    await app2.configure(ws2.configureServer);
    app2.get('/ws', ws2.handleRequest);

    var http = new AngelHttp(app2);
    await http.startServer();
    var wsPath =
        http.uri.replace(scheme: 'ws', path: '/ws').removeFragment().toString();
    app2Client = new client.WebSockets(wsPath);
    await app2Client.connect();

  tearDown(() {

  test('events propagate', () async {
    // The point of this test is that neither app1 nor app2
    // is aware that the other even exists.
    // Regardless, a WebSocket event broadcast in app1 will be
    // broadcast by app2 as well.

    var stream = app2Client.on['message'];
    var response =
        await'/message', body: {'message': 'Hello, world!'});
    print('app1 response: ${response.body}');

    var msg = await stream.first.timeout(const Duration(seconds: 5));
    print('app2 got message: ${}');

Use this package as a library

1. Depend on it

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

  angel_sync: ^2.0.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:angel_sync/angel_sync.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 Feb 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/angel_sync.dart. (-1.49 points)

Analysis of lib/angel_sync.dart reported 3 hints:

line 12 col 7: Unnecessary new keyword.

line 30 col 21: Unnecessary new keyword.

line 46 col 7: DO use curly braces for all flow control structures.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (stream_channel).

Package is getting outdated. (-24.66 points)

The package was last published 65 weeks ago.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
angel_framework ^2.0.0-alpha 2.1.1
angel_websocket ^2.0.0-alpha 2.0.1 2.0.3
pub_sub ^2.0.0 2.3.0
stream_channel ^1.0.0 1.7.0 2.0.0
Transitive dependencies
angel_auth 2.1.5+1
angel_client 2.0.2
angel_container 1.1.0
angel_http_exception 1.1.0
angel_model 1.0.3
angel_route 3.1.0+1
async 2.4.0
charcode 1.1.3
code_buffer 1.0.1
collection 1.14.12
combinator 1.1.0
convert 2.1.1
crypto 2.1.4
dart2_constant 1.0.2+dart2
file 5.1.0
http 0.12.0+4
http2 1.0.0
http_parser 3.1.3
http_server 0.9.8+3
intl 0.16.1
json_god 2.0.0-beta+3
json_rpc_2 2.1.0
logging 0.11.4
matcher 0.12.6
merge_map 1.0.2
meta 1.1.8
mime 0.9.6+3
mock_request 1.0.6
path 1.6.4
pedantic 1.9.0
quiver 2.1.2+1
quiver_hashcode 2.0.0
recase 2.0.1 3.0.0
source_span 1.6.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
tuple 1.0.3
typed_data 1.1.6
uuid 2.0.4
web_socket_channel 1.1.0
Dev dependencies
angel_test ^2.0.0-alpha
test ^1.0.0