diff --git a/render.go b/render.go index 75a90e2..53232d4 100644 --- a/render.go +++ b/render.go @@ -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 { diff --git a/responder.go b/responder.go index 9365350..a5e1ffa 100644 --- a/responder.go +++ b/responder.go @@ -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() }