Skip to content

Instantly share code, notes, and snippets.

@versionsix
Created June 23, 2022 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save versionsix/e8bf3ebcf849a13feb65f6e690280527 to your computer and use it in GitHub Desktop.
Save versionsix/e8bf3ebcf849a13feb65f6e690280527 to your computer and use it in GitHub Desktop.
go serverside events curl
package main
import (
"log"
"net/http"
"strconv"
"time"
"gopkg.in/antage/eventsource.v1"
)
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(":8089", nil))
}
// ❯ curl -N --http2 -H "Accept:text/event-stream" localhost:8089/events
/*
id: 4
event: tick-event
data: tick
id: 5
event: tick-event
data: tick
id: 6
event: tick-event
data: tick
*/
/*
❯ curl -v -N --http2 -H "Accept:text/event-stream" localhost:8089/events
* Trying 127.0.0.1:8089...
* Connected to localhost (127.0.0.1) port 8089 (#0)
> GET /events HTTP/1.1
> Host: localhost:8089
> User-Agent: curl/7.79.1
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAAQCAAAAAAIAAAAA
> Accept:text/event-stream
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/event-stream
< Vary: Accept-Encoding
* no chunk, no close, no size. Assume close to signal end
<
id: 3
event: tick-event
data: tick
id: 4
event: tick-event
data: tick
id: 5
event: tick-event
data: tick
^C
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment