Socket¶
User-facing API passed to LiveView lifecycle methods.
Wraps assigns and provides framework actions like pushing server-initiated events to the client, subscribing to PubSub topics, obtaining a shareable connection reference, and managing stateful components.
Constructors¶
create¶
new ref create(
assigns: Assigns ref,
pending_events: Array[(String val , (JsonObject val | JsonArray val | String val | I64 val | F64 val | Bool val | None val))] ref,
self': InfoReceiver tag,
pub_sub: PubSub tag,
components: _ComponentRegistry ref)
: Socket ref^
Parameters¶
- assigns: Assigns ref
- pending_events: Array[(String val , (JsonObject val | JsonArray val | String val | I64 val | F64 val | Bool val | None val))] ref
- self': InfoReceiver tag
- pub_sub: PubSub tag
- components: _ComponentRegistry ref
Returns¶
- Socket ref^
Public Functions¶
assign¶
Set an assign value. The framework re-renders after the current handler if any assigns changed.
Parameters¶
- key: String val
- value: (String val | TemplateValue box)
Returns¶
- None val
get_assign¶
Read an assign value.
Parameters¶
- key: String val
Returns¶
- TemplateValue box ?
push_event¶
Push a server-initiated event to the client. Events are queued and flushed after the current render cycle.
fun ref push_event(
event: String val,
payload: (JsonObject val | JsonArray val | String val |
I64 val | F64 val | Bool val |
None val))
: None val
Parameters¶
- event: String val
- payload: (JsonObject val | JsonArray val | String val | I64 val | F64 val | Bool val | None val)
Returns¶
- None val
self¶
Return a shareable reference to this connection.
Pass this to external actors so they can send messages via
InfoReceiver.info, which arrive at LiveView.handle_info.
Returns¶
- InfoReceiver tag
connected¶
True when this socket is backed by a live WebSocket connection. False
during HTTP rendering via PageRenderer, where PubSub operations are
no-ops and push events are silently dropped.
Check this in mount to distinguish the two contexts — for example,
to skip subscribing to PubSub topics during HTTP render.
Returns¶
- Bool val
subscribe¶
Subscribe this connection to a PubSub topic. Messages published to
the topic will arrive via LiveView.handle_info. Subscriptions are
automatically cleaned up when the connection closes.
No-op on disconnected sockets (during HTTP rendering).
Parameters¶
- topic: String val
Returns¶
- None val
unsubscribe¶
Unsubscribe this connection from a PubSub topic.
No-op on disconnected sockets (during HTTP rendering).
Parameters¶
- topic: String val
Returns¶
- None val
register_component¶
Register a stateful component with the given ID. Returns false if the per-connection component limit is reached.
The component's mount is called immediately. The ID must be unique
within this connection -- registering a duplicate ID replaces the
existing component (the old component is discarded).
Parameters¶
- id: String val
- component: LiveComponent ref
Returns¶
- Bool val
unregister_component¶
Remove a stateful component. Its state is discarded and events targeting this ID will produce errors.
Parameters¶
- id: String val
Returns¶
- None val
update_component¶
Update a component's assigns with new data and call its update
callback. Use this to pass data from the parent view to a component.
fun ref update_component(
id: String val,
data: Array[(String val , (String val | TemplateValue box))] val)
: None val
Parameters¶
- id: String val
- data: Array[(String val , (String val | TemplateValue box))] val
Returns¶
- None val