Sse
Server-Sent Events for Go. A tiny, dependency-free, spec-compliant library compatible with the HTTP stdlib.
Install / Use
/learn @jetify-com/SseREADME
sse – Server‑Sent Events for Go
A tiny, dependency‑free library that makes it easy to use SSE streaming with any HTTP framework.
Primary Author(s): Daniel 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 and used by our AI agents to handle SSE in production settings.
✨ Features
- Spec‑compliant: Follows the official WHATWG spec 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)
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)
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 - Complete Go package documentation
- Examples - Real-world usage patterns
Community & Support
Join our community and get help:
- Discord – https://discord.gg/jetify (best for quick questions & showcase)
- GitHub Discussions – Discussions (best for ideas & design questions)
- Issues – Bug reports & feature requests
Contributing
We 💖 contributions! Please read CONTRIBUTING.md for guidelines.
License
Licensed under the Apache 2.0 License – see LICENSE for details.
Related Skills
xurl
330.3kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
kubeshark
11.8kCluster-wide network observability for Kubernetes. Captures L4 packets, L7 API calls, and decrypted TLS traffic using eBPF, with full Kubernetes context. Available to AI agents via MCP and human operators via dashboard.
wanwu
4.2kChina Unicom's Yuanjing Wanwu Agent Platform is an enterprise-grade, multi-tenant AI agent development platform. It helps users build applications such as intelligent agents, workflows, and rag, and also supports model management. The platform features a developer-friendly license, and we welcome all developers to build upon the platform.
gin-vue-admin
24.5k🚀Vite+Vue3+Gin拥有AI辅助的基础开发平台,企业级业务AI+开发解决方案,内置mcp辅助服务,内置skills管理,支持TS和JS混用。它集成了JWT鉴权、权限管理、动态路由、显隐可控组件、分页封装、多点登录拦截、资源权限、上传下载、代码生成器、表单生成器和可配置的导入导出等开发必备功能。
