Eventsource
Server-sent events for Go
Install / Use
/learn @antage/EventsourceREADME
eventsource
eventsource provides server-sent events for net/http server.
Usage
SSE with default options
package main
import (
"gopkg.in/antage/eventsource.v1"
"log"
"net/http"
"strconv"
"time"
)
func main() {
es := eventsource.New(nil, nil)
defer es.Close()
http.Handle("/events", es)
go func() {
id := 1
for {
es.SendEventMessage("tick", "tick-event", strconv.Itoa(id))
id++
time.Sleep(2 * time.Second)
}
}()
log.Fatal(http.ListenAndServe(":8080", nil))
}
SSE with custom options
package main
import (
"gopkg.in/antage/eventsource.v1"
"log"
"net/http"
"strconv"
"time"
)
func main() {
es := eventsource.New(
&eventsource.Settings{
Timeout: 5 * time.Second,
CloseOnTimeout: false,
IdleTimeout: 30 * time.Minute,
}, nil)
es.SendRetryMessage(3 * time.Second)
defer es.Close()
http.Handle("/events", es)
go func() {
id := 1
for {
es.SendEventMessage("tick", "tick-event", strconv.Itoa(id))
id++
time.Sleep(2 * time.Second)
}
}()
log.Fatal(http.ListenAndServe(":8080", nil))
}
SSE with custom HTTP headers
package main
import (
"gopkg.in/antage/eventsource.v1"
"log"
"net/http"
"strconv"
"time"
)
func main() {
es := eventsource.New(
eventsource.DefaultSettings(),
func(req *http.Request) [][]byte {
return [][]byte{
[]byte("X-Accel-Buffering: no"),
[]byte("Access-Control-Allow-Origin: *"),
}
},
)
defer es.Close()
http.Handle("/events", es)
go func() {
id := 1
for {
es.SendEventMessage("tick", "tick-event", strconv.Itoa(id))
id++
time.Sleep(2 * time.Second)
}
}()
log.Fatal(http.ListenAndServe(":8080", nil))
}
Related Skills
node-connect
333.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
82.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
333.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
82.0kCommit, push, and open a PR
