sse

package
v0.1.37 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package sse holds the server-sent-events wire format shared by every stream this server serves — session events and the devtools console alike.

It is deliberately only the wire: framing, the resume header, and the flush-and-keepalive contract. What to stream, and when a stream is over, is the caller's business.

Index

Constants

View Source
const DefaultKeepalive = 20 * time.Second

DefaultKeepalive is how often an otherwise silent stream reassures the proxies between here and the browser that the connection is alive. A trace that ran for seconds never needed one; a tail can sit idle for minutes, and the common idle timeout in front of it is a minute.

View Source
const KeepaliveFrame = ": ping\n\n"

KeepaliveFrame is a comment frame: the one thing that can be written into an event stream without being an event.

Write it from the same goroutine — ideally the same select — that writes event frames. A ping emitted from a timer goroutine can land between a frame's id and its data, which splits the frame and desynchronises the client for the life of the connection.

Variables

This section is empty.

Functions

func Begin

func Begin(w http.ResponseWriter) (http.Flusher, error)

Begin writes the event-stream headers and returns the flusher the stream must call after every frame.

It lifts the server's write deadline for this response. A stream is open for as long as it has something to follow, and a server WriteTimeout sized for ordinary requests would otherwise cut every stream at that age. A writer that cannot set deadlines has none to lift; any other failure is returned, and the caller should end the response.

It panics rather than degrading when the writer cannot flush: an unflushed event stream looks like a hung request, which is far harder to diagnose than a stack trace naming the middleware that swallowed the Flusher.

func LastEventSequence

func LastEventSequence(r *http.Request) (int64, error)

LastEventSequence reads the resume header a browser sends after a dropped connection. Anything that is not a sequence is a client the server cannot answer correctly — replaying the whole ring instead would silently duplicate every event it already has.

func WriteFrame

func WriteFrame(w io.Writer, frame Frame) error

WriteFrame writes one frame; an error means the client disconnected and the stream should stop.

func WriteNDJSON

func WriteNDJSON[T any](w http.ResponseWriter, filename string, items []T)

WriteNDJSON serves the same events as a downloadable file, for a caller that wants the stream's contents without following it.

Types

type Frame

type Frame struct {
	Event string
	ID    int64
	Data  any
}

Frame is one wire frame. ID carries the event's sequence, which is what a reconnecting browser echoes back in Last-Event-ID — a frame without it is a frame the client cannot resume from, so only a terminal frame (which nobody resumes into) leaves it unset.

Jump to

Keyboard shortcuts

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