example

command
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: MIT Imports: 12 Imported by: 0

README

example/ — the live-feed demo

go run . in this directory starts a live-feed dashboard on :8765 built entirely from go-datastar patches: broadcaster fan-out, MemoryStore replay on reconnect, and the pinned DataStar JS served by ScriptHandler. Zero custom client-side JavaScript.

domain-adapter/ is a second, smaller demo: a domain layer speaking its own events, bridged into patches — see the README section "Patches as values in a domain architecture" in the repository root.

Heartbeat keep-alive

The event handler starts a heartbeat goroutine per connection:

go stream.Heartbeat(request.Context(), heartbeatInterval) // 15s

Stream.Heartbeat (go-sse) writes SSE comment frames on an interval so intermediate proxies and load balancers do not reap idle connections. The context ends the heartbeat when the client disconnects or the handler returns. Typical intervals: 15–30s — shorter wastes bytes, longer risks proxy idle timeouts (nginx's default proxy_read_timeout is 60s).

Drop observability

The broadcaster registers go-sse's WithOnDrop callback, logging events that are dropped when a subscriber's buffer is full (ondrop_test.go exercises it). This is deliberately a broadcaster-level concern: go-sse's OnDrop lives on Broadcaster, not Stream, so Response (which wraps a stream) cannot expose it. Route lossy-feed logging through your broadcaster, not through per-connection responses.

docker-compose

docker-compose.yml builds and runs the demo; the Dockerfile pattern is a plain multi-stage Go build (no Node toolchain — the JS bundle is embedded). The build context is the repository root (the example is part of the root Go module), so build with docker build -f example/Dockerfile . or via compose; a bare docker build example/ fails by design (no go.mod in that context).

Compression

gzipSSEMiddleware (see sse_middleware.go) shows the recommended way to compress SSE through the HTTP layer — per-event flushing included.

Documentation

Overview

Example: live feed using go-datastar patches as values with go-sse Broadcaster.

Run: go run ./example/ Open: http://localhost:8765

Slow-client behavior: when a subscriber's buffer is full the broadcaster drops events for that subscriber only — observe it with sse.WithOnDrop, as done below (see TestWithOnDropFiresWhenSubscriberBufferFull for the pattern in test form).

Directories

Path Synopsis
Command domain-adapter demonstrates the "patches as values" architecture: a domain layer speaks its own events, an EventBridge translates them into go-datastar Patch values, and the transport layer stores/replays/broadcasts those values without knowing anything about the domain.
Command domain-adapter demonstrates the "patches as values" architecture: a domain layer speaks its own events, an EventBridge translates them into go-datastar Patch values, and the transport layer stores/replays/broadcasts those values without knowing anything about the domain.

Jump to

Keyboard shortcuts

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