https://github.com/euskadi31/go-sse
Server Sent Events for Go
https://github.com/euskadi31/go-sse
go golang golang-library server-sent-events sse
Last synced: 3 months ago
JSON representation
Server Sent Events for Go
- Host: GitHub
- URL: https://github.com/euskadi31/go-sse
- Owner: euskadi31
- License: mit
- Created: 2017-09-15T19:40:18.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-03-17T08:50:38.000Z (3 months ago)
- Last Synced: 2025-03-17T09:42:47.854Z (3 months ago)
- Topics: go, golang, golang-library, server-sent-events, sse
- Language: Go
- Size: 48.8 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
Server Sent Events for Go [](https://github.com/euskadi31/go-sse/releases/latest) [](https://godoc.org/github.com/euskadi31/go-sse)
=========================[](https://goreportcard.com/report/github.com/euskadi31/go-sse)
| Branch | Status | Coverage |
|---------|--------|----------|
| master | [](https://travis-ci.org/euskadi31/go-sse) | [](https://coveralls.io/github/euskadi31/go-sse?branch=master) |Golang Server Sent Events server
## Example
### Server
```go
package mainimport (
"fmt"
"log"
"net/http"
"strconv"
"time""github.com/euskadi31/go-sse"
)func main() {
serve := sse.NewServer(func(rw sse.ResponseWriter, r *http.Request) {
tickChan := time.NewTicker(time.Second * 2).C// recovery
lastID := r.Header.Get(sse.LastEventID)
if lastID != "" {
log.Printf("Recovery with ID: %s\n", lastID)
}for {
select {
case t := <-tickChan:
eventString := fmt.Sprintf("the time is %v", t)log.Println("Send event...")
rw.Send(&sse.MessageEvent{
ID: strconv.Itoa(int(t.Unix())),
Data: []byte(eventString),
})
case <-r.Context().Done():
log.Println("Done")return
}
}
})serve.SetRetry(time.Second * 5)
http.Handle("/events", serve)
log.Panic(http.ListenAndServe(":1337", nil))
}```
### Client
```js
var client = new EventSource("http://localhost:1337/events");client.onmessage = (msg) => {
console.log(msg);
};
```## License
go-sse is licensed under [the MIT license](LICENSE.md).