Skip to content
Strata is pre-1.0. APIs, protocol, and storage format may change.

strata_transport/browser

Browser WebSocket bridge.

A native browser event for a WebSocket close or error.

pub type CloseEvent

Why a browser send did not happen.

pub type SendError {
SocketNotOpen
}

A handle to a native browser WebSocket.

The browser owns the underlying connection.

pub type Socket

Start the browser WebSocket close operation.

pub fn close(Socket) -> Nil

Open a browser WebSocket and return its handle immediately.

The browser calls on_open when the connection opens. It calls on_message only for text frames. It calls on_close for a close or error event.

pub fn open_socket(
String,
fn(Socket) -> Nil,
fn(String) -> Nil,
fn(CloseEvent) -> Nil
) -> Socket

Create a local replica identifier.

The function prefers browser cryptographic random APIs. It uses Math.random only when those APIs are not available. Do not use the value as an authentication secret.

pub fn random_replica_id() -> String

Send one text frame. Returns Error(SocketNotOpen) when the browser socket is not open.

pub fn send_text(
Socket,
String
) -> Result(Nil, SendError)

Build a WebSocket URL with one encoded token query parameter.

Set secure to True to use wss. Set it to False to use ws.

pub fn websocket_url(
host: String,
port: Int,
path: String,
token: String,
secure: Bool
) -> String