Skip to content

Client Connection

SpacetimeDB: Client Connection ↗

Stdb.project(Module.spec) produces a client-safe projection. It contains no server runtime; it is only the callable, route, table, and subscription shape needed by clients.

import * as Effect from "effect/Effect"
import * as Stdb from "effect-spacetimedb"
declare const Module: Stdb.AnyStdbModule
declare const uri: string
declare const databaseName: string
declare const token: string
const Example = Stdb.project(Module.spec)
const HttpLive = Example.client.http.layerFetch({ uri, databaseName, token })
const program = Effect.gen(function* () {
const client = yield* Example.client.http.Tag
yield* client.reducers.user_upsert({ userId: "u1", name: "Ada" })
}).pipe(Effect.provide(HttpLive))

Typed HTTP handlers are called with just their payload. Raw handlers take the payload and request options.

Typed post, put, and patch routes can be projected into a stock Effect HttpApi:

const api = Stdb.toHttpApi(Module.spec)

This is useful when callers already use HttpApiClient.make(...) and want status-based declared-error decoding.

WebSocket sessions use generated native bindings through effect-spacetimedb/client.

import * as StdbClient from "effect-spacetimedb/client"
import * as Stdb from "effect-spacetimedb"
declare const Module: Stdb.AnyStdbModule
declare const DbConnection: StdbClient.GeneratedWsConnectionFactory<
typeof Module.spec,
unknown
>
declare const uri: string
declare const databaseName: string
declare const token: string
const Example = Stdb.project(Module.spec)
const WsLive = Example.client.ws.layerGenerated({
DbConnection,
uri,
databaseName,
token,
})

layerGenerated and scopedGenerated are the primary generated-client paths. The lower-level layer(GeneratedWs.adapter(...)) form is still available when you need to build the adapter separately.

Named tags let one process hold multiple sessions for the same module.

import * as StdbClient from "effect-spacetimedb/client"
import * as Stdb from "effect-spacetimedb"
declare const Module: Stdb.AnyStdbModule
declare const DbConnection: StdbClient.GeneratedWsConnectionFactory<
typeof Module.spec,
unknown
>
declare const uri: string
declare const databaseName: string
declare const token: string
const Example = Stdb.project(Module.spec)
const MainSession = Example.client.ws.tag("main")
const MainLive = Example.client.ws.layerGenerated(
{ DbConnection, uri, databaseName, token },
{ name: "main" },
)

The native generated client still owns the actual WebSocket connection. effect-spacetimedb layers typed Effect services, scoped sessions, and projected HTTP clients over the module contract.