atomvm_gleam/emscripten_websocket
Browser WebSocket NIF wrappers for AtomVM’s emscripten-only websocket
module (avm_emscripten).
Emscripten / browser only. This is distinct from
atomvm_gleam/websocket, which wraps the
ESP-IDF atomvm_websocket_client port driver.
Upstream Erlang module is websocket (called via FFI); the Gleam path is
emscripten_websocket to avoid clashing with the ESP client module.
Source:
libs/avm_emscripten/src/websocket.erl.
Docs:
Module websocket.
Inbound owner messages
The controlling process receives these mailbox messages (from the NIF):
{websocket_open, Websocket}— connection opened{websocket, Websocket, Data}— text or binary payload (Datais a binary){websocket_error, Websocket}— error event{websocket_close, Websocket, {WasClean, Code, Reason}}— closed;WasCleanis a boolean,Codean integer status,Reasona binary
Check atomvm.platform() == Emscripten (or call is_supported)
before using this module on mixed-target builds.
Types
Errors from the emscripten websocket NIF.
pub type Error {
Failed
NotSupported
Badarg
Timeout
NotOwner
SocketClosed
Other(String)
}
Constructors
-
Failed -
NotSupported -
Badarg -
Timeout -
NotOwnerCaller is not the current controlling process.
-
SocketClosedSocket is already closed (
{error, closed}upstream). -
Other(String)
Connection ready-state from ready_state.
pub type ReadyState {
Connecting
Open
Closing
Closed
}
Constructors
-
Connecting -
Open -
Closing -
Closed
Values
pub fn buffered_amount(ws: Websocket) -> Result(Int, Error)
Bytes queued with send but not yet transmitted.
pub fn close(ws: Websocket) -> Result(Nil, Error)
Close with status code 1000 (normal) and an empty reason.
See websocket:close/1.
pub fn close2(
ws: Websocket,
status_code: Int,
) -> Result(Nil, Error)
Close with the given status code and an empty reason.
See websocket:close/2.
pub fn close3(
ws: Websocket,
status_code: Int,
reason: String,
) -> Result(Nil, Error)
Close with status code and reason string.
See websocket:close/3.
pub fn controlling_process(
ws: Websocket,
owner: process.Pid,
) -> Result(Nil, Error)
Transfer ownership to owner. Must be called by the current owner.
pub fn extensions(ws: Websocket) -> Result(String, Error)
Extensions selected by the server, if any.
pub fn is_supported() -> Bool
true if browser WebSockets are available in this environment.
pub fn new(url: String) -> Result(Websocket, Error)
Open a WebSocket to url with default protocols and self() as owner.
Equivalent to new3 with [] protocols and the calling process.
See websocket:new/1.
pub fn new2(
url: String,
protocols: List(String),
) -> Result(Websocket, Error)
Open a WebSocket to url negotiating protocols, with self() as owner.
See websocket:new/2.
pub fn new3(
url: String,
protocols: List(String),
owner: process.Pid,
) -> Result(Websocket, Error)
Open a WebSocket to url with protocols and explicit owner process.
See websocket:new/3.
pub fn protocol(ws: Websocket) -> Result(String, Error)
Subprotocol selected by the server, if any.
See websocket:protocol/1.
pub fn ready_state(ws: Websocket) -> Result(ReadyState, Error)
Current connection ready-state.
pub fn url(ws: Websocket) -> Result(String, Error)
URL used to open the socket.
See websocket:url/1.