https://github.com/livebud/sse
Simple, low-level server-sent event (SSE) handler and client.
https://github.com/livebud/sse
Last synced: 3 months ago
JSON representation
Simple, low-level server-sent event (SSE) handler and client.
- Host: GitHub
- URL: https://github.com/livebud/sse
- Owner: livebud
- Created: 2023-10-04T07:56:04.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-05T19:39:52.000Z (about 1 year ago)
- Last Synced: 2025-06-19T19:53:53.393Z (12 months ago)
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: Changelog.md
Awesome Lists containing this project
README
# SSE
[](https://pkg.go.dev/github.com/livebud/sse)
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
```sh
go get github.com/livebud/sse
```
## Example
On the server-side:
```go
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:
```js
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
- Matt Mueller ([@mattmueller](https://twitter.com/mattmueller))
## License
MIT