Skip to content

Socket

[Source]

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.

class ref Socket

Constructors

create

[Source]

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

Returns


Public Functions

assign

[Source]

Set an assign value. The framework re-renders after the current handler if any assigns changed.

fun ref assign(
  key: String val,
  value: (String val | TemplateValue box))
: None val

Parameters

Returns


get_assign

[Source]

Read an assign value.

fun box get_assign(
  key: String val)
: TemplateValue box ?

Parameters

Returns


push_event

[Source]

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

Returns


self

[Source]

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.

fun box self()
: InfoReceiver tag

Returns


connected

[Source]

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.

fun box connected()
: Bool val

Returns


subscribe

[Source]

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).

fun ref subscribe(
  topic: String val)
: None val

Parameters

Returns


unsubscribe

[Source]

Unsubscribe this connection from a PubSub topic.

No-op on disconnected sockets (during HTTP rendering).

fun ref unsubscribe(
  topic: String val)
: None val

Parameters

Returns


register_component

[Source]

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).

fun ref register_component(
  id: String val,
  component: LiveComponent ref)
: Bool val

Parameters

Returns


unregister_component

[Source]

Remove a stateful component. Its state is discarded and events targeting this ID will produce errors.

fun ref unregister_component(
  id: String val)
: None val

Parameters

Returns


update_component

[Source]

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

Returns