Creates a WebSocket handler for use with http_server().
Arguments
- path
URI path for WebSocket connections (e.g., "/ws").
- on_message
Function called when a message is received. Signature:
function(ws, data)wherewsis the connection object anddatais the message. Usews$send()to send responses; the return value is ignored.- on_open
[default NULL] Function called when a connection opens. Signature:
function(ws)- on_close
[default NULL] Function called when a connection closes. Signature:
function(ws)- textframes
[default FALSE] Logical, use text frames instead of binary. When TRUE: incoming
datais character, outgoing data should be character. When FALSE: incomingdatais raw vector, outgoing data should be raw vector.
Value
A handler object for use with http_server().
Connection Object
The ws object passed to callbacks has the following fields and methods:
ws$send(data): Send a message to the client.datacan be a raw vector or character string. Returns 0 on success, or an error code on failure (e.g., if the connection is closed).ws$close(): Close the connection.ws$id: Unique integer identifier for this connection. IDs are unique server-wide across all WebSocket handlers, making them safe to use as keys in a shared data structure.
Examples
# Simple echo server
h <- handler_ws("/ws", function(ws, data) ws$send(data))
# With connection tracking
clients <- list()
h <- handler_ws(
"/chat",
on_message = function(ws, data) {
# Broadcast to all
for (client in clients) client$send(data)
},
on_open = function(ws) {
clients[[as.character(ws$id)]] <<- ws
},
on_close = function(ws) {
clients[[as.character(ws$id)]] <<- NULL
},
textframes = TRUE
)
