Skip to content
/ sse Public

Simple, low-level server-sent event (SSE) handler and client.

Notifications You must be signed in to change notification settings

livebud/sse

Repository files navigation

SSE

Go Reference

Simple, low-level server-sent event (SSE) handler and client.

Features

  • Easy to build live-reloading on top
  • Low-level and customizable
  • Comes with an SSE client

Install

go get github.com/livebud/sse

Example

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()
})

Contributors

License

MIT

About

Simple, low-level server-sent event (SSE) handler and client.

Resources

Stars

Watchers

Forks

Packages

No packages published