Simple, low-level server-sent event (SSE) handler and client.
- Easy to build live-reloading on top
- Low-level and customizable
- Comes with an SSE client
go get github.com/livebud/sse
On the server-side:
package main
import (
"context"
"log/slog"
"net/http"
"strconv"
"time"
"github.com/livebud/sse"
)
func main() {
ctx := context.Background()
log := slog.Default()
handler := sse.New(log)
go http.ListenAndServe(":8080", handler)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
count := 0
for {
<-ticker.C
handler.Broadcast(ctx, &sse.Event{
Data: []byte(strconv.Itoa(count)),
})
count++
}
}
From the browser:
const es = new EventSource("/")
// listen for messages
es.addEventListener("message", function (e) {
console.log("got message", e.data)
})
// cleanup afterwards (too many zombie clients may cause server to hang)
window.addEventListener("beforeunload", function () {
es && es.close()
})
- Matt Mueller (@mattmueller)
MIT