From e250c41808d650f0ed49b73b2e270156cc1fb590 Mon Sep 17 00:00:00 2001 From: Beorn Facchini Date: Tue, 12 Feb 2019 14:22:33 +1100 Subject: [PATCH] Add event interface to send ids in event responses --- render.go | 5 +++++ responder.go | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) 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() }