Documentation
¶
Overview ¶
templ: version: v0.3.977
Package stream provides a reactive SSE stream backed by NATS pub/sub.
Components register scopes during render via WatchEffect. The stream handler subscribes to NATS subjects for those scopes and pushes stale signals when invalidations occur. Components watch the stale signal via data-effect and reload themselves.
Scopes use colon-separated naming: "invoice:42", "invoices:*", "workspace:1:*". Wildcards map to NATS wildcards (* = single level, > via multi-level scope).
Index ¶
Constants ¶
const ( // SignalNamespace is the Datastar signal namespace for stream stale flags. // The underscore prefix makes it local-only (never sent to backend). SignalNamespace = "_stream" )
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
Connect renders a hidden element that opens the persistent SSE stream. Place this AFTER { children... } in the page layout so all component scopes are accumulated before this component reads them.
If no scopes have been registered, nothing is rendered.
func InitScope ¶
func InitScope(sse *datastar.ServerSentEventGenerator, scope string) error
InitScope pushes a PatchSignals to initialize a stale signal for a scope that wasn't known at initial render (e.g. new rows from infinite scroll).
func ScopeKey ¶
ScopeKey converts a scope string to a safe signal property name. This is the key within the _stream signal namespace.
ScopeKey("invoice:42") → "invoice_42"
ScopeKey("invoices:*") → "invoices_WILD"
ScopeKey("workspace:1:*") → "workspace_1_WILD"
func ScopeSignals ¶
ScopeSignals returns a data-signals value that initializes the stale flags for the given scopes. Place this on the component element so the signals exist before data-effect runs.
stream.ScopeSignals("counter:shared") → "_stream: {\"counter_shared\":false}"
func WatchEffect ¶
WatchEffect registers a scope on the context and returns a data-effect expression string that auto-reloads when the scope goes stale.
stream.WatchEffect(ctx, "invoice:42", "/showcase/api/invoice/42")
// registers scope, returns: "if($_stream.invoice_42) { $_stream.invoice_42 = false; @get('/showcase/api/invoice/42') }"
Types ¶
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker wraps a NATS connection and provides publish/subscribe for scope invalidation. One Broker per application.
func NewBroker ¶
NewBroker creates a Broker from an existing NATS connection. Use embedded NATS or connect to an external NATS server.
func (*Broker) Handler ¶
func (b *Broker) Handler() http.HandlerFunc
Handler returns an http.HandlerFunc that serves the persistent SSE stream. It reads scopes from the "scope" query parameter and subscribes to NATS subjects for each scope (supporting exact and wildcard patterns).
func (*Broker) Invalidate ¶
Invalidate publishes an invalidation message for the given scope. Call this after mutations to notify all connected browsers.
broker.Invalidate("invoice:42")
func (*Broker) InvalidateMany ¶
InvalidateMany publishes invalidation for multiple scopes at once.