Server-Sent Events (SSE) handler is a generic compatible and http.Handler compliant module, that implements real-time event streaming from a server to web clients using the Server-Sent Events protocol. It's a robust module for managing multiple client connections, broadcasting events, and handling client registrations and unregistrations efficiently.
Examples of using this module can be found from the ./examples directory.
- Go 1.23.0 or above
go get -u github.com/softwarespot/sse
A basic example of using SSE.
package main
import (
"fmt"
"net/http"
"time"
"github.com/softwarespot/sse"
)
func main() {
// Use the default configuration
h := sse.New[int](nil)
defer h.Close()
go func() {
var evt int
for {
fmt.Println("sse handler: broadcast event", h.Broadcast(evt))
evt++
time.Sleep(64 * time.Millisecond)
}
}()
http.Handle("/events", h)
http.ListenAndServe(":3000", nil)
}
The code has been licensed under the MIT license.