Cloudflare and SSE: 520 error

Hi all,

So i have setup Server Side Events which uses a keep-alive connection to the server for a sustained period of time. Works fine locally, no issues. I pushed to production a few weeks ago (prod is behind cloudflare) and after a period of time I get a 520 error, which I understand is specific to Cloudflare.

The client will reconnect only for this to keep happening after several minutes. I outlined my issues on stack overflow where I posted screenshots etc.

I tried to disable CloudFlare on the URL per my screenshot below but this didn’t resolve the issue. I don’t know how I can prevent this happening. Appreciate your help.

Thanks

I think the user on SO meant you need to switch the DNS from orange cloud to grey cloud, effectively bypassing CF servers and exposing your origin. Or create a subdomain eventsource.example.com and have that on grey cloud.
A bypass rule still has the request going via CF, so is still subject to their restrictions.

I am also releasing SSE to prod shortly and are behind CF (Orange cloud, proxied) and do not want to expose my origin - so our plan is to
Create the EventSource(“https://example.com/events”) where example.com is on CF
Have the server validate the request and use a 307 redirect to another site
eg
307 Moved Location: https://anothersite.com/events/some-unique-session-type-key
307 redirects are mentioned in the SSE spec as being permitted

Where anothersite.com is not using CF as a proxy and is locked down very tightly. You will need to enable CORS or similar and install an SSL certificate if needed.

This means that our origin IP is only discoverable by validated users, but we do still have some DOS risk that CF normally removes. However, we are lucky in that we have multiple servers from different providers so can move server easily

The other option is to change to WebSockets which CF allow, but we haven’t tried that alternative ourselves yet.