Realtime
Live updates in both directions: server-sent events for one-way feeds, WebSocket topics for anything browsers also write to, and typed event payloads generated from the Go source.
Server-sent events#
borgo.SSE(w, r) turns any handler into an event stream — it writes the headers and flushes them at once, so the browser's EventSource opens before the first event, and returns the stream, with Send(event, data) (data JSON-encoded), Ping() to keep idle proxies from closing it, and Done() for the end of the stream — the client disconnecting, or the server beginning to shut down:
//borgo:route GET /api/ticker
func Ticker(w http.ResponseWriter, r *http.Request) {
stream, err := borgo.SSE(w, r)
if err != nil {
return
}
for {
select {
case tick := <-ticks:
stream.Send("tick", tick)
case <-stream.Done():
return
}
}
}
borgo.NewSSEHub() adds broadcast — hub.Publish(event, data) from anywhere, hub.ServeHTTP as the route handler:
var events = borgo.NewSSEHub()
//borgo:route GET /api/events
func Events(w http.ResponseWriter, r *http.Request) {
events.ServeHTTP(w, r)
}
//borgo:route POST /api/tasks
func CreateTask(w http.ResponseWriter, r *http.Request) {
body, err := borgo.Bind[TaskCreate](r)
if err != nil {
borgo.BindError(w, err)
return
}
task := Task{Title: body.Title}
events.Publish("task-created", task)
borgo.JSON(w, http.StatusCreated, TaskItem{Task: task})
}
An event stream is an ordinary /api route: everyone who can open it gets every event published on its hub, logged in or not. Wrap it in borgo.Authed, or cover it in api/middleware.go, when the events are not public.
And in the page:
import { useEffect, useState } from "react";
import type { Task } from "@/.borgo/api-types";
export default function Tasks({ tasks: initial }: { tasks: Task[] }) {
const [tasks, setTasks] = useState(initial);
useEffect(() => {
const source = new EventSource("/api/events");
const refresh = async () => setTasks((await (await fetch("/api/tasks")).json()).tasks);
source.addEventListener("task-created", refresh);
return () => source.close();
}, []);
return <ul>{tasks.map((t) => <li key={t.ID}>{t.title}</li>)}</ul>;
}
Send and Ping return an error, and it is worth reading: a stream the handler ended itself with stream.Close() answers every later write with borgo.ErrStreamClosed (one value, errors.Is-able), while a stream the client dropped or the server is shutting down reports whatever the connection reported — that is the more useful answer, and Done() is where you watch for it. Close() is for the handler that detached the request context (context.WithoutCancel, a clone onto a background context) and so holds a stream no disconnection will ever end: idempotent, safe from any goroutine, and it writes nothing — the response ends when the handler returns. The hub has the same pair: hub.Subscribers() for the live count (publish it on a timer for presence, or skip producing when it is zero) and hub.Close(), which ends every open stream and makes later Publish calls no-ops.
Create a task in one tab, watch it appear in the other. The stream is written by the process that answers the port, nothing between it and the socket buffers, so a plain EventSource works with no client library, and the whole thing is standard library on the Go side.
A publish never blocks on a slow subscriber — a subscriber whose buffer is full misses that frame rather than stalling everyone else, which is one more reason to refetch state rather than apply deltas — and a subscriber that disappears without closing its connection — a laptop lid, a dropped mobile connection — is reaped rather than holding a goroutine and a file descriptor forever: every write carries a 10-second deadline, so a stream nobody is reading errors out on its next frame and unsubscribes itself.
WebSocket topics#
A topic is what your app says it is. Every topic a socket asks for is put to
borgo.CanSubscribeat the handshake, with the session already verified, before the socket is upgraded — see who may hold a topic. Without a check,borgo devleaves every topic open and says so at startup;borgo startrefuses every subscription, and says so too. Through 0.22 nothing was asked: an anonymous client with no cookie read a private channel by guessing its name, and__counttold it how many people were in the room. That is the hole the check closes.And a payload is not proof it came from Go. Without
borgo.CanPublish, every socket a topic admits may publish any event on it - the onesPushsends included, with any data. See who may hold a topic.
The Go process that answers the public port is also the WebSocket server: /ws is answered in-process, behind api/middleware.go and the same session verification as every page, by a relay written on net/http alone. Browsers join named topics with the subscribe helper; every {event, data} published on a topic reaches every subscriber, including the publisher's other tabs:
import { subscribe } from "borgo-framework";
const channel = subscribe("live", (event, data) => { /* ... */ });
channel.publish("message", "hello"); // browser -> everyone on the topic
channel.close();
subscribe returns the channel, not a cleanup function, so useEffect(() => subscribe(...)) is a type error - deliberately: the socket has to be closed when the component goes, or every navigation leaves one open. In a component, open it in the effect and close it in the effect's cleanup:
import { useEffect, useState } from "react";
import { subscribe } from "borgo-framework";
export function Presence({ topic }: { topic: string }) {
const [count, setCount] = useState(0);
useEffect(() => {
const channel = subscribe(topic, (event, data) => {
if (event === "__count") setCount(data as number);
});
return () => channel.close();
}, [topic]);
return <p>{count} here</p>;
}
The built-in __count event reports the topic's subscriber count (presence for free), and the connection reconnects itself. When presence needs names and not a number, borgo.Subscribers[Me](topic) is the Go side of the same fact — the session of every socket holding the topic, one entry per socket — and the count event, which reaches every subscriber on each join and leave, is the moment for a page to ask a route of yours for them again. One close is final: a socket the server refuses — the handshake's Origin is not the server's own, or there is no Origin and BORGO_WS_ALLOW_NO_ORIGIN is unset, or a topic CanSubscribe said no to — is closed with code 4403 and a reason naming the origin, or the switch, or the topic; subscribe stops redialling, and reports the reason to an onRefused callback — subscribe(topic, handler, { onRefused }) — or to console.error without one. Every other close keeps reconnecting with backoff.
On the Go side, borgo.Push(topic, event, data) publishes into the same topics. Since 0.30 that is a call inside the process, not a request: there is no endpoint, no key, and nothing crosses the network. It returns an error for an event no subscriber could ever receive — a topic no handshake could name (empty, blank, over 128 characters, containing a comma), or a payload over the 1 MB a socket may carry — and nil for a topic nobody holds right now, which is delivered to nobody. An app that mounts borgo's handlers on a server of its own mounts borgo.Realtime() at /ws — one relay per process, configured from the environment, its 101 carrying the security headers — and Push reaches it the same way.
borgo.Push("live", "task-created", task.Title)
Who may hold a topic#
borgo.CanSubscribe registers the check every topic a socket asks for must pass. It runs once per topic at the handshake, before the socket is upgraded, with the request the socket came in on — so it reads the session with GetSession as a handler would, the front having verified it, and a CheckSession you registered is consulted once for the whole handshake. A refused topic closes the socket with 4403 and a reason naming the topic; __count is a subscription like any other, so a refused topic's count is never seen either.
type User struct{ ID, Name string }
func init() {
borgo.CanSubscribe(func(r *http.Request, topic string) bool {
if topic == "lobby" {
return true
}
user, ok := borgo.GetSession[User](r)
return ok && topic == "user:"+user.ID
})
}
Key a per-user topic by an id the app assigns, not by a name the user chose: a topic cannot hold a ,, is at most 128 characters and loses the whitespace around it, so a chosen name can leave its owner with a channel the relay will not open. examples/tasks keys user:<id> for this reason.
A socket is judged once, at its handshake, and keeps its topics until it ends: a session revoked or expired afterwards is refused at its next handshake, and so is a member removed from a channel. When that is too late — and for a private room it is — the handler that made the change calls borgo.Disconnect after storing it:
func removeMember(w http.ResponseWriter, r *http.Request, removed string) {
// ... the membership is deleted, and the transaction committed ...
borgo.Disconnect(func(me Me, topics []string) bool { return me.Username == removed })
}
It closes every socket whose session the function accepts and reports how many. The session is the one the handshake read, decoded into the type you give SetSession; a socket with no session is never matched. The close is not a refusal: the browser's subscribe dials again within a second and each socket passes CanSubscribe again, with the session read again — so the rule still lives in one place, a socket that may keep its topic is back at once, and one that may not is refused for good. A handshake that was being checked while Disconnect ran is not let in on that answer either: it is closed the same way, and dials again. A Disconnect therefore costs every handshake in flight a second of delay and nothing more — one pass of checks per handshake, however often it is called. Called faster than a handshake takes, though, it lets nobody in until the calls stop: after a change that touches many people, call it once, with a function that accepts all of them, not once for each. Call it after the change is stored, never before: a socket that redials faster than the commit is judged by the old rows.
A check that panics refuses — failing open would hand a private topic to whoever hits the panic — and its stack is logged once per process. borgo.CanPublish decides a browser's publish separately, per message, for the topic it names; without one, publishing follows CanSubscribe — a socket may publish to what it may hold. A refused message is dropped, the socket stays open, and the refusal is logged once per socket. Go's own Push is never asked: it is your code publishing. And a subscriber cannot tell the two apart: a browser's publish reaches the others as the same {event, data} a Push would, so without a CanPublish any socket a topic admits may emit any event name declared for it, typed or not, with whatever data it likes — the type map is a compile-time promise about your code, not a wire check.
CanPublish answers yes or no; it cannot make a message true. A socket allowed to publish typing can publish it under anybody's name, and the only way round that used to be a POST that Go turned into a Push. borgo.OnPublish registers a stamp instead: every message a browser publishes passes through it after CanPublish, and what it returns is what the subscribers receive.
type Typing struct {
Username string `json:"username"`
}
func init() {
borgo.OnPublish(func(r *http.Request, topic, event string, _ json.RawMessage) (json.RawMessage, bool) {
me, ok := borgo.GetSession[Me](r)
if !ok || event != "typing" {
return nil, false
}
data, err := json.Marshal(Typing{Username: me.Username})
return data, err == nil
})
}
The browser's own data is handed to the stamp and relayed only if the stamp returns it; false drops the message and keeps the socket. A stamp is a list of what browsers may say: the one above lets typing through with the server's word on who sent it, and nothing else. What it returns must be JSON a message can carry — anything else is dropped and logged once per socket, as is a stamp that panics, since a message nobody vouched for should reach nobody. Push is never stamped.
Without a CanSubscribe, borgo dev leaves every topic open to every socket and prints so at startup; borgo start refuses every subscription with 4403 and prints so too. A production relay with no rule is a relay with the wrong rule, and the one that fails loud is the one you notice before a visitor does.
Typed events#
borgo.Push makes the payload visible to static analysis: called with literal topic and event strings, borgogen records the payload type in a generated "topic/event" map, exactly like borgo.JSON[T] types a route. The subscribe callback for that topic then narrows — checking event types data, and an event name nobody declared fails tsc. channel.publish is held to the same map: on a topic with declared events, only a declared event name with its payload type compiles (CI proves both directions with deliberate wrong-payload files). Browser-published events join the map through declaration merging in any .d.ts of the app (see ws-events.d.ts in the tasks example):
declare module "borgo-framework" {
interface WsEvents {
"live/message": string; // browsers publish this one
}
}
Topics with no declared events keep the untyped (event: string, data: unknown) callback.
Computed topics: //borgo:topic#
Every realtime app has per-entity topics — "channel:"+id, "user:"+id — and a computed name has no literal for borgogen to read. Name its shape on the call instead, and the generator keys the map by the pattern and keeps owning the payload type:
//borgo:topic channel:{id}
borgo.Push("channel:"+id, "message", msg)
The directive sits on the line above the call, or at the end of the line the call starts on, and names that one call; literal text and {name} placeholders, no /, no ,, no whitespace, at most 128 characters of literal text (counted as the relay counts, in UTF-16 units). It becomes a template-literal key, \channel:${string}/message`, and subscribe("channel:42", ...)orsubscribe(`channel:${id}`, ...)narrowsdataexactly as a literal topic does;channel.publishis held to it too. A pattern with no placeholder says the topic is exactly that name. Every mistake fails the run and names the line: a directive on aPushwhose topic is already a literal, one on noPushat all, one claimed by two calls, one with a computed event name, a comment that looks like the directive but is not one, a malformed pattern — and a collision, where a literalPushnames a topic the pattern admits with a payload the pattern does not carry, or two patterns nest with different payloads, since the generated file would then type one event two ways, andtscsettles it in silence with no Go line to point at: the literal topic keeps its own payload whatever the pattern says, and two nested patterns type the eventnever, which no handler can read. A typing that silently did not happen is the drift this exists to end. Pushstill accepts a computed topic with no directive — nothing is recorded, the subscriber keeps the untyped callback, and borgogen says nothing about it. Do not hand-write a key for the same pattern in the app's.d.ts: that is the same duplicate index signature, and under skipLibChecknothing reports it. The escape hatch is for browser-published events, which have no Go source to read. One consequence on the client:subscribe(topic)with atopicthat is anystring` is untyped, since there is no name to narrow on — it used to see every event of every literal topic at once.
One naming rule: a topic cannot contain /, which would make the "topic/event" key ambiguous and subscribe the browser to the part before the slash. Such a push still delivers — refusing to generate would break a working app over a typing detail — so borgogen warns, names the file and line, and leaves that event untyped.
Typing nuances#
Channel.publish is declared with method syntax on purpose: TypeScript checks method parameters bivariantly, so a typed Channel<"live"> stays assignable to a plain Channel — you can keep a Channel[] of mixed topics for cleanup, or pass a typed channel to a helper that only ever calls close(). Strict property syntax would reject those assignments; the loosened direction (publishing through the widened reference) is the same escape hatch borgo.Push already provides, so nothing new leaks. Wrong payloads through the typed reference still fail tsc, and CI proves it with deliberate wrong-payload files.
Go stays stdlib-only: the WebSocket server is borgo's own, an RFC 6455 implementation on net/http in the process that answers the port. Choose SSE for one-way server→browser feeds; choose WebSocket topics for anything browsers also write to. The /live page in examples/tasks demos both directions: two-tab chat plus Go pushes, with api/realtime.go deciding who may hold the topic.
The relay itself stays dumb by design: it forwards {event, data} between subscribers and Push. Per-message business logic belongs in Go routes, and in CanPublish when the logic is a yes or a no.
Honest limits#
- One process.
Push, the hubs and the relay reach the sockets of the process that holds them. Behind several instances, a subscriber connected to another one never sees the event, unless the instances share a bus: since 1.1borgo.FanOuthands everyPushto a function of yours in place of the local sockets, andborgo.Deliveris what each instance calls with what the bus brought. borgo does not choose the bus. More than one instance has the recipe, built on PostgresLISTEN/NOTIFY.Subscribers,Disconnect, the__countevent and anSSEHubstay per process either way. - Open connections are counted. A request is bounded by its timeouts; a socket and an event stream are held for as long as the other side likes, each with goroutines and a send queue of its own. The process holds 10,000 of each at once —
BORGO_MAX_SOCKETS,BORGO_MAX_STREAMS,0for no limit — and answers the next one503withRetry-After, whichsubscribeandEventSourceboth come back from by themselves. For a socket that answer comes beforeCanSubscribeis asked, so a flood of handshakes at the limit costs your store nothing.borgo.SSEreturnsErrTooManyStreamsbeside it. - The relay is not a message broker. There is no durability, no replay, no delivery guarantee and no ordering guarantee across topics. A subscriber that was offline missed what happened. If a client must not miss an event, give it a way to re-fetch state on reconnect — which is what the SSE example above does by refetching rather than applying a delta.
- Authorization is per topic and yours.
CanSubscribeis the whole rule: a topic it admits is public to everyone it admits, and a name is not a secret — scope by the session, not by an unguessable id. The check is made once, at the handshake, so a revocation reaches a socket at its next handshake and not before;borgo.Disconnectis how the handler that revoked something makes that handshake happen now. - Limits are enforced: 32 topics per client, 128 characters per topic name, 1 MB per message, and a same-origin check on the upgrade — scheme and host, and an absent
Originis a refusal, not a pass. That changed in 0.21: skipping the check for a request with noOriginmeant the one header an attacker can simply omit turned the check off. Browsers always send one, so this costs them nothing; a non-browser client — including bun's ownWebSocket— is now refused, andBORGO_WS_ALLOW_NO_ORIGIN=1is how you take that back, along with every other originless caller (Go reads it since 0.30, with the grammar of every other boolean:yesrefuses the boot). Behind a proxy that terminates TLS,X-Forwarded-Protosupplies the scheme from a peer inBORGO_TRUSTED_PROXIES; the nginx configborgo deploy initwrites already sets it. See security. - A topic containing a comma is refused, on subscribe and on publish alike. The comma separates topics in the relay protocol, so one embedded in a name used to connect, report its subscriber counts, and deliver nothing — the worst shape a defect can take, because everything looks like it is working.
subscribe()now refuses it before opening a socket, throwing with the topic in the message, so the mistake is a stack trace at the call site rather than a connection that closes. It also refuses an empty topic, one padded with whitespace — which fail the same silent way: the relay trims the name, the client filters on the untrimmed one, and nothing is ever delivered — and one over 128 characters, which the relay would answer400to and the client would otherwise redial forever. The server refuses the comma too, and logs it, for anything that reaches/wswithout going throughsubscribe(); aborgo.Pushto such a topic returns an error naming it. - Reconnection is exponential and capped at 30 seconds. After a long outage a client can be up to half a minute behind before it even tries; a page that must feel live should refetch on reconnect rather than trusting the gap was empty:
subscribe(topic, onEvent, { onOpen })is told each time the socket joins, andonOpen({ reconnected: true })is the moment to ask for what was missed. - SSE holds a connection per subscriber. That is cheap in Go, but it is not free at the proxy in front of you: make sure it does not buffer, and does not cut idle connections shorter than your ping interval.
borgo deploy init nginxwrites a config that gets both right. - borgo's own front is not one of those proxies. An
/apievent stream is written by the process that answers the public port, so nothing between your handler and the socket buffers it or holds a slot for it.
Streams and server timeouts#
borgo.Serve runs a hardened http.Server: ReadHeaderTimeout (5s) cuts off slow-header clients, IdleTimeout (2m) reclaims kept-alive connections. The public port's read and write timeouts (a minute each) are deadlines on progress, not on the whole request: every byte read or written moves them, so a slow upload that keeps arriving and a document that keeps streaming are never cut, and a client that stops is. An api served alone leaves both at 0. Request-body abuse is bounded by Bind's 1 MB cap and BORGO_MAX_BODY (see the typed bridge). A WebSocket is hijacked from the server at its handshake and keeps deadlines of its own: a ping every 30 seconds, and a socket that says nothing for two minutes is closed. All four knobs have env overrides (BORGO_READ_HEADER_TIMEOUT and friends — see the environment reference), and if you do set a write timeout, borgo.SSE clears the deadlines on its own connection, so event streams outlive it by design.
Nothing sits in front of Go on this path any more: an event stream is written by the process that answered the public port, and the deadline that could cut it (BORGO_WRITE_TIMEOUT, a minute on the public port) is cleared by an event stream at its first byte, and borgo.SSE then bounds each of its own writes at 10 seconds. The read deadlines bound what a client sends and cannot reach a stream — see security.
A starved render service no longer cuts a stream#
Through 0.2x, heavy CPU load on the Bun front server could cut a live stream, and the client could not tell. It was measured and written down rather than chased, because it was not fixable in that design: the stream and the renders that starved it shared one event loop.
Measured on 0.30. A page whose loader reads 600,000 rows from a Go route, rendered in waves of 24 and then 48 concurrent requests for 44 s (the harness and every run); beside them, an SSEHub stream from Go held silent but for its pings, and a page streamed by the render service whose <Suspense> boundary resolves after 45 s. Once the last wave has ended - at 45 s or a few seconds past it - a Go route publishes one event. In seven runs at the default timeouts, neither stream was ever cut:
- The Go stream delivered its event every time. The render service's load never reaches it: it is written by another process. (The runs below time it from 45 s rather than from the publish, so their "+6.2 s" includes the last wave still running; an instrumented rerun saw the event 8 ms after it was published.)
- The page stream arrived whole every time - the same 1519 bytes an idle server sends, ending in
</html>- its last chunk at most 2.6 s late in these runs. The renders around it took 1.8 to 8.3 s each at 24 concurrent and 14.7 to 17.0 s at 48; the render service is saturated, and its stream waits for a turn rather than being cut.
No timer of the Go front measures a quiet stream: BORGO_WRITE_TIMEOUT is re-armed before every write, so it cuts a client that stops reading, never a page that is slow to say its next word. Before 0.30, for the record: twenty-four concurrent renders of the same payload against a silent stream truncated it once in two runs, after an event-loop stall of some twenty seconds, with the client holding 200 OK and no terminating chunk - and a longer stall did not, which is what made it impossible to bound.
Why no version of that design closed it. The keep-warm is a JavaScript timer, so an event loop with no turn to give cannot run it. The only alternative — arm a long deadline and lower it when the response ends — is not available, because a server.timeout applied after the exchange is over does not take effect at all. Closing this would need a bound bun enforces from outside the loop, which bun does not expose.
How it presented, before 0.30. A truncated 200 that EventSource silently reconnects from, indistinguishable from a complete response unless you are counting bytes. If you are diagnosing gaps in a feed under load on a version before 0.30, this is a candidate; the practical mitigation is the ordinary one — keep long CPU-bound renders off the process serving your streams. A heartbeat is worth having regardless, since any byte written re-arms the deadline — but it is written by the same event loop, so it does not help while that loop is the thing being starved.