Server Sent Events and Cloudflare

I’m developing an API for a service that’s gonna launch soon. Clients will connect and listen for data via Server Sent Events. Everything went well until recently, when initial data got larger (really not that large though, just 37kb uncompressed JSON) only part of that initial data get streamed to the clients. This works with every other hosting (Heroku, Localhost, Digitalocean) – my guess is that Cloudflare is somehow interfering. We are setting the headers “Cache-control: no-cache”, but that’s probably not enough. Is there any way to bypass Cloudflare, at least for debugging reasons (to make sure that really Cloudflare is the problem)?

Hi Severin,

I’m happy to help look into this. Can you share a ray-id of a request that had this issue?

Hi! Here you go:

cf-ray: 6d48b28e2c5023df-ZRH

You can also test it out yourself: https://fcut-api.onrender.com/listen

Seems like something is holding back the data until it reaches a certain amount (we’re sending a ping event every minute, so after a while it will send more data).

I can confirm that this is an issue with Cloudflare. We are working with them to see how we can prevent this going forward. In the meantime, have you considered using websockets? This use case seems like a good fit for them and Cloudflare tends to handle those connections more gracefully.

Thanks for looking into it. For this case it was actually much easier to switch to Heroku (although I hoped to ditch their service). WebSockets would be an alternative, but I prefer SSE for unidirectional realtime updates – lightweight, simple and super performant.

As four Cloudflare: I do like their service (a lot), but I’m not too happy about not being able to opt out or at least configure when edge cases happen. And IMHO edge cases happen more often then you’d think.

Anyway, thanks again for looking into this, I’d be happy to switch back :slight_smile:

1 Like

I definitely understand where you’re coming from. It’s important that all traffic goes through Cloudflare, so we want to work through these edge cases so that Cloudflare remains as transparent as possible from a customer perspective.

I’ll be sure to let you know once we are able to resolve this with them!

Hello

The sample page https://go-gin.onrender.com. also does not work. The messages seem to be buffered

Hi folks, any update on this? We are having the same issue and it is blocking our release.

Hi Frank,

We are working with Cloudflare to identify a workaround. We don’t have an ETA for this yet, but it’s a high priority for us.

Still no ETA for this? Running into all sorts of issues with server-sent events migrating from Heroku

We’ve released a fix for this issue. You should now be able to use server-sent events. Let me know if you continue to run into any problems.

Hi Jake,
Has this issue resurfaced since the fix a year ago? I built service using SSE that works fine locally, but it fails most of the time with my render service. Sometimes I get server-events but usually it just hangs and times out. Sounds very similar to the issues described, though it was unclear if the issues described were flakey or just worked 0% of the time until your fix was deployed?

Thanks,
Tom

Hi all,
I’m also experiencing the same issue. No SSE events are received on the client because they appear to get buffered by Cloudflare. I was able to test this by making my server send the same event many times in a 100ms interval. Eventually, a huge batch of messages is received by the client.

Any updates Jake?

Thanks,
Joe

[update] For me this issue was a missing page cache rule. Found the answer here: https://community.cloudflare.com/t/websockets-and-server-side-events-in-my-application-are-not-working-anymore-if-cloudfare-cache-is-enabled/179584

How did you set up Cloudflare to bypass cache for render Web Service?
I have domain set up on other DNS provider and no access to Cloudflare configuration

Can you please share the steps on how to do this?

Hi Jake, Is the sse fix not avaialable for free tier. SSE connection does not seem to be working for me. It stays up in pending state.