Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Websockets on Cloud Functions 2nd gen?

I heard that Cloud Functions 2nd gen is built on top of Cloud run which supports Websockets. Does this mean that Cloud Functions 2nd gen can also handle Websockets?

https://cloud.google.com/blog/products/serverless/cloud-run-gets-websockets-http-2-and-grpc-bidirectional-streams

https://cloud.google.com/blog/products/serverless/cloud-functions-2nd-generation-now-generally-available

like image 859
user12208004 Avatar asked Oct 16 '25 14:10

user12208004


2 Answers

I don't think so.

Cloud Functions are a FAAS, fully on-demand serverless. The purpose of a function is to come to life when an event occurs (pub/sub trigger, http request, etc), perform some compute logic and than die.

Web sockets do require a connection alive, so Cloud Functions don't fit well for that.

Go for Cloud Run (low learning curve coming from functions - no docker knowledge required) or investigate the option to use Firebase Datastore to have client/server read/write and stay in synch (even easier then web socket).

like image 113
Fabio Avatar answered Oct 19 '25 09:10

Fabio


Yes it is possible to use websockets with Firebase functions v2. The key is to use onRequest and pull the server from req.socket.server so that you can make the call to server.on("update", ...). The client will also need a retry mechanism for the first call or you can insert a file scope priming function that will cause the server.on("update", ...) listener to be attached before the real client call arrives. See https://stackoverflow.com/a/77153547/581848 for further details.

like image 43
Chris Chiasson Avatar answered Oct 19 '25 08:10

Chris Chiasson