Skip to content
/ go-sse Public

Package sse implements a Server-Sent Events server.

License

Notifications You must be signed in to change notification settings

twpayne/go-sse

Repository files navigation

go-sse

PkgGoDev

Package sse implements a Server-Sent Events server.

In short, it makes sending events to a client just like writing to a Go channel.

Example

sseServer := sse.NewServer(
	sse.WithConnectFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool {
		w.Header().Set("Access-Control-Allow-Origin", "*")
		return true
	}),
	sse.WithEstablishedFunc(func(ctx context.Context, ch chan<- sse.Event, r *http.Request) {
		defer close(ch)
		for i := 0; i < 4; i++ {
			time.Sleep(time.Second)
			ch <- sse.Event{
				Name: fmt.Sprintf("event-%d", i),
				Data: []byte(strconv.Itoa(i)),
			}
		}
	}),
)

serverMux := http.NewServeMux()
serverMux.Handle("GET /events", sseServer)

http.ListenAndServe(":8080", serverMux)

License

MIT

About

Package sse implements a Server-Sent Events server.

Topics

Resources

License

Stars

Watchers

Forks

Languages