Hello I actually have a REST api running on Cloud Functions for Firebase using http request, but now I need to sync the data on real time requesting to the functions. I read something about web sockets.
Is there a way to run a web socket on Cloud Functions for Firebase?
cloud run doesn't have support for ingress web-socket messages.
You won't have to make individual WebSocket calls since one socket connection is all you need. All of your data syncs automatically through the single WebSocket as fast as your client's network can carry it and Firebase sends you new data as and when it's updated.
After creating your WebSocket API, you must deploy it to make it available for your users to invoke. To deploy an API, you create an API deployment and associate it with a stage. Each stage is a snapshot of the API and is made available for client apps to call.
You can use web frameworks, like Express. js, in Cloud Functions to serve your app's dynamic content and write complex web apps more easily. Firebase Hosting also supports HTTPS requests to Cloud Run containers.
Theoretically you could use two different layers: one to manage the websocket connections and another layer to handle the data processing.
The websocket layer will not be Cloud Functions, but a Docker container running Push Pin in Cloud Run and that’ll route HTTP calls to your Cloud Functions to do the actual data processing.
This is not going to be a good fit for Cloud Functions. Websockets rely on long-lived connections to the same server over time; Cloud Functions are ephemeral compute instances that are spun down when there's no traffic. There's no way to force or guarantee that a Cloud Function will keep running or hold a connection open indefinitely.
I would encourage you to investigate using the Firebase Realtime Database as a conduit here instead of trying to add realtime to Cloud Functions.
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