InitiateStream initiates the SSE stream by sending headers and starting the ping ticker. This is safe to call multiple times as only the first call has any effect.
(w http.ResponseWriter)
| 73 | // ping ticker. This is safe to call multiple times as only the first call has |
| 74 | // any effect. |
| 75 | func (s *EventStream) InitiateStream(w http.ResponseWriter) { |
| 76 | s.initiateOnce.Do(func() { |
| 77 | s.initiated.Store(true) |
| 78 | s.logger.Debug(s.ctx, "stream initiated") |
| 79 | |
| 80 | // Send headers for Server-Sent Event stream. |
| 81 | w.Header().Set("Content-Type", "text/event-stream") |
| 82 | w.Header().Set("Cache-Control", "no-cache") |
| 83 | w.Header().Set("Connection", "keep-alive") |
| 84 | w.Header().Set("X-Accel-Buffering", "no") |
| 85 | |
| 86 | // Send initial flush to ensure connection is established. |
| 87 | if err := flush(w); err != nil { |
| 88 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | // Start ping ticker. |
| 93 | s.tick.Reset(pingInterval) |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | // Start handles sending Server-Sent Event to the client. |
| 98 | func (s *EventStream) Start(w http.ResponseWriter, r *http.Request) { |