Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good idea to run Socket.io on a Firebase Cloud Function?

Implementing it works, but I have read that due to how Cloud Functions are designed they are not the best way to use socket.io. Why?

like image 206
Toma Avatar asked Jul 05 '19 12:07

Toma


People also ask

When should I use Firebase cloud function?

You should use Cloud Functions for Firebase if you're a developer building a mobile app or mobile web app. Firebase gives mobile developers access to a complete range of fully managed mobile-centric services including analytics, authentication and Realtime Database.

Should I use Socket.IO or Firebase?

"Realtime backend made easy", "Fast and responsive" and "Easy setup" are the key factors why developers consider Firebase; whereas "Real-time", "Event-based communication" and "Node. js" are the primary reasons why Socket.IO is favored.

Does Firebase support WebSockets?

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. When your client saves a change to the data, all clients who are connected, receive the updated data almost instantly.

Is Socket.IO any good?

Conclusion. I think Socket.io is a very useful piece of technology and is incredibly relevant today in spite of the popular view that widespread support for WebSockets makes it redundant. I would recommend that it be used for highly interactive applications. Its namespacing in particular is its strongest point.


2 Answers

Actually, socket.io does not work with Cloud Functions. Cloud Functions have the following properties that make them incompatible with long-lived socket connections:

  1. The maximum duration of a Cloud Function can only be 9 minutes. The socket will be forced closed after that time. This is counter to the normal expectation of socket.io to keep a socket connection alive indefinitely.
  2. Cloud Functions will read the entire contents of the request, and only then will write the entire contents of the response. There is only one full round trip - a client can not "chat back and forth" over the connection with the function.

See also

  • Google Cloud Functions with socket.io
  • Run a web socket on Cloud Functions for Firebase?
  • Is it possible to host a express and socket.io app on Firebase Hosting?
like image 113
Doug Stevenson Avatar answered Nov 07 '22 13:11

Doug Stevenson


Cloud Functions are made for simple requests, they are not made for long-running processes. If you want to stick with serverless architecture, try Cloud Run. They released an update this year (January 2021) and the platform is now able to support WebSockets including Socket.io.

Here is the link to the Cloud Run documentation

Here is the link to the blog post (their announcement)

like image 39
Michal Moravik Avatar answered Nov 07 '22 15:11

Michal Moravik