Skip to content

API reference

realtime(options) creates a Layeron module for rooms, channels, presence, history, snapshots, authorization, and CRDT rooms.

Terminal window
import { realtime } from "@layeron/modules"

Create a Layeron realtime module.

Terminal window
realtime(options: RealtimeModuleOptions): RealtimeModule

Options accepted by realtime(options).

FieldTypeDescription
namestringStable Realtime instance name inside the Layeron app.
namespacestring | undefinedPlatform namespace for the instance. Defaults to default.
databaseNamestring | undefinedDatabase product name used for Realtime state. Omit this unless you need a custom internal state store name.
bindingstring | undefinedCustom binding name for advanced runtime access.
historyLimitint32 | undefinedDefault page size for room history and CRDT document updates. Defaults to 100.
presenceTtlSecondsint32 | undefinedPresence freshness window in seconds. Defaults to 60.
lifecycleRealtimeLifecycleOptions | undefinedRoom lifecycle behavior for idle cleanup and destroyed room handling.
autoResolveUserboolean | undefinedWhether Realtime should resolve the actor from request context headers. Defaults to true.
allowAnonymousboolean | undefinedWhether anonymous actors are allowed when no user or explicit actor is available. Defaults to false.
observabilityLayeron.Observability.ObservabilityConfig | undefinedObservability settings for the Realtime module.
placementLayeron.Common.Placement.LayeronPlacement | undefinedPlacement overrides for this Realtime product instance.

Lifecycle options accepted by realtime({ lifecycle }).

FieldTypeDescription
idleTtlSecondsint32 | undefinedSeconds after the last activity before the Durable Object can mark the room idle. Defaults to 300.
destroyTtlSecondsint32 | undefinedSeconds after destroy before hot Durable Object state can be fully cleared. Defaults to 86400.
allowRecreateboolean | undefinedWhether writes can recreate a destroyed room. Defaults to false.

Layeron Realtime module returned by realtime(options).

FieldTypeDescription
name”realtime”Module name.
configRealtimeModuleOptionsNormalized module configuration.
observabilityLayeron.Observability.ObservabilityConfig | undefinedEffective observability policy recorded on the module declaration.

Actor attached to a Realtime write.

FieldTypeDescription
kind”anonymous” | “user”Actor kind.
idstring | undefinedStable actor id when available.
userIdstring | undefinedAuthenticated user id when the actor is a user.
sessionIdstring | undefinedSession id associated with the write.
tenantIdstring | undefinedTenant id associated with the write.
emailstring | undefinedUser email when available.
displayNamestring | undefinedDisplay name when available.

Room or channel identity.

FieldTypeDescription
kind”room” | “channel”Realtime target kind.
namestringRoom or channel name.

Message input accepted by room.publish(), room.broadcast(), channel.publish(), and channel.broadcast().

FieldTypeDescription
typestringClient-visible event type.
dataunknown | undefinedJSON event payload.
metadataRecord<unknown> | undefinedJSON metadata associated with the event.
idempotencyKeystring | undefinedRetry-safe idempotency key for duplicate publish protection.
actorRealtimePrincipal | undefinedExplicit actor for this write. Omit to use automatic identity resolution.

Internal publish payload with room identity.

FieldTypeDescription
typestringClient-visible event type.
dataunknown | undefinedJSON event payload.
metadataRecord<unknown> | undefinedJSON metadata associated with the event.
idempotencyKeystring | undefinedRetry-safe idempotency key for duplicate publish protection.
actorRealtimePrincipal | undefinedExplicit actor for this write. Omit to use automatic identity resolution.

Result returned by publish and broadcast.

FieldTypeDescription
roomstringRoom or channel name.
kind”room” | “channel”Realtime target kind.
messageIdstringGenerated message id.
publishedAtstringPublish timestamp.
actorRealtimePrincipal | undefinedActor attached to the message.

Input accepted by room.join().

FieldTypeDescription
memberIdstring | undefinedStable member id. Omit to derive from the actor.
actorRealtimePrincipal | undefinedExplicit actor for this join.
metadataRecord<unknown> | undefinedMember metadata such as role or client information.
presenceRecord<unknown> | undefinedInitial live presence state.

Internal join payload with room identity.

