Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/softwarespot/sse
Server-Sent Events (SSE) handler is a generic compatible and http.Handler compliant module
https://github.com/softwarespot/sse
go handler server-sent-events
Last synced: about 2 months ago
JSON representation
Server-Sent Events (SSE) handler is a generic compatible and http.Handler compliant module
- Host: GitHub
- URL: https://github.com/softwarespot/sse
- Owner: softwarespot
- License: mit
- Created: 2024-10-20T11:17:53.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-12-10T15:24:39.000Z (about 2 months ago)
- Last Synced: 2024-12-10T16:36:43.319Z (about 2 months ago)
- Topics: go, handler, server-sent-events
- Language: Go
- Homepage: https://pkg.go.dev/github.com/softwarespot/sse
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Server-Sent Events (SSE) handler
[![Go Reference](https://pkg.go.dev/badge/github.com/softwarespot/sse.svg)](https://pkg.go.dev/github.com/softwarespot/sse) ![Go Tests](https://github.com/softwarespot/replay/actions/workflows/go.yml/badge.svg)
**Server-Sent Events (SSE) handler** is a generic compatible and [http.Handler](https://pkg.go.dev/net/http#Handler) compliant module, that implements real-time event streaming from a server to web clients using the [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_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](./examples/) directory.
## Prerequisites
- Go 1.23.0 or above
## Installation
```bash
go get -u github.com/softwarespot/sse
```## Usage
A basic example of using **SSE**.
```Go
package mainimport (
"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)
}
```## License
The code has been licensed under the [MIT](https://opensource.org/license/mit) license.