Skip to Content
DevelopWebSocket client

WebSocket Client

Golem agents use WASI HTTP for outgoing HTTP requests, but WASI HTTP does not support WebSocket upgrades. Golem 1.5 introduces a dedicated WebSocket client API (golem:websocket@1.5.0) that complements WASI HTTP and allows agents to open WebSocket connections, send and receive messages, and handle connection lifecycle events.

The Golem TypeScript SDK provides a WebSocket client API. connectWebsocket opens a connection and returns a WebSocketHandle with send, receive, receiveWithTimeout, and close methods. Received messages are { tag: "text", val } / { tag: "binary", val }, and a graceful close surfaces as a WebsocketError with tag === "closed" and a closeInfo.

import { z } from "zod"; import { defineAgent, method, connectWebsocket, WebsocketError } from "@golemcloud/golem-ts-sdk"; export const ExampleAgent = defineAgent({ name: "ExampleAgent", id: { name: z.string() }, methods: { run: method({ input: {}, returns: z.void() }), }, }); export const ExampleAgentImpl = ExampleAgent.implement({ init: () => ({}), methods: { async run() { const ws = await connectWebsocket("wss://example.com/chat"); console.log("Connected"); ws.send("Hello, server!"); try { for (;;) { const message = await ws.receive(); if (message.tag === "text") { console.log("Text:", message.val); } else { console.log("Binary:", message.val); } } } catch (err) { if (err instanceof WebsocketError && err.tag === "closed") { const info = err.closeInfo; if (info) { console.log(`Closed [${info.code}] "${info.reason}"`); } } else { throw err; } } }, }, });

Runtime reconstruction and session loss

A physical WebSocket session does not survive runtime reconstruction, whether caused by suspension, restart, or crash. Choose a reconstruction policy when you connect:

  • reconnect-automatically is the default when the option is absent. The first live access to a reconstructed successful handle opens a replacement connection. Your application must tolerate a new physical session.
  • report-connection-loss prevents replacement I/O on that handle. The first live send, receive, receive-with-timeout, or close returns the typed session-lost error and makes the handle terminal. Further accesses return that loss; they do not reconnect. Dropping the handle only releases it locally.

Both policies replay recorded outcomes unchanged, including messages, timeouts, send results, and close results. Neither changes retry behavior for an ordinary live network disconnect. Session loss is not proof that an earlier send never reached the peer. Use application acknowledgments, deduplication, or reconciliation before resending effects.

Recovery repairs eligible incomplete durable calls at their own recorded start and retains completed calls and their outcomes. It does not roll back an atomic region or repeat a completed connect as a new call. The reconstruction policy controls what happens when live code next accesses the reconstructed handle.

SDK policy and error forms

TypeScript accepts ConnectOptions.reconstructionPolicy:

const ws = await connectWebsocket("wss://example.com/chat", { reconstructionPolicy: "report-connection-loss", }); // Catch WebsocketError from send, receive, receiveWithTimeout, or close: // err.tag === "session-lost"