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: 15 days 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 (9 months ago)
- Default Branch: main
- Last Pushed: 2025-02-12T17:51:13.000Z (5 months ago)
- Last Synced: 2025-03-30T14:29:53.045Z (4 months ago)
- Topics: go, handler, server-sent-events
- Language: Go
- Homepage: https://pkg.go.dev/github.com/softwarespot/sse
- Size: 20.5 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
[](https://pkg.go.dev/github.com/softwarespot/sse) 
**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.24.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.