https://github.com/jetify-com/sse
Server-Sent Events for Go. A tiny, dependency-free, spec-compliant library compatible with the HTTP stdlib.
https://github.com/jetify-com/sse
event-driven go golang real-time realtime server-sent-events sse sse-client streaming
Last synced: about 1 year ago
JSON representation
Server-Sent Events for Go. A tiny, dependency-free, spec-compliant library compatible with the HTTP stdlib.
- Host: GitHub
- URL: https://github.com/jetify-com/sse
- Owner: jetify-com
- License: apache-2.0
- Created: 2025-05-21T15:59:01.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-27T15:45:51.000Z (about 1 year ago)
- Last Synced: 2025-06-08T16:47:29.750Z (about 1 year ago)
- Topics: event-driven, go, golang, real-time, realtime, server-sent-events, sse, sse-client, streaming
- Language: Go
- Homepage: https://www.jetify.com
- Size: 37.1 KB
- Stars: 89
- Watchers: 9
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# sse – Server‑Sent Events for Go
### A tiny, dependency‑free library that makes it easy to use SSE streaming with any HTTP framework.
[](https://github.com/jetify-com/sse/releases)
[](https://pkg.go.dev/go.jetify.com/sse)
[]()
[](https://discord.gg/jetify)
*Primary Author(s)*: [Daniel Loreto](https://github.com/loreto)
---
## Introduction
Jetify's `sse` package is a tiny, dependency‑free library that makes it easy to use SSE streaming with any HTTP framework.
It is maintained and developed by [Jetify](https://www.jetify.com) and used by our AI agents to handle
SSE in production settings.
## ✨ Features
* **Spec‑compliant**: Follows the [official WHATWG spec](https://html.spec.whatwg.org/multipage/server-sent-events.html) for server-side events.
* **Zero dependencies**: Depends only on the go standard library.
* **Well-tested** Comprehensive test suite with >90% coverage, including end-to-end tests.
* **Framework Agnostic**: Designed to be used with any Go HTTP framework.
* **Flexible Encoding**: Supports both JSON and raw data encoding.
## Quick Start
### Server (Sending Events)
```go
package main
import (
"fmt"
"log"
"net/http"
"time"
"go.jetify.com/sse"
)
func eventsHandler(w http.ResponseWriter, r *http.Request) {
// Upgrade HTTP connection to SSE
conn, err := sse.Upgrade(r.Context(), w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer conn.Close()
// Send events until client disconnects
count := 0
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// Send a simple event with JSON data
event := &sse.Event{
ID: fmt.Sprintf("%d", count),
Data: map[string]any{"count": count, "time": time.Now().Format(time.RFC3339)},
}
if err := conn.SendEvent(r.Context(), event); err != nil {
log.Printf("Error: %v", err)
return
}
count++
case <-r.Context().Done():
return // Client disconnected
}
}
}
func main() {
http.HandleFunc("/events", eventsHandler)
log.Println("SSE server starting on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
```
### Client (Receiving Events)
```go
package main
import (
"context"
"fmt"
"log"
"net/http"
"go.jetify.com/sse"
)
func main() {
// Make request to SSE endpoint
resp, err := http.Get("http://localhost:8080/events")
if err != nil {
log.Fatalf("Failed to connect: %v", err)
}
defer resp.Body.Close()
// Create decoder for the SSE stream
decoder := sse.NewDecoder(resp.Body)
// Process events as they arrive
for {
var event sse.Event
err := decoder.Decode(&event)
if err != nil {
log.Printf("Stream ended: %v", err)
break
}
fmt.Printf("Event ID: %s, Data: %v\n", event.ID, event.Data)
}
}
```
## Documentation
The following dcumentation is available:
* **[API Reference](https://pkg.go.dev/go.jetify.com/sse)** - Complete Go package documentation
* **[Examples](examples/)** - Real-world usage patterns
## Community & Support
Join our community and get help:
* **Discord** – [https://discord.gg/jetify](https://discord.gg/jetify) (best for quick questions & showcase)
* **GitHub Discussions** – [Discussions](https://github.com/jetify-com/sse/discussions) (best for ideas & design questions)
* **Issues** – [Bug reports & feature requests](https://github.com/jetify-com/sse/issues)
## Contributing
We 💖 contributions! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
## License
Licensed under the **Apache 2.0 License** – see [LICENSE](LICENSE) for details.