Server Sent Events on Cloud Run with IAP

98 views Asked by At

I've got a NodeJS API deployed to Cloud Run behind IAP for authentication. I'm trying to send SSEs to the client, but they are all buffering until they hit a max buffer length or the events are done and then they send.

It works locally and the events send immediately. And in the deployed environment, I can see the Cloud Run logs that the API is triggering the writes immediately, but they don't arrive until later in batches.

I assume it has something to do with header stripping or something in the GFE or IAP, but I can't seem to get the events to send immediately. I've added the "X-Accel-Buffering": "no" but that didn't help.

EDIT - Cloud Run HTTP/2 is disabled because it currently loops infinitely when used with IAP. Nothing I can find in the docs say that Cloud Run requires HTTP/2 for SSE.

Here's the deploy line:

gcloud run deploy api-XXXX 
--quiet 
--image us-east4-docker.pkg.dev/hq-xxxxx/XXXX 
--update-env-vars NODE_CONFIG_ENV=develop 
--update-labels managed-by=github-actions,commit-sha=XXX 
--platform managed 
--format json 
--region us-east4 
--project XXXX
--no-cpu-throttling

Current headers returned by API - thought I've tried many.

"Content-Type": "text/event-stream; charset=UTF-8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
1

There are 1 answers

1
jamesvillarrubia On

Default status code for a POST in most frameworks is 201. If you submit a body and expect a SSE response, you should modify the status code to 200. Google Cloud Run will buffer the entire response if the status is not 200.

In sum: ensure that your "success" response code is always 200 when sending text/event-stream / server sent events.