Remote / foldkit-remote-server

LiveHub

interface · packages/remote-server/src/index.ts:1245

interface LiveHub<P, R = never> {
  readonly changed: (
    ref: LiveRef,
    fields: ReadonlyArray<string>,
    values?: Readonly<Record<string, unknown>> | undefined,
  ) => Effect.Effect<void, RemoteServerError, R>
  readonly deleted: (ref: LiveRef) => Effect.Effect<void, RemoteServerError, R>
  readonly subscribe: (context: {
    readonly requirements: ReadonlyArray<Requirement>
    readonly after: number
    readonly principal: P
    readonly viewer?: Option.Option<EntityRef> | undefined
  }) => Stream.Stream<LiveChangeValue, RemoteServerError>
  readonly size: Effect.Effect<number>
}

The server-side "these fields changed" signal. A hub tracks each live subscriber's requirements (entity, id, fields) and principal; changed re-reads the changed fields a subscriber selects through the entity's own source, under that subscriber's principal, and streams the patch to it. Subscribers that select none of the changed fields do no work. Cursors continue from the cursor each subscriber resumed at, so the client's duplicate and gap handling is unchanged.

In the guides

Members

LiveHub.changed

property · packages/remote-server/src/index.ts:1251

readonly changed: (
  ref: LiveRef,
  fields: ReadonlyArray<string>,
  values?: Readonly<Record<string, unknown>> | undefined,
) => Effect.Effect<void, RemoteServerError, R>

values, when given, are the row's stored values of fields (a declared write's answer): a field of them the source declares shared is sent as it is, and only the rest are re-read.

LiveHub.deleted

property · packages/remote-server/src/index.ts:1261

readonly deleted: (ref: LiveRef) => Effect.Effect<void, RemoteServerError, R>

ref may be gone: each subscriber's selection of it is read again, and one it is no longer there to is sent a deletion. A row deleted for one writer may only be hidden from them, and still there to others.

LiveHub.subscribe

property · packages/remote-server/src/index.ts:1266

readonly subscribe: (context: {
  readonly requirements: ReadonlyArray<Requirement>
  readonly after: number
  readonly principal: P
  readonly viewer?: Option.Option<EntityRef> | undefined
}) => Stream.Stream<LiveChangeValue, RemoteServerError>

Registers a subscriber for the stream's lifetime; handlers calls this, with requirements it has admitted (see LiveSource.subscribe).

LiveHub.size

property · packages/remote-server/src/index.ts:1274

readonly size: Effect.Effect<number>

How many subscribers are registered now.