Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Callbacks - what's the underlying trigger?

Tags:

I understand that in Firebase I can register my page for callbacks with the "on" method.

According to their docs:

on( ) is used to listen for data changes at a particular location. This is the primary way to read data from Firebase.

firebaseRef.on('value', function(dataSnapshot) {
  // code to handle new value.
});

My question is:

How does it work ?

How does it know that something has changed on the serverside ?

(better) How does the server can 'callback' the browser ?

One answer might be that it is "polling". But I have seen no reference about this approach in Firebase documentation or properties to configure polling time ...

Does anybody know ?

Many Thanks

like image 805
Zo72 Avatar asked Sep 05 '13 14:09

Zo72


People also ask

What are triggers in Firebase?

Triggers when an event occurs and performs its tasks (see What can I do with Cloud Functions? for examples of use cases). Receives a data object that contains a snapshot of the data stored in the specified document.

What is callback in Firebase?

If a callback function is provided, it will be called when the response from Firebase is received. The callback function has two parameters: error and data. If no error was encountered, error will be null . If any error occurred, an error message will be passed to the callback's error parameter.

Does Firebase use WebSockets?

Firebase uses WebSockets to allow the server to "push" data to the client. Since not all browser versions support WebSockets yet, it also falls back to long polling for those browsers.


1 Answers

Firebase uses WebSockets to allow the server to "push" data to the client. Since not all browser versions support WebSockets yet, it also falls back to long polling for those browsers.

The implementation details of how that works on the server are proprietary and sophisticated--enough to write a book about and beyond the scope of a SO question. Logically, works exactly as advertised: The service is designed so that any time a set(), push(), or update() is called (or the REST equivalents), it notifies any listeners of the change.

Regardless of whether the browser uses WebSockets or not, there is no "polling time" as the client is not repeatedly contacting the server. Long polling means waiting for a data change to occur, rather than polling repeatedly to see if a change has occurred. As you can see by trying out the tutorial or any of the real-time examples, data changes are synced to all clients in a matter of milliseconds--nothing to configure.

like image 118
Kato Avatar answered Oct 12 '22 02:10

Kato