FieldTypeDescription
memberIdstring | undefinedStable member id. Omit to derive from the actor.
actorRealtimePrincipal | undefinedExplicit actor for this join.
metadataRecord<unknown> | undefinedMember metadata such as role or client information.
presenceRecord<unknown> | undefinedInitial live presence state.

Input accepted by room.leave().

FieldTypeDescription
memberIdstring | undefinedMember id to mark as left. Omit to derive from the actor.

Result returned by room.leave().

FieldTypeDescription
roomstringRoom or channel name.
kind”room” | “channel”Realtime target kind.
memberIdstringMember id that left.
leftAtstringLeave timestamp.

Room member record returned by join() and members().

FieldTypeDescription
memberIdstringStable member id inside the room.
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
joinedAtstringJoin timestamp.
lastSeenAtstringLast seen timestamp.
leftAtstring | undefinedLeave timestamp when the member has left.
actorRealtimePrincipal | undefinedActor associated with the member.
metadataRecord<unknown>Member metadata such as role or client information.
presenceRecord<unknown>Latest live presence state.

Options accepted by room.members().

FieldTypeDescription
limitint32 | undefinedMaximum members to return.

Input accepted by room.presence().

FieldTypeDescription
memberIdstring | undefinedMember id to update. Omit to derive from the actor.
presenceRecord<unknown> | undefinedLive presence state.

Internal presence payload with room identity.

FieldTypeDescription
memberIdstring | undefinedMember id to update. Omit to derive from the actor.
presenceRecord<unknown> | undefinedLive presence state.

Result returned by room.presence().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
memberIdstringMember id that was updated.
updatedAtstringPresence update timestamp.
presenceRecord<unknown>Latest live presence state.

Input accepted by room.metadata().

FieldTypeDescription
metadataRecord<unknown> | undefinedShared room metadata.

Result returned by room.metadata().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
metadataRecord<unknown>Latest shared room metadata.
updatedAtstringMetadata update timestamp.

Options accepted by room.history() and channel.history().

FieldTypeDescription
limitint32 | undefinedMaximum messages to return.
cursorstring | undefinedCursor returned by a previous history call.

History message record.

FieldTypeDescription
messageIdstringGenerated message id.
roomstringRoom or channel name.
kind”room” | “channel”Realtime target kind.
typestringClient-visible event type.
dataunknown | undefinedJSON event payload.
metadataRecord<unknown>JSON metadata associated with the event.
actorRealtimePrincipal | undefinedActor attached to the message.
publishedAtstringPublish timestamp.

Result returned by history().

FieldTypeDescription
roomstringRoom or channel name.
kind”room” | “channel”Realtime target kind.
itemsRealtimeHistoryMessage[]History messages in result order.
cursorstring | undefinedCursor for fetching older messages.

Result returned by snapshot().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
snapshotIdstringGenerated snapshot id.
createdAtstringSnapshot creation timestamp.
stateRecord<unknown>Snapshot state payload.

Result returned by restore().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
snapshotIdstringSnapshot id that was restored.
restoredAtstringRestore timestamp.

Input accepted by room.authorize().

FieldTypeDescription
memberIdstring | undefinedMember id attached to the websocket connection.
clientIdstring | undefinedClient or device id attached to the websocket connection.
presenceRecord<unknown> | undefinedInitial presence state attached to the websocket connection.
actorRealtimePrincipal | undefinedExplicit actor for the token. Omit to use automatic identity resolution.
ttlSecondsint32 | undefinedToken lifetime in seconds.

Result returned by room.authorize().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
tokenstringShort-lived authorization token.
expiresAtstringToken expiration timestamp.
urlstringWebSocket URL used by browser clients.
protocolsstring[]WebSocket protocols used by browser clients.
actorRealtimePrincipal | undefinedActor associated with the token.
memberIdstring | undefinedMember id attached to the websocket connection.
clientIdstring | undefinedClient or device id attached to the websocket connection.
presenceRecord<unknown> | undefinedInitial presence state attached to the websocket connection.

Realtime instance stats.

FieldTypeDescription
roomsint32Number of rooms tracked by the Realtime instance.
membersint32Number of active members tracked by the Realtime instance.
messagesint32Number of messages tracked by the Realtime instance.

