Go handles real-time data streaming with goroutines and channels for in-process pipelines, net and net/http for network transport, and time and context for scheduling and cancellation. The standard library has no WebSocket server, so browsers are usually served with Server-Sent Events over plain net/http, or with a third-party WebSocket package.
A good streaming design has bounded buffers, explicit backpressure, cancellation on every goroutine, and clean shutdown. The sections below build those pieces step by step.
time.TickerA pipeline is a chain of stages connected by channels. Each stage reads from an input channel, does work, and writes to an output channel. A channel with a small buffer absorbs short bursts, and a full buffer makes the producer wait, which is backpressure.
The producer stops when the context expires and closes the channel, so the range loop ends. Only the sender closes a channel, and every blocking send should sit in a select with ctx.Done() to avoid leaked goroutines.
time.TickerUse time.NewTicker for fixed-interval work such as polling or flushing a batch. Since Go 1.23, unreferenced tickers are garbage collected, but stopping them with defer ticker.Stop() is still good practice.
If processing takes longer than the interval, ticks are dropped rather than queued, so a slow consumer never builds an unbounded backlog.
The net package gives you raw TCP and UDP. A TCP stream suits custom binary protocols, log shipping, or feeds between your own services. Handle each connection in its own goroutine and set deadlines so that a dead client cannot hold a goroutine forever.
Test it with nc localhost 9000 and you see a line per second.
For one-way server-to-browser streams such as dashboards, notifications, or live logs, Server-Sent Events (SSE) work with the standard library alone. The response uses the text/event-stream content type and you flush after every event.
In the browser, new EventSource("/events") receives the messages and reconnects automatically. Note that http.Server.WriteTimeout applies to the whole response, so long-lived streams need it disabled or extended per request with http.ResponseController.
Use WebSockets when the browser must also send frequent messages. The standard library has no WebSocket implementation (golang.org/x/net/websocket is not recommended for new code). Popular choices are github.com/coder/websocket and github.com/gorilla/websocket. Add authentication and an origin check before upgrading the connection.
| Concern | Technique |
|---|---|
| Backpressure | Bounded channels; block, drop oldest, or reject when full |
| Slow consumers | Per-client buffer plus a drop or disconnect policy |
| Shutdown | http.Server.Shutdown and context cancellation on every goroutine |
| Transient errors | Retry with exponential backoff and jitter |
| Observability | log/slog fields plus counters for items in, out, and dropped |
| Security | TLS, authentication, and per-client rate limits |
| Scaling | Partition streams by key across instances; use a broker such as Kafka or NATS for fan-out |
For high-throughput work, reuse buffers with sync.Pool, batch small messages, and measure with go test -bench and pprof before optimising.
For most services, yes. Channel operations cost tens to hundreds of nanoseconds, which is far below network latency. Measure before replacing them with lock-free structures.
Choose SSE for one-way updates because it uses plain HTTP, proxies well, and reconnects by itself. Choose WebSockets when clients send a steady flow of messages back.
Give every goroutine a context.Context and select on ctx.Done() around each blocking send or receive. Close channels from the sender side only.
No. Go has a garbage collector and no hard latency guarantees, so it fits soft real-time streaming, but not hard real-time control loops.
Build streaming systems in Go from bounded channels for backpressure, context for cancellation, time.Ticker for periodic work, and net or net/http for transport. Use SSE for browser feeds, add a WebSocket package when you need two-way messaging, and always set deadlines, shut down cleanly, and measure before optimising.