https://github.com/mvrilo/eventsource
Server-sent events for Go
https://github.com/mvrilo/eventsource
Last synced: 9 months ago
JSON representation
Server-sent events for Go
- Host: GitHub
- URL: https://github.com/mvrilo/eventsource
- Owner: mvrilo
- License: mit
- Fork: true (antage/eventsource)
- Created: 2014-07-18T02:44:11.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-07-18T03:26:25.000Z (almost 12 years ago)
- Last Synced: 2025-03-26T19:02:13.811Z (over 1 year ago)
- Language: Go
- Size: 136 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE.txt
Awesome Lists containing this project
README
# eventsource
_eventsource_ provides server-sent events for net/http server.
## Usage
### SSE with default options
``` go
package main
import (
"github.com/antage/eventsource"
"log"
"net/http"
"strconv"
"time"
)
func main() {
es := eventsource.New(nil, nil)
defer es.Close()
http.Handle("/events", es)
go func() {
id := 1
for {
es.SendEventMessage("tick", "tick-event", strconv.Itoa(id))
id++
time.Sleep(2 * time.Second)
}
}()
log.Fatal(http.ListenAndServe(":8080", nil))
}
```
### SSE with custom options
``` go
package main
import (
"github.com/antage/eventsource"
"log"
"net/http"
"strconv"
"time"
)
func main() {
es := eventsource.New(
&eventsource.Settings{
Timeout: 5 * time.Second,
CloseOnTimeout: false,
IdleTimeout: 30 * time.Minute,
}, nil)
es.SendRetryMessage(3 * time.Second)
defer es.Close()
http.Handle("/events", es)
go func() {
id := 1
for {
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
``` go
package main
import (
"github.com/antage/eventsource"
"log"
"net/http"
"strconv"
"time"
)
func main() {
es := eventsource.New(
eventsource.DefaultSettings(),
func(req *http.Request) [][]byte {
return [][]byte{
[]byte("X-Accel-Buffering: no"),
[]byte("Access-Control-Allow-Origin: *"),
}
},
)
defer es.Close()
http.Handle("/events", es)
go func() {
id := 1
for {
es.SendEventMessage("tick", "tick-event", strconv.Itoa(id))
id++
time.Sleep(2 * time.Second)
}
}()
log.Fatal(http.ListenAndServe(":8080", nil))
}
```