Documentation Index
Fetch the complete documentation index at: https://mintlify.com/MercuryWorkshop/epoxy-tls/llms.txt
Use this file to discover all available pages before exploring further.
connect_websocket() establishes a WebSocket connection to a remote server by tunneling through your Wisp proxy. You register event callbacks through an EpoxyHandlers object before connecting, and the returned EpoxyWebSocket instance exposes a send() method for writing data in either direction.
connect_websocket() and EpoxyHandlers are only available in the full build of Epoxy TLS. The minimal build does not include WebSocket connect support. Import from @mercuryworkshop/epoxy-tls or @mercuryworkshop/epoxy-tls/epoxy to get the full build. See Bundle Variants.Creating EpoxyHandlers
EpoxyHandlers wraps the four lifecycle callbacks that map to familiar WebSocket events. All four are required — the constructor will reject with an error if any are missing.
Connecting to a WebSocket server
Callconnect_websocket(handlers, url, protocols, headers) on your EpoxyClient instance. The call is async and resolves once the opening handshake completes.
| Parameter | Type | Description |
|---|---|---|
handlers | EpoxyHandlers | Event callbacks created above |
url | string | URL | WebSocket URL (ws:// or wss://) |
protocols | string[] | Requested subprotocols (may be empty) |
headers | Headers | { [key: string]: string } | Extra HTTP upgrade headers |
Requesting subprotocols
Pass the subprotocol names you want to negotiate in the third argument. The server selects one from the list (or rejects them all).Sending custom upgrade headers
Provide arbitrary HTTP headers that are forwarded with theGET upgrade request. This is useful for authentication tokens or proprietary handshake parameters.
Sending messages
EpoxyWebSocket.send() accepts either a UTF-8 text string or an ArrayBuffer. The type accepted is string | ArrayBuffer — pass TypedArray.buffer if you have a typed array such as Uint8Array.
Complete working example
The following example mirrors the WebSocket test found indemo.js. It connects to a public echo server, sends incrementing messages on a timer, and logs every echoed reply.
Error handling and cleanup
Theonerror callback receives a JavaScript Error describing what went wrong — network failures, TLS errors, and invalid frames all surface here. Use it to update UI state and decide whether to reconnect.
The onclose callback fires after the connection has fully closed, regardless of which side initiated the close. Perform any teardown (timers, UI updates, reconnect logic) here rather than relying on onerror alone, since a clean close does not trigger onerror.