2

I have a Firebase realtime database structure that looks like this:

rooms/
     room_id/
            user_1/
                  name: 'hey',
                  connected: true

connected is basically a Boolean indicating as to whether the user is connected or not and will be set to false using onDisconnect() that Firebase provides.

Now my question is - If I trigger a cloud function every time theconnected property of a user changes , can I run a setTimeout() for 45 seconds . If the connected property is still false, at the end of the setTimeout() (for which I read that particular connected value from the db ) then I delete the node of the user (like the user_1 node above).

Will ths setTimeout() pose a problem if there are many triggers fired simultaneously?

Aryaman Shrey
  • 93
  • 1
  • 9

3 Answers3

0

In short, Cloud Functions have a maximum time they can run.

If your timeout makes it's callback after that time limit expired, the function will already have been terminated.

Consider using a very simple and efficient way for calling scheduled code in Cloud Functions called a cron-job.

Ref

benhatsor
  • 1,863
  • 6
  • 20
0

If you use setTimeout() to delay the execution of a function for 45 seconds, that's probably not enough time to cause a problem. The default timeout for a function is 1 minute, and if you exceed that, the function will be forced to terminate immediately. If you are concerned about this, you should simply increase the timeout.

Bear in mind that you are paying for the entire time that a function executes, so even if you pause the function, you will be billed for that time. If you want a more efficient way of delaying some work for later, consider using Cloud Tasks to schedule that.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

For my understanding your functionality is intend to monitoring the users that connected and is on going connect to the Firebase mealtime database from the cloud function. Is that correct?

For monitoring the Firebase Realtime database, GCP provided the tool to monitor the DB performance and usage.

Or simply you just want to keep the connection a live ?

if the request to the Firebase Realtime DB is RESTful requests like GET and PUT, it only keep the connection per request, but is is High requests, it still cost more.

Normally, we suggest the client to use the native SDKs for your app's platform, instead of the REST API. The SDKs maintain open connections, reducing the SSL encryption costs and database load that can add up with the REST API.

However, If you do use the REST API, consider using an HTTP keep-alive to maintain an open connection or use server-sent events and set keep-alive, which can reduce costs from SSL handshakes.

Shawn Di Wu
  • 460
  • 2
  • 9
  • No. In my app , I have a functionality called rooms and so when someone disconnects , I want to show all the people in a room that the person has been disconnected, same for reconnection.But what I want here is that if the person hasnt reconnected for 45 seconds, then remove the person from the room – Aryaman Shrey Oct 12 '20 at 13:08
  • I believe firebase. database. OnDisconnect()method should be the first step to record your client disconnects from the DB server. then the messing() method should be implemented for notification. – Shawn Di Wu Oct 13 '20 at 19:54