Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type Binder interface {
Bind(r *http.Request) error
}

// Event interface for event stream responses.
type Event interface {
GetID() string
}

// Bind decodes a request body and executes the Binder method of the
// payload structure.
func Bind(r *http.Request, v Binder) error {
Expand Down
10 changes: 9 additions & 1 deletion responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,15 @@ func channelEventStream(w http.ResponseWriter, r *http.Request, v interface{}) {
}
continue
}
w.Write([]byte(fmt.Sprintf("event: data\ndata: %s\n\n", bytes)))
var resp string
if ev, ok := v.(Event); ok {
id := ev.GetID()
if id != "" {
resp += fmt.Sprintf("id: %s\n", id)
}
}
resp += fmt.Sprintf("event: data\ndata: %s\n\n", bytes)
w.Write([]byte(resp))
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
Expand Down