angel_sync 2.0.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 50

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.

example/main.dart

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([
      adapter,
    ])
      ..registerClient(const pub_sub.ClientInfo('angel_sync1'))
      ..registerClient(const pub_sub.ClientInfo('angel_sync2'))
      ..start();

    app1 = new Angel();
    app2 = new Angel();

    app1.post('/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(
      app1,
      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(
      app2,
      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();
    print(wsPath);
    app2Client = new client.WebSockets(wsPath);
    await app2Client.connect();
  });

  tearDown(() {
    server.close();
    app1Port.close();
    app2Port.close();
    app1.close();
    app2.close();
    app1Client.close();
    app2Client.close();
  });

  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 app1Client.post('/message', body: {'message': 'Hello, world!'});
    print('app1 response: ${response.body}');

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

Use this package as a library

1. Depend on it

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


dependencies:
  angel_sync: ^2.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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';
  
Version Uploaded Documentation Archive
2.0.0 Nov 15, 2018 Go to the documentation of angel_sync 2.0.0 Download angel_sync 2.0.0 archive
1.0.0 Aug 8, 2017 Go to the documentation of angel_sync 1.0.0 Download angel_sync 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, other

Primary library: package:angel_sync/angel_sync.dart with components: isolate.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
angel_framework ^2.0.0-alpha 2.0.0-alpha.14
angel_websocket ^2.0.0-alpha 2.0.0-alpha.7
pub_sub ^2.0.0 2.1.0
stream_channel ^1.0.0 1.6.8
Transitive dependencies
angel_auth 2.0.2
angel_client 2.0.0-alpha.2
angel_container 1.0.0
angel_http_exception 1.0.0+3
angel_model 1.0.0+1
angel_route 3.0.0
async 2.0.8
body_parser 1.1.1
charcode 1.1.2
code_buffer 1.0.1
collection 1.14.11
combinator 1.0.0+3
convert 2.0.2
crypto 2.0.6
dart2_constant 1.0.2+dart2
file 5.0.6
http 0.12.0
http2 0.1.9
http_parser 3.1.3
http_server 0.9.8+1
intl 0.15.7
json_god 2.0.0-beta+3
json_rpc_2 2.0.9
logging 0.11.3+2
matcher 0.12.4
merge_map 1.0.1
meta 1.1.6
mime 0.9.6+2
mock_request 1.0.4
path 1.6.2
quiver 2.0.1
source_span 1.4.1
stack_trace 1.9.3
string_scanner 1.0.4
tuple 1.0.2
typed_data 1.1.6
uuid 1.0.3
web_socket_channel 1.0.9
Dev dependencies
angel_test ^2.0.0-alpha
test ^1.0.0