Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse proxy for Server-Sent Events #1172

Closed
dselim opened this issue Aug 2, 2018 · 5 comments
Closed

Reverse proxy for Server-Sent Events #1172

dselim opened this issue Aug 2, 2018 · 5 comments

Comments

@dselim
Copy link

dselim commented Aug 2, 2018

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 !

@stale
Copy link

stale bot commented Nov 30, 2018

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 stale bot added the wontfix label Nov 30, 2018
@stale stale bot closed this as completed Dec 7, 2018
@Faridalim
Copy link

Any ideas/recipe to implement SSE on Echo?

@aldas
Copy link
Contributor

aldas commented Aug 8, 2022

Probably by adding FlushInterval to ProxyConfig struct and assinging it to the proxy we created at

proxy := httputil.NewSingleHostReverseProxy(tgt.URL)

also removing this case statement and letting it to fall into default

echo/middleware/proxy.go

Lines 256 to 260 in 61422dd

case c.IsWebSocket():
proxyRaw(tgt, c).ServeHTTP(res, req)
case req.Header.Get(echo.HeaderAccept) == "text/event-stream":
default:
proxyHTTP(tgt, c, config).ServeHTTP(res, req)

but this need some solid test cases and/or explanation how to test it.

@W1M0R
Copy link

W1M0R commented Apr 15, 2024

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

@aldas
Copy link
Contributor

aldas commented Apr 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants