#community-help

Typesense Firestore Real-Time Update Query

TLDR DomijKu required help with real-time updates on frontend when backends changes occur. Jason, CaptainCodeman and Loic explained triggering updates via direct subscription to Firestore, cloud function triggers, and stream subscriptions. Loic recommended switching to Typesense for searches beyond 20 documents.

Powered by Struct AI

2

1

1

10
17mo
Solved
Join the chat
May 26, 2022 (17 months ago)
DomijKu
Photo of md5-161fbff60dfc1f70540ac9b1b55dd455
DomijKu
01:56 PM
Hey guys and girls, hope you are all doing great.

Got a bit desperate so here I am :)) Anyone use Search Firestore with Typesense extension and know how to "listen" for a collection update in Typesense and refetch data as soon as that happens? E.g. creating or deleting a document and updating the collection in real time, something like how Firestore's snapshot works? It of course works with a setTimeout delay and manually refetching, but surely that's not the way to do it..

Thank you!
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:36 PM
DomijKu The Typesense Firestore extension does this for you, but it does it in reverse - it watches your Firestore collection for any updates and then ingests the changes into Typesense in real-time: https://firebase.google.com/products/extensions/typesense-firestore-typesense-search
DomijKu
Photo of md5-161fbff60dfc1f70540ac9b1b55dd455
DomijKu
02:39 PM
Thank you, Jason! Yes, I am aware of that. What I am trying to do is get some kind of trigger when that happens, so I could trigger an update on the front end. It does whenver the page rerenders of course, but I am trying to figure whether it can be real time. Does that make any sense?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:40 PM
Typesense doesn't have these triggers built-in. But you could create these triggers via a cloud function trigger on your Firestore database directly and use a pub-sub mechanism to broadcast the change to clients (See CaptainCodeman's note below)
DomijKu
Photo of md5-161fbff60dfc1f70540ac9b1b55dd455
DomijKu
02:43 PM
Aaah, got it. That's a bit over my head for now, but at least I know where to start looking. Thank you very much, you are a legend! :)

1

1

CaptainCodeman
Photo of md5-d3a4ca49ba4aeb3b9d0cb7d846eb0989
CaptainCodeman
11:37 PM
You can just subscribe to firestore directly on the client, and it will trigger callbacks as the backend data changes

1

May 27, 2022 (17 months ago)
Loic
Photo of md5-3a88a602e4aba6a18b4a6c6985fcf08c
Loic
04:46 AM
DomijKu Typesense is a « replica » of your firestore and, through the extension, is updated anytime you CRUD your data in firestore.

For real-time in your UI, you should get a stream subscription from firestore.

If you have to get results from Typesense (for technical or price reasons for example), you could trigger a call to Typesense with a periodic timer but it is a little hacky
DomijKu
Photo of md5-161fbff60dfc1f70540ac9b1b55dd455
DomijKu
05:44 AM
Thank you Loic. Excuse of naivete as I am quite a newbie, but do I understand correctly that I should use Firestore's onSnapshot for the regular use and switch to Typesense's collection whenever a search or sort happens?

1

Loic
Photo of md5-3a88a602e4aba6a18b4a6c6985fcf08c
Loic
04:07 PM
DomijKu I recommend to switch on Typesense search as soon as you will go through more than 20 documents with Firestore
May 28, 2022 (17 months ago)
DomijKu
Photo of md5-161fbff60dfc1f70540ac9b1b55dd455
DomijKu
07:09 AM
Thank you Loic!