Current lifecycle state for a room or CRDT room.

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
status”active” | “idle” | “destroyed”Lifecycle status.
activeConnectionsint32Current live WebSocket connections held by the room Durable Object.
lastActivityAtstring | undefinedLast activity timestamp when known.
idleSincestring | undefinedTimestamp when the room became idle.
destroyedAtstring | undefinedTimestamp when the room was destroyed.

Options accepted by room.destroy() and crdtRoom.destroy().

FieldTypeDescription
deleteHistoryboolean | undefinedDelete message history for this room.
deleteSnapshotsboolean | undefinedDelete snapshots for this room.
deleteCrdtUpdatesboolean | undefinedDelete CRDT updates and awareness records for this room.

Result returned by room.destroy() and crdtRoom.destroy().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
destroyedAtstringDestroy timestamp.
deletedHistorybooleanWhether message history was deleted.
deletedSnapshotsbooleanWhether snapshots were deleted.
deletedCrdtUpdatesbooleanWhether CRDT updates and awareness records were deleted.

Input accepted by crdtRoom.applyUpdate().

FieldTypeDescription
updateunknownJSON update payload produced by the client.
clientIdstring | undefinedClient or device id that produced the update.
clockint32 | undefinedOptional client clock used for ordered reads.
actorRealtimePrincipal | undefinedExplicit actor for this update.

Internal CRDT update payload with room identity.

FieldTypeDescription
updateunknownCRDT update payload.
clientIdstring | undefinedClient id that produced the update.
clockint32 | undefinedOptional client clock used for ordered reads.
actorRealtimePrincipal | undefinedExplicit actor for this update.

Result returned by crdtRoom.applyUpdate().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
updateIdstringGenerated update id.
appliedAtstringUpdate application timestamp.
actorRealtimePrincipal | undefinedActor attached to the update.

Input accepted by crdtRoom.awareness().

FieldTypeDescription
clientIdstring | undefinedClient or device id whose awareness is being updated.
stateRecord<unknown> | undefinedJSON awareness state such as cursor, selection, tool, or display details.
actorRealtimePrincipal | undefinedExplicit actor for this awareness update.

Result returned by crdtRoom.awareness().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
clientIdstringClient or device id whose awareness was updated.
updatedAtstringAwareness update timestamp.
stateRecord<unknown>Latest awareness state.
actorRealtimePrincipal | undefinedActor attached to the awareness update.

Options accepted by crdtRoom.document().

FieldTypeDescription
sinceClockint32 | undefinedReturn updates after this client clock.
limitint32 | undefinedMaximum updates to return.

CRDT update record returned by crdtRoom.document().

FieldTypeDescription
updateIdstringGenerated update id.
updateunknownJSON update payload.
clientIdstring | undefinedClient or device id that produced the update.
clockint32 | undefinedClient clock used for ordered reads.
actorRealtimePrincipal | undefinedActor attached to the update.
appliedAtstringUpdate application timestamp.

Result returned by crdtRoom.document().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
updatesRealtimeCrdtDocumentUpdate[]Ordered document updates.
awarenessRealtimeCrdtAwarenessResult[]Current awareness records.
cursorstring | undefinedCursor for reading additional updates.

Input accepted by crdtRoom.applyYjsUpdate().

FieldTypeDescription
updateBase64stringYjs update encoded as base64.
clientIdstring | undefinedClient or device id that produced the update.
clockint32 | undefinedOptional client clock used for ordered reads.
actorRealtimePrincipal | undefinedExplicit actor for this update.

Result returned by crdtRoom.applyYjsUpdate().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
updateIdstringGenerated update id.
appliedAtstringUpdate application timestamp.
updateBase64stringYjs update encoded as base64.
stateVectorBase64string | undefinedCurrent Yjs state vector encoded as base64 when available.
actorRealtimePrincipal | undefinedActor attached to the update.

Options accepted by crdtRoom.syncYjs().

FieldTypeDescription
stateVectorBase64string | undefinedClient state vector encoded as base64. Omit to receive the current merged update.
limitint32 | undefinedMaximum historical update records included beside the merged update.

Yjs update record returned by crdtRoom.syncYjs().

