Skip to main content

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

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

example/lib/main.dart

/// A chat app built on `peer_coordinator`.
///
/// Nodes join a named room — through a WebSocket hub, or serverless, with the
/// host's page as the hub — elect a coordinator between themselves, and
/// exchange lines over the user-message channel. See
/// `lib/src/chat/chat_session.dart` for how the messages actually travel,
/// `lib/src/p2p/p2p_room.dart` for the serverless rooms, and README.md for how
/// to run a hub.
library;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:logging/logging.dart';
import 'package:peer_coordinator/peer_coordinator.dart';

import 'src/p2p/invite_codec.dart';
import 'src/ui/connect_page.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  // Read before Flutter touches the URL, then tell it to leave the URL alone:
  // there are no routes here, and the default hash strategy would treat
  // `#join=...` as a route name and replace it.
  final invite = inviteCodeIn(Uri.base);
  setUrlStrategy(null);

  // peer_coordinator logs through package:logging. Its default printer emits
  // ANSI colour, which the Flutter console renders as noise, so print plainly.
  Log.useColors = false;
  Logger.root.level = kDebugMode ? Level.INFO : Level.WARNING;
  Logger.root.onRecord.listen((record) {
    debugPrint(
      '[${record.level.name}] ${record.loggerName}: ${record.message}',
    );
  });

  runApp(ChatApp(initialInvite: invite));
}

class ChatApp extends StatelessWidget {
  const ChatApp({this.initialInvite, super.key});

  /// An invite code from the page's `#join=` fragment, if it was opened from
  /// an invite link.
  final String? initialInvite;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'peer_coordinator chat',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.teal),
        useMaterial3: true,
      ),
      darkTheme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
          seedColor: Colors.teal,
          brightness: Brightness.dark,
        ),
        useMaterial3: true,
      ),
      home: ConnectPage(initialInvite: initialInvite),
    );
  }
}
0
likes
160
points
1.16k
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