Skip to content

Commit e250c41

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

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Diff for: render.go

+5
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 {

Diff for: responder.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,15 @@ 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+
id := ev.GetID()
196+
if id != "" {
197+
resp += fmt.Sprintf("id: %s\n", id)
198+
}
199+
}
200+
resp += fmt.Sprintf("event: data\ndata: %s\n\n", bytes)
201+
w.Write([]byte(resp))
194202
if f, ok := w.(http.Flusher); ok {
195203
f.Flush()
196204
}

0 commit comments

Comments
 (0)