Skip to content

Sessions

A rendered message is a live component, not a payload. It owns a renderer fiber, a query cache, and listeners. Close it when it should stop.

renderMessage is the shortest path. It returns the platform message:

const message = yield * renderMessage(ref, App, props);

Use mountMessage when you need the root handle:

const { message, root } = yield * mountMessage(ref, App, props);
yield * root.close; // idempotent

root.close interrupts the renderer, removes the message from the client, closes the target, and releases the component Layer, the query cache, and every interaction listener.

Prefer the scoped form in Effect code. The message cannot outlive its owner:

yield *
Effect.scoped(
Effect.gen(function* () {
const { message } = yield* mountMessageScoped(ref, App, props);
// use the message
}),
);

unmountAfter closes the root after a duration:

yield * renderMessage(ref, App, props, { unmountAfter: "10 minutes" });

DiscordGatewayClient.layerEphemeral keeps roots in memory. A restart loses them.

DiscordGatewayClient.layer requires a MessageSessionStore:

DiscordGatewayClient.layer(options).pipe(
Layer.provide(SqliteStore.layer({ filename: "sessions.sqlite" })),
);

The store persists the session and its rendered state. A restarted process can resume the message, and a stale interaction is rejected by the session version.

Available stores:

  • @arona/store-sqlite: Effect SQL with the node SQLite adapter.
  • @arona/store-cloudflare: D1 and KV for Workers.

D1 and SQLite expose consistency: "strong"; KV exposes "eventual" because it has no atomic compare-and-set. Mounting a resumable session on KV fails with SessionStoreInconsistent rather than allowing two workers to overwrite each other; use D1 when concurrency matters.

See Resumable messages for the protocol and the store setup.

Each message mount builds a Layer scope. Provide application services through the command or the mount options:

yield *
mountMessageScoped(ref, App, props, {
layer: Layer.merge(SearchCharacters.layer, FetchHttpClient.layer),
});

The host builds its own services. MessageTarget and the host layer are created per message, so two messages never share a target or a query cache.