Skip to content

Commit 0b19dc8

Browse files
committed
Add event interface to send ids in event responses
1 parent 3215478 commit 0b19dc8

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

render.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ type Binder interface {
1515
Bind(r *http.Request) error
1616
}
1717

18+
// Event interface for event stream responses.
19+
type Event interface {
20+
GetID() string
21+
}
22+
1823
// Bind decodes a request body and executes the Binder method of the
1924
// payload structure.
2025
func Bind(r *http.Request, v Binder) error {

responder.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ func channelEventStream(w http.ResponseWriter, r *http.Request, v interface{}) {
190190
}
191191
continue
192192
}
193-
w.Write([]byte(fmt.Sprintf("event: data\ndata: %s\n\n", bytes)))
193+
var resp string
194+
if ev, ok := v.(Event); ok {
195+
resp += fmt.Sprintf("id: %s\n", ev.GetID())
196+
}
197+
resp += fmt.Sprintf("event: data\ndata: %s\n\n", bytes)
198+
w.Write([]byte(resp))
194199
if f, ok := w.(http.Flusher); ok {
195200
f.Flush()
196201
}

0 commit comments

Comments
 (0)