Server-Sent Events for Go
Inspired from alexandrevincenzi's work
Server-sent events is a method of continuously sending data from a server to the browser, rather than repeatedly requesting it, replacing the "long polling way".
go-libsse
is a small, thread-safe library to create a Server-Sent Events server in Go.
- Fully thread-safe
- Custom initialization after connection from browser (for example to patch the gap in last-event-id)
- Custom headers (useful for CORS)
Last-Event-ID
support (resend lost messages)- Follow SSE specification
package main
import (
"log"
"net/http"
"strconv"
"time"
"github.com/AdminXVII/go-libsse"
)
func main() {
// Create the server.
s := sse.NewServer(nil)
// Register with /events endpoint.
http.Handle("/events/", s)
// Dispatch messages to channel-1.
go func () {
for {
s.SendMessage(sse.Message{Data: time.Now().String()})
time.Sleep(5 * time.Second)
}
}()
http.ListenAndServe(":3000", nil)
}
Connecting to our server from JavaScript:
e1 = new EventSource('/events/');
e1.onmessage = function(event) {
// do something...
};