echo icon indicating copy to clipboard operation
echo copied to clipboard

Reverse proxy for Server-Sent Events

Open dselim opened this issue 6 years ago • 3 comments

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 !

dselim avatar Aug 02 '18 09:08 dselim

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.

stale[bot] avatar Nov 30 '18 13:11 stale[bot]

Any ideas/recipe to implement SSE on Echo?

Faridalim avatar Aug 08 '22 20:08 Faridalim

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 avatar Aug 08 '22 21:08 aldas

@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.

W1M0R avatar Apr 15 '24 06:04 W1M0R

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

aldas avatar Apr 15 '24 18:04 aldas