Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Worthwhile mentioning how to use with subscribeToMore? (+ possible caveats) #9

Closed
s-h-a-d-o-w opened this issue Nov 21, 2019 · 1 comment

Comments

@s-h-a-d-o-w
Copy link
Contributor

I was scratching my head for quite a while about the behavior of this (based on the full example) compared to simply publishing directly on mutations with something like graphql-yoga's pubsub.

Seeing how I suppose neither using the apollo-client, nor subscribeToMore is particularly rare, I wondered whether it would be worth mentioning in the README that if one uses it, one has to skip broadcasting the initial snapshot. (I keep track of it with an isInitialSnapshot in the handler.)

This has also made me realize that in a scenario where you have e.g. 1000 documents in that collection and hundreds of people per hour loading up the page, that's a lot of unnecessary load and traffic. Maybe this would also be good to point out?

@psteinroe
Copy link

I struggled with the same. One key information is that registerHandler gets called every time someone subscribes.

This is how it works for me:

ps.registerHandler(events.MESSAGE_ADDED, broadcast => {
  let isInitialSnapshot = true;
  firestore.collectionGroup('Messages').onSnapshot(snapshot => {
    if (isInitialSnapshot) {
      isInitialSnapshot = false;
      return;
    }
    snapshot
      .docChanges()
      .filter(change => change.type === 'added')
      .map(async item => {
        return broadcast({
          chatId: item.doc.ref.parent.parent.id,
          message: item.doc.data(),
        });
      });
  });
});

@m19c m19c closed this as completed in d6e1e1e Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants