twpayne / go-sse

Package sse implements a Server-Sent Events server.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

License:MIT License


Languages

Language:Go 100.0%