stream

package
v0.0.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 12 Imported by: 0

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

View Source
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

func Connect() templ.Component

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

func ScopeKey(scope string) string

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

func ScopeSignals(scopes ...string) string

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

func WatchEffect(ctx context.Context, scope string, reloadURL string) string

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

func NewBroker(conn *nats.Conn, opts ...Option) *Broker

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

func (b *Broker) Invalidate(scope string) error

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

func (b *Broker) InvalidateMany(scopes ...string) error

InvalidateMany publishes invalidation for multiple scopes at once.

type Option

type Option func(*Broker)

Option configures the Broker.

func WithSubjectPrefix

func WithSubjectPrefix(prefix string) Option

WithSubjectPrefix overrides the default NATS subject prefix ("webx.scope").

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL