Skip to content

OTP Actors

Use message.select when your process must receive Gun events and application commands. Add your command subject to the same selector. You do not need caller Erlang FFI or a process that forwards Gun messages.

Gun sends connection messages to the process that opens it. Call connection.open inside actor.new_with_initialiser, then return your command subject with actor.returning. The init function waits for connection setup and upgrade. After init, the actor receives frames and commands in its Gleam loop. Monitor the connection with connection.monitor and add only that monitor through process.select_specific_monitor: an unexpected Gun process exit can occur without a gun_down event.

The complete example below connects to a local echo server at ws://localhost:8080/echo. It sends a text command, receives the echo, then closes. The example package adds gleam_otp; Gluegun does not require it.

import gleam/erlang/process.{type Subject}
import gleam/io
import gleam/otp/actor
import gleam/result
import gleam/string
import gluegun/connection
import gluegun/error
import gluegun/message
import gluegun/websocket
pub type Command {
SendText(text: String, reply: Subject(Result(Nil, error.GluegunError)))
Inspect(reply: Subject(Nil))
Close(reply: Subject(Result(Nil, error.GluegunError)))
Gun(Result(message.Event, error.GluegunError))
ConnectionExited(process.Down)
}
pub type Notice {
FrameReceived(message.Frame)
Lifecycle(message.Event)
Failed(error.GluegunError)
}
type State {
State(
socket: websocket.Socket,
monitor: process.Monitor,
notices: Subject(Notice),
)
}
/// Open and own Gun inside actor init. The loop receives Gun events and
/// application commands through one selector, without caller Erlang FFI.
pub fn start(
host: String,
port: Int,
path: String,
upgrade_options: websocket.UpgradeOptions,
notices: Subject(Notice),
) -> actor.StartResult(Subject(Command)) {
actor.new_with_initialiser(11_000, fn(commands) {
use source <- result.try(
connection.options()
|> connection.with_protocols([connection.Http1])
|> connection.open(host:, port:)
|> result.map_error(string.inspect),
)
let monitor = connection.monitor(source)
let timeout = connection.default_timeout()
let setup = {
use protocol <- result.try(connection.await_up(source, timeout))
use stream <- result.try(websocket.upgrade(
source,
protocol,
path,
[],
upgrade_options,
))
use handshake <- result.try(websocket.await_upgrade(
source,
stream,
timeout,
))
Ok(websocket.from_upgrade(source, stream, handshake, timeout))
}
case setup {
Ok(socket) -> {
let selector =
process.new_selector()
|> process.select(commands)
|> message.select(Gun)
|> process.select_specific_monitor(monitor, ConnectionExited)
Ok(
actor.initialised(State(socket, monitor, notices))
|> actor.selecting(selector)
|> actor.returning(commands),
)
}
Error(error) -> {
process.demonitor_process(monitor)
case connection.terminate(source) {
Ok(Nil) -> Nil
Error(cleanup_error) -> process.send(notices, Failed(cleanup_error))
}
Error(string.inspect(error))
}
}
})
|> actor.on_message(handle)
|> actor.start
}
fn handle(state: State, command: Command) -> actor.Next(State, Command) {
case command {
SendText(text, reply) -> {
process.send(reply, websocket.send_text(state.socket, text))
actor.continue(state)
}
Inspect(reply) -> {
process.send(reply, Nil)
actor.continue(state)
}
Close(reply) -> {
process.demonitor_process(state.monitor)
process.send(reply, websocket.close(state.socket))
actor.stop()
}
Gun(Ok(message.StreamMessage(envelope))) -> {
case websocket.record_frame(state.socket, envelope) {
Ok(frame) -> {
process.send(state.notices, FrameReceived(frame))
actor.continue(state)
}
Error(error) -> fail(state, error)
}
}
Gun(Ok(message.ConnectionDown(_, _, _, _) as event)) -> {
process.send(state.notices, Lifecycle(event))
stop_socket(state)
}
Gun(Ok(message.ConnectionUp(_, _) as event))
| Gun(Ok(message.TunnelUp(_, _, _) as event)) -> {
process.send(state.notices, Lifecycle(event))
actor.continue(state)
}
Gun(Ok(message.StreamFailure(_, _, error)))
| Gun(Ok(message.ConnectionFailure(_, error)))
| Gun(Error(error)) -> fail(state, error)
ConnectionExited(process.ProcessDown(_, _, reason)) ->
fail(state, error.ConnectionDown(string.inspect(reason)))
ConnectionExited(process.PortDown(_, _, _)) ->
fail(state, error.InvalidMessage("expected a Gun process monitor"))
}
}
fn fail(state: State, error: error.GluegunError) -> actor.Next(State, Command) {
process.send(state.notices, Failed(error))
stop_socket(state)
}
fn stop_socket(state: State) -> actor.Next(State, Command) {
process.demonitor_process(state.monitor)
case websocket.close(state.socket) {
Ok(Nil) -> Nil
Error(error) -> process.send(state.notices, Failed(error))
}
actor.stop()
}
pub fn main() -> Nil {
let notices = process.new_subject()
let assert Ok(started) =
start("localhost", 8080, "/echo", websocket.upgrade_options(), notices)
let assert Ok(Nil) = process.call(started.data, 2000, SendText("hello", _))
let assert Ok(FrameReceived(message.Text(reply))) =
process.receive(notices, 2000)
io.println(reply)
let assert Ok(Nil) = process.call(started.data, 2000, Close)
Nil
}

Run it from examples/websocket_actor with gleam run. Its tests use a local wire peer and need no external server.

message.select registers supported Gun tuple tags and arities across the receiver's mailbox. It cannot select only one connection. For multiple connections or streams, match the source handles in the event before handling it. StreamMessage contains the existing Envelope; StreamFailure and ConnectionFailure keep their source handles with the typed error.

ConnectionUp, ConnectionDown, and TunnelUp describe lifecycle changes. ConnectionDown includes the protocol, error, and killed streams. With HTTP retry enabled, a down event can precede a later up event. After a WebSocket upgrade, Gun reports WebSocketProtocol. Reconnecting as HTTP does not restore the upgrade.

Malformed supported tuples reach your mapping as DecodeError. Malformed stream payloads with valid handles arrive as StreamFailure. The selector does not register arbitrary application messages, unsupported tuple arities, or external DOWN messages. Add your own subject and monitor handlers as needed. The OTP actor itself logs and discards messages outside its selector.

Pass each selected WebSocket Envelope to websocket.record_frame(socket, envelope) exactly once. It checks both source handles and records peer-close and delivered-frame credit for websocket.close. A different source or an HTTP envelope returns InvalidMessage without changing socket state.

Use this bridge for text, binary, visible ping/pong, and close frames. Gun answers incoming pings itself. For finite credit, replenish during normal processing through request.update_flow(connection, stream, increment) using the envelope's handles. Recording a frame does not replenish normal receive credit; close reimburses delivered credit for teardown.

Keep receiving and closing in the actor that owns the socket. The example reports close errors to the caller. A close command waits for the peer under the socket timeout and then stops the actor; it does not process other commands during that close handshake.

See the message reference and WebSocket reference for the event and recording APIs. Existing Message variants and message.decode retain their behavior.