Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a web socket on Cloud Functions for Firebase?

Tags:

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?

like image 973
DAVID TEC Avatar asked Aug 17 '17 17:08

DAVID TEC


People also ask

Does cloud functions support WebSockets?

cloud run doesn't have support for ingress web-socket messages.

Can you use WebSockets with Firebase?

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.

How do I deploy a WebSocket server?

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.

Can I host dynamic website on Firebase?

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.


2 Answers

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.

like image 32
2upmedia Avatar answered Nov 02 '22 06:11

2upmedia


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.

like image 163
Michael Bleigh Avatar answered Nov 02 '22 07:11

Michael Bleigh