Slap.Yjs.Presence (slap_yjs v0.3.0)

Copy Markdown View Source

Publishes an awareness state for a server-side participant, such as an agent or tool. The process subscribes to a document and keeps one client id for its lifetime.

Awareness is Yjs's term for ephemeral participant state. To inspect and authorize client awareness messages, use the Slap.Yjs.Awareness codec.

{:ok, presence} =
  Slap.Yjs.Presence.start_link(
    module: MyApp.DocServer,
    doc_id: {"my-service", "doc-1"},
    state: %{"user" => %{"name" => "Assistant"}}
  )

:ok =
  Slap.Yjs.Presence.update(presence, %{
    "user" => %{"name" => "Assistant"},
    "busy" => true
  })

The process republishes its state every :renew_every milliseconds (15,000 by default). y-protocols clients remove a state if its clock does not advance for 30 seconds. If the document server stops, the process rejoins and republishes. Transient rejoin failures are retried after 1 s, backing off to at most 30 s. Updates received while disconnected are published on rejoin. Stopping the presence process removes its state. Deleting the document stops the process without a supervisor restart.

Options:

  • :module (required) - the document server module, as for Slap.Yjs.Docs.join/3.
  • :doc_id (required) - the document.
  • :state (required) - a JSON map with string keys. Nested maps also require string keys; strings must be valid UTF-8, and integers must fit in signed 64 bits. Invalid states raise ArgumentError in the caller.
  • :renew_every - milliseconds between renewals (default 15,000).
  • :docs, :timeout - as for Slap.Yjs.Docs.join/3.
  • :name - the process name.

Summary

Functions

Returns a specification to start this module under a supervisor.

Starts a linked presence process and publishes its initial state.

Sets state, publishing it now if connected, or on rejoin.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts a linked presence process and publishes its initial state.

update(presence, state, timeout \\ 5000)

@spec update(GenServer.server(), map(), timeout()) :: :ok

Sets state, publishing it now if connected, or on rejoin.