Server Sent Events on Cloud Run not working

Hi everyone.

I have a problem with the implementation of SSE (Server Sent Events) and Cloud Run.

I have a service in node.js that is listening for events on a PubSub subscription. At the same time, I have another web application that is connected to this service by SSE. In the local environment it works smoothly, every time an event arrives to the service in node.js, it emits the message to the web client, but once deployed in Cloud Run it stops working, or, it works slow and not all messages are delivered. Anyone with a similar experience? Maybe I should use Compute Engine to deploy the node.js service instead of Cloud Run.

Best regards

Solved Solved
0 12 2,183
1 ACCEPTED SOLUTION

There isn't solution for this problem in Cloud Run. Finally I had to create a Compute Engine and deploy the service there, now it works.

View solution in original post

12 REPLIES 12

Hi @chaloman ,

Welcome to Google Cloud Community!

Check your logs for any returned errors or potential issues so you could identify the reasons for the mentioned behaviors. There could be several causes for this, example scenarios could be:

I highly suggest for you to contact Google Support as Engineers have better visibility on your logs/project. 

I'm having the same problem. Works locally but does not work when deployed to cloud run.

There isn't solution for this problem in Cloud Run. Finally I had to create a Compute Engine and deploy the service there, now it works.

Hi @chaloman

By default, Cloud Run only allocates CPU and charges when a container instance is processing at least one request. You can opt-into having CPU for the entire lifecycle of container instances, which are still subject to autoscaling. Cloud Run pricing depends on the selected CPU allocation.

Since Server Side Event are server side, pushing changes to the client won't work, unless you have CPU allocated.

Here is the documentation explaining the difference https://cloud.google.com/run/docs/configuring/cpu-allocation

Note that you can also use Kubernetes autopilot instead of Cloud Run if you feel it suits you best. There is a nice tutorial here: https://console.cloud.google.com/welcome/new?tutorial=kubernetes--autopilot

I hope it helps,

Julien

Hi Julien, I tried your solution but it doesn't seem to be working. Is there an example of this working?

hello, I'm facing the same issue I have the code as you said, but it doesn't work as expected. do you have another way to do it? Thank you.

Yes, I finally created a small VM on Compute Engine to host the process, it works fine

SSE wont work in Cloud Run as far as I know so I just used Websockets which do work. 

Hi @chaloman  ,  even i'm facing the same issue now, we are using the Server Sent Events for the server initiated communication to client, but when i tested in local, everything is working fine, but when we deployed to the dev environemnt in the AWS cloud we are facing inconsisent issues, if i open 4 browsers we are getting events to 2 browsers, if i open 6 broswers we are getting events to 3 browsers, like half we are getting not to the other half.

Can you help us here ??? We are blocked on this.

Hi,

SSE is quite limited using multiple tabs at the same time, the connection are limited per client. I recommend you to implement WebSockets, I think SSE is still lacking maturity on cloud technology. I made that change and it worked very well.

Switch to using websockets and it worked on Cloud Run. I essentially setup up a SSE with a websocket by sending updates from the backend to the front and nothing but a heartbeat ever gets returned. Good luck!

Hi, I have exactly the same problem. If I understand correctly, you have built a unidirectional SSE by sending events/updates to a site from your backend deployed on Cloud Run. You have used websockets instead of SSE-associated technologies (emitter...). Is that how you have it currently, and it has worked for you, right?

thanks