FieldTypeDescription
updateIdstringGenerated update id.
updateBase64stringYjs update encoded as base64.
clientIdstring | undefinedClient or device id that produced the update.
clockint32 | undefinedClient clock used for ordered reads.
actorRealtimePrincipal | undefinedActor attached to the update.
appliedAtstringUpdate application timestamp.

Result returned by crdtRoom.syncYjs().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
updateBase64string | undefinedMerged Yjs update encoded as base64.
stateVectorBase64string | undefinedCurrent Yjs state vector encoded as base64.
updatesRealtimeYjsUpdateRecord[]Historical Yjs update records included for inspection or replay.
awarenessRealtimeCrdtAwarenessResult[]Current awareness records.

Result returned by crdtRoom.compact().

FieldTypeDescription
roomstringRoom name.
kind”room” | “channel”Realtime target kind.
compactedAtstringCompaction timestamp.
updatesCompactedint32Number of Yjs updates represented by the checkpoint.
checkpointUpdateBase64string | undefinedMerged checkpoint update encoded as base64.
stateVectorBase64string | undefinedYjs state vector encoded as base64.

Create a room handle.

Terminal window
realtimeRoom(name: string): RealtimeRoomIdentity

Create a channel handle.

Terminal window
realtimeChannel(name: string): RealtimeRoomIdentity

Create a CRDT room handle.

Terminal window
realtimeCrdtRoom(name: string): RealtimeRoomIdentity

Publish and record a room or channel message.

Terminal window
realtimePublish(input: RealtimeMessageInput): RealtimePublishResult

Broadcast and record a room or channel message.

Terminal window
realtimeBroadcast(input: RealtimeMessageInput): RealtimePublishResult

Join or refresh membership in a room.

Terminal window
realtimeJoin(input?: RealtimeJoinOptions): RealtimeMember

Mark a member as left.

Terminal window
realtimeLeave(input?: RealtimeLeaveOptions): RealtimeLeaveResult

List active room members.

Terminal window
realtimeMembers(options?: RealtimeMembersOptions): RealtimeMember[]

Update live presence state.

Terminal window
realtimePresence(input?: RealtimePresenceOptions): RealtimePresenceResult

Update shared room metadata.

Terminal window
realtimeMetadata(input?: RealtimeMetadataOptions): RealtimeMetadataResult

Read room or channel history.

Terminal window
realtimeHistory(options?: RealtimeHistoryOptions): RealtimeHistoryResult

Create a room snapshot.

Terminal window
realtimeSnapshot(): RealtimeSnapshotResult

Restore a room snapshot.

Terminal window
realtimeRestore(snapshotId: string): RealtimeRestoreResult

Create a short-lived room authorization token.

Terminal window
realtimeAuthorize(input?: RealtimeAuthorizeOptions): RealtimeAuthorizeResult

Read the lifecycle state for a room.

Terminal window
realtimeLifecycle(): RealtimeLifecycleResult

Destroy a room and optionally delete stored history, snapshots, and CRDT updates.

Terminal window
realtimeDestroy(input?: RealtimeDestroyOptions): RealtimeDestroyResult

Record a collaborative document update.

Terminal window
realtimeCrdtApplyUpdate(input: RealtimeCrdtUpdateOptions): RealtimeCrdtUpdateResult

Update collaborative document awareness state.

Terminal window
realtimeCrdtAwareness(input?: RealtimeCrdtAwarenessOptions): RealtimeCrdtAwarenessResult

Read ordered document updates and current awareness state.

Terminal window
realtimeCrdtDocument(options?: RealtimeCrdtDocumentOptions): RealtimeCrdtDocumentResult

Apply a binary Yjs update encoded as base64.

Terminal window
realtimeYjsApplyUpdate(input: RealtimeYjsUpdateOptions): RealtimeYjsUpdateResult

Synchronize a Yjs document using a base64 state vector.

Terminal window
realtimeYjsSync(options?: RealtimeYjsSyncOptions): RealtimeYjsSyncResult

Compact Yjs updates into a Durable Object checkpoint.

Terminal window
realtimeCrdtCompact(): RealtimeCrdtCompactResult

Read Realtime instance stats.

Terminal window
realtimeStats(): RealtimeStats