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
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With