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.

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.

Map(Paginate(
    Range(
      Match(Index("fweets_after_ts")),
      ToMicros(Time("2020-05-22T19:12:07.121247Z")),
      null
    )
  ),
  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