It's likely that Firebase use the page visibility API:
https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
Most browsers stop running JS on tabs some time after they go into the background in order to conserve battery life and reduce the performance hit caused by tabs a user isn't looking at. In some situations, they will also reduce the amount of network traffic they process, and many ways around this involve using web-workers (which are able to remain persistent but have their own set of technicalities to contend with).
Firebase likely use either the page visibility API, or a web worker, or a combination of both.
I'm not 100% sure on the internals, but it's probable that Firebase store a hash of the latest data that was sent via realtime and then when the page/tab becomes active again, it'll send a request to compare the stored hash against a hash hosted on the server-side. If they're different, it'll have a mechanism to retrieve the latest data.
You could potentially build something like this yourself - add a Postgres trigger that generates a hash based upon some unique values (e.g. the epoch in ms along with the contents of the record that's inserted) each time a record is inserted, and store this in a dedicated table - perhaps have 1 row for each table in your DB.