Skip to main content

peer_coordinator 0.4.7 copy "peer_coordinator: ^0.4.7" to clipboard
peer_coordinator: ^0.4.7 copied to clipboard

Transport-neutral peer coordination: election, membership, heartbeat and synchronised data streams over a pluggable backend. Pure Dart, web-safe.

example/peer_coordinator_example.dart

// Two nodes form a session over the in-memory transport: one is elected
// coordinator, and the two exchange messages over the user-message channel.
//
//     dart run example/peer_coordinator_example.dart
//
// With another transport (the WebSocket hub in this package, LSL from
// liblsl_coordinator, WebRTC from webrtc_coordinator) each node runs on its
// own device, and nothing below changes but the transportConfig.
//
// For a whole application, see the chat app in the repository, which adds a
// user interface, a WebSocket hub and a serverless WebRTC mode:
// https://github.com/NexusDynamic/liblsl.dart/tree/main/apps/peer_chat
import 'package:peer_coordinator/in_memory.dart';
import 'package:peer_coordinator/peer_coordinator.dart';

Future<void> main() async {
  // Nodes in one process find each other on a shared bus.
  final bus = InMemoryBus();

  Future<PeerSession> join(String name) async {
    final session = PeerSession.create(
      CoordinationConfig(
        name: 'example_app',
        sessionConfig: CoordinationSessionConfig(name: 'room_1', maxNodes: 2),
        transportConfig: InMemoryTransportConfig(bus: bus),
      ),
      thisNodeConfig: NodeConfigFactory().defaultConfig().copyWith(name: name),
    );
    await session.initialize();
    // Elects a coordinator: the first node finds nobody and takes the role,
    // the second finds it and joins as a participant.
    await session.join();
    return session;
  }

  final ana = await join('ana');
  final bo = await join('bo');
  print('ana is ${ana.isCoordinator ? 'coordinator' : 'participant'}');
  print('bo is ${bo.isCoordinator ? 'coordinator' : 'participant'}');

  // The coordinator's messages reach every participant; a participant's
  // messages reach the coordinator.
  bo.events.userCoordinationMessages.listen((event) async {
    print('bo got "${event.description}" ${event.payload}');
    await bo.sendUserMessage('reply', 'ready', {
      'trial': event.payload['trial'],
    });
  });
  ana.events.userParticipantMessages.listen((event) {
    print('ana got "${event.description}" ${event.payload}');
  });

  await ana.waitForMinNodes(2);
  await ana.sendUserMessage('trial', 'Trial 1 starts', {'trial': 1});

  await Future<void>.delayed(const Duration(milliseconds: 200));
  await bo.leave();
  await ana.leave();
  await bo.dispose();
  await ana.dispose();
}
0
likes
160
points
928
downloads

Documentation

API reference

Publisher

verified publisherzeyus.com

Weekly Downloads

Transport-neutral peer coordination: election, membership, heartbeat and synchronised data streams over a pluggable backend. Pure Dart, web-safe.

Homepage
Repository (GitHub)
View/report issues
Contributing

License

MIT (license)

Dependencies

collection, crypto, fast_immutable_collections, logging, lsl_core, meta, synchronized, test, uuid, web_socket_channel

More

Packages that depend on peer_coordinator