Example custom subscription function?

What is an example of using a custom subscription function? It is mentioned in this blog post to be possible. I am trying out FaunaDB and using it in a Vercel/NextJS app.

1 Like

Although maybe a completely different use-case, the same is discussed here: Export big collections via CDC or dump to S3 or Snowflake

Copy paste from the above conv

Is using the temporal aspect of FaunaDB to monitor changes an option for you in the meantime?
It’s relatively easy (and efficient compared to dumbs) to say: ‘last sync of collection X was at timestamp Y, give me all created/updated/deleted documents’ thanks to the temporal features.

At this point it’s slightly inconvenient, you’ll have to combine two calls.

Get all events of a collection after timestamp

Paginate(Documents(Collection("fweets")), {
  events: true,
  after: Time("2020-05-22T19:12:07.121247Z")

this will give you create/delete events after a timestamp in an efficient way. Events on a collection only gives you create/delete so you are obviously missing updates there which is a bit cumbersome atm. So we’ll combine it with the next step.

Get all documents with timestamps after a given timestamp

You can however create an index on ‘ts’ and do the following.

  Lambda(['ts', 'ref'], Paginate(Var('ref'), {events: true, after: Time("2020-05-22T19:12:07.121247Z")}))

This will give you all documents that were created or changed after a given timestamp (create/update) and immediately gets the events of each of these documents after the same timestamp (update/create/delete)

Combine and conquer

FQL allows you to easily combine queries, so you could call those two in one transaction and therefore combine them in clean create/update/delete. I really need to write an example for that but as far as I know an external writer is working on that. In theory, that should give you access to a cheap and very robust (fault proof) pull-based sync system.

There is a writer currently writing about this and writing a nodejs package to simplify it (see topic above), current state of package: https://www.npmjs.com/package/faunadb-real-time

Thanks for the answer - I have the same question - Do you know where this is being written about - what blog? The npm package looks very interesting, but also looks to be part of the answer - I am only just starting with both Fauna and graphQL, so perhaps things that are self evident to others are still obscure to me - the package helps with polling, but presumably I need to connect that up with a graphQL subscription - You mentioned that someone external was writing about it - I’ve found potatopotaro’s repos but I can’t find any posts on the subject - do you know where he writes about it?

He is still writing and had to focus on another article iirc so it’s not out there yet. But in essence, you have to combine the two queries above to pull the changes. You will indeed have to connect a graphql subscription to that and probably have to stitch schemas together to implement subscription locally, the article will definitely not go into these GraphQL details. Of course, you could also simply implement a ‘getChangesForCollectionX’ GraphQL mutation and implement the changeset fetching with FQL via a User Defined Function (look into bindings and UDFs with FaunaDB and you’ll find some docs on using FaunaDB UDFs as resolvers).

That said, engineering is hard at work to provide subscriptions, in the end the goal is also to expose these subscriptions to the GraphQL layer.

1 Like