echo
echo copied to clipboard
Reverse proxy for Server-Sent Events
The Proxy middleware currently doesn't support Server-Sent Events. A HTTP request expecting a text/event-stream
response is discarded because of this line in proxy.go
. I guess it may be an expected behavior, however adding support for SSE would be very nice.
To solve this, we could use a httputil.ReverseProxy
with a FlushInterval
of ~100ms. We could add a new proxyHTTP factory like so :
func proxyHTTPWithFlushInterval(t *ProxyTarget, interval time.Duration) http.Handler {
proxy := httputil.NewSingleHostReverseProxy(t.URL)
proxy.FlushInterval = interval
return proxy
}
Then use it in the switch statement
case req.Header.Get(echo.HeaderAccept) == "text/event-stream":
proxyHTTPWithFlushInterval(tgt, 100*time.Millisecond).ServeHTTP(res, req)
This may introduce performance issues but since it's reserved for specific use cases, it shouldn't be a problem.
Btw, Echo is an awesome framework, loving it !
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Any ideas/recipe to implement SSE on Echo?
Probably by adding FlushInterval
to ProxyConfig
struct and assinging it to the proxy we created at https://github.com/labstack/echo/blob/61422dd7de9b0359708ff56b67099b91b5954c31/middleware/proxy.go#L279
also removing this case statement and letting it to fall into default
https://github.com/labstack/echo/blob/61422dd7de9b0359708ff56b67099b91b5954c31/middleware/proxy.go#L256-L260
but this need some solid test cases and/or explanation how to test it.
@aldas @vishr Is this still an issue? And is the recommended resolution still the same? If so, my understanding is that I would need to modify the Echo source to get this working, i.e. there is no callback or hook or function parameter that can be set, without modifying the source, that would fix this issue.
I think this issue has been addressed now. See #2624 . Release 4.12.0 contains this fix https://github.com/labstack/echo/releases/tag/v4.12.0