Firebase Firestore Initial Payload with onSnapshot
Tags: realtime, javascript, programming, firestore
Published: 2020-06-09 23:56:24

---
title: Firebase Firestore Initial Payload with onSnapshot
published: true
date: 2018-09-06 18:42:19 UTC
tags: realtime,javascript,programming,firestore
cover_image: https://cdn-images-1.medium.com/max/1024/1*B0Av_NnFrCKsyC7gN94ytg.png
description: Simple way to check for initial payload returned by Firebase subscription.
canonical_url:
---

# Firebase Firestore Initial Payload with onSnapshot

![](https://cdn-images-1.medium.com/max/1024/1*B0Av_NnFrCKsyC7gN94ytg.png)<figcaption>Image from <a href="https://firebase.google.com/images/social.png">https://firebase.google.com/images/social.png</a></figcaption>

**NOTE** : This approach only works for web because Firestore doesn’t offer caching for web. On iOS and Android, you must take another approach to handle initial payload and subsequent updates due to caching. The first response from a subscription on iOS or Android will come from cache. Second response is new updates from Firestore and subsequent responses are updates/create/destroy only.

I’ve been using Firebase for a while now. One of the problems I’ve had with Firebase is this weird behaviour when you want to [subscribe to a query](https://firebase.google.com/docs/firestore/query-data/listen), it returns the whole result on the first call and returns new/updated documents afterward.

Let’s say I want to ignore the initial load and only listen to new changes. That’s just purely subscription. **How do you know which one is the first call?**

Firebase doesn’t provide any ways to check whether the result set is the first one. I’ve tried stackoverflow to see if anybody encountered the same problem. This [thread](https://stackoverflow.com/questions/49798693/differentate-between-first-time-querysnapshot-and-change-listener-in-firestore) is promising:

![](https://cdn-images-1.medium.com/max/1024/1*lZTfjQWe_7AfK1P7fi8EIA.png)<figcaption>Stackoverflow Thread <a href="https://stackoverflow.com/questions/49798693/differentate-between-first-time-querysnapshot-and-change-listener-in-firestore">https://stackoverflow.com/questions/49798693/differentate-between-first-time-querysnapshot-and-change-listener-in-firestore</a></figcaption>

Looks like the first query snapshot only contains added event. Sure, but what if you want to listen to new document events too? Do you have to handle the first added event the same as subsequent added events?

In my case, I want to **ignore the first payload** altogether and only listen to new updates. A new update can be an added event other other events. Therefore, checking whether it’s an added event for first payload doesn’t work.

What if I can count the number of invocation of the callback function and ignore the first invocation all together? Here’s a generic function wrapper that count the number of times the wrapped function is called.

{% gist https://gist.github.com/omnisyle/71e4c1984761433d18fa7ee0d4ae3843.js %}

createFnCounter() takes 2 arguments, first is the function being wrapped, and second is the number of invocation needed before the wrapped function is called. Therefore, createFnCounter(foo, 1) will only call foo on the second call. I’m pretty sure some names in this function can be more explicit but you get the idea (in case you want to discuss, I’m down for it).

How do you use this function? I have a collection named activities that contains all the activities happened to my chat channels collection. When a user sent a message to another user, there’s a new activity created/modified to notify the chat client, it’s saying, “Hey, there’s a new activity on this channel, do your thing”. The client received the event, and does its things. When the client initialize, I subscribe to activities collection as follow.

{% gist https://gist.github.com/omnisyle/a115d3f05dbf90fddb4959c1e2b200ac %}

Let’s incorporate createFnCounter to only receive updates and ignore initial payload.

{% gist https://gist.github.com/omnisyle/1637e679297cebbb7b5d288a2fe75bc6 %}

Too verbose, I know, but 6 months from now when I look back at this code, I’ll know what happened.

What if I want to get that initial payload too but do something different with it? Well, here we go

{% gist https://gist.github.com/omnisyle/0257ad9e94efbd11be8e3a6424b05174 %}

There you go, now you have a way to distinguish between initial load and subsequent updates when subscribing to documents on Firestore. Have other methods? Let me know!

Thanks for reading! ≈