liveBook · Manning

Server-sent events

As explained in Go in Practice, Second Edition

Server-sent events are a web mechanism for continuously delivering text-based event updates from a server to a client over HTTP.

Server-sent events (SSE) provide a lightweight, long-lived HTTP connection for delivering text-based updates from a server to clients. They fit applications that need real-time, one-way notifications without polling or bidirectional communication. section 7.4.2 section 7.5

Use SSE when the server is the central source of truth:

  • Replace periodic polling and successive requests with updates delivered over one persistent connection. section 7.4.2
  • Prefer SSE over WebSockets when clients do not need to send messages back to the server. section 7.4.2
  • Apply the pattern to notifications, collaboration tools, and other system-generated updates. section 7.4.2

File-change notification website

A Go service watches a directory with fsnotify in a goroutine and sends file changes to a browser through an SSE endpoint. The application reads the directory from SSE_DIRECTORY, exposes /sse and /files, and serves on port 8080; updates can include a file name, operation, and remaining byte length.

How Go in Practice, Second Edition covers it

How deep it goes
Explains the communication model and demonstrates a Go file-watching service, without developing bidirectional messaging.
Examples
Demonstrates a Go website that watches filesystem changes and reports file operations and byte lengths in real time.
What it assumes
Assumes basic networking and HTTP/TCP server knowledge while applying goroutines, filesystem watching, and HTTP handlers.
What you can do after
Build a Go SSE service with fsnotify that streams directory changes to browser clients.
How it connects
Connects SSE with HTTP, TCP, WebSockets, polling, file watching, notifications, and concurrency.

Also explained in

Compare how 2 books explain server-sent events →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.