serve static method

Future<CoordinationHub> serve({
  1. required HubCredentials credentials,
  2. InternetAddress? address,
  3. int port = 0,
  4. WsLimits limits = const WsLimits(),
  5. Duration? sessionTtl,
})

Starts a hub for one session.

credentials is required rather than optional on purpose: an open relay is not a mode this package offers, not even as a default, because the failure is silent and total.

Pass port: 0 (the default) for an ephemeral port, so tests can run in parallel without colliding; read port afterwards.

sessionTtl closes the session automatically once it elapses — the point being that an experiment can be scheduled to end without anyone having to remember to end it.

Implementation

static Future<CoordinationHub> serve({
  required HubCredentials credentials,
  InternetAddress? address,
  int port = 0,
  WsLimits limits = const WsLimits(),
  Duration? sessionTtl,
}) async {
  final server = await HttpServer.bind(
    address ?? InternetAddress.loopbackIPv4,
    port,
    shared: false,
  );
  final hub = CoordinationHub._(
    server,
    credentials.session,
    credentials.secret,
    limits,
  );
  if (sessionTtl != null) hub._armTtl(sessionTtl);
  unawaited(hub._accept());
  logger.info(
    'Coordination hub listening on port ${hub.port} for session '
    '"${credentials.session}" (epoch ${hub._epoch})',
  );
  return hub;
}