Skip to content

Repository files navigation

eventsource

Build Status

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"
)
funcmain() {
es:=eventsource.New(nil, nil)
deferes.Close()
http.Handle("/events", es)
gofunc() {
id:=1for {
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"
)
funcmain() {
es:=eventsource.New(
&eventsource.Settings{
Timeout: 5*time.Second,
CloseOnTimeout: false,
IdleTimeout: 30*time.Minute,
}, nil)
es.SendRetryMessage(3*time.Second)
deferes.Close()
http.Handle("/events", es)
gofunc() {
id:=1for {
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"
)
funcmain() {
es:=eventsource.New(
eventsource.DefaultSettings(),
func(req*http.Request) [][]byte {
return [][]byte{
[]byte("X-Accel-Buffering: no"),
[]byte("Access-Control-Allow-Origin: *"),
}
},
)
deferes.Close()
http.Handle("/events", es)
gofunc() {
id:=1for {
es.SendEventMessage("tick", "tick-event", strconv.Itoa(id))
id++time.Sleep(2*time.Second)
}
}()
log.Fatal(http.ListenAndServe(":8080", nil))
}

About

Server-sent events for Go

Resources

Stars

497 stars

Watchers

12 watching

Forks

Releases

Packages

Used by

Contributors

Languages