EventSource

Learn: Track changes with Event Feeds and Event Streams

An event source emits an event when tracked changes occur in a database.

Create an event source

To create an event source, append set.eventSource() or set.eventsOn() to a supported Set:

// Tracks all changes to the `Product` collection.
Product.all().eventSource()

// Tracks all changes to the `name`, `price`,
// and `stock` fields in `Product` documents.
Product.all().eventsOn(.name, .price, .stock)

The query returns a hash with the EventSource type:

"g9WD1YPG..."

When consumed as an Event Feed or Event Stream, the event source emits JSON events when a tracked change occurs:

{
  "type": "add",
  "data": {
    "@doc": {
      "id": "392914348360597540",
      "coll": { "@mod": "Product" },
      "ts": { "@time": "2099-03-20T21:46:12.580Z" },
      "name": "cups",
      ...
    }
  },
  "txn_ts": 1710968002310000,
  "cursor": "gsGabc123",
  "stats": {
    "read_ops": 8,
    "storage_bytes_read": 208,
    "compute_ops": 1,
    "processing_time_ms": 0,
    "rate_limits_hit": []
  }
}

For event properties, see event schema.

Consume an event source

Applications can consume an event source in two ways:

  • Event Feeds : Asynchronous requests that poll the event source for paginated events.

  • Event Streams: Real-time subscriptions that push events from the event source to your application using an open connection to Fauna.

Instance methods

You can call the following instance methods on event sources in FQL.

Method Description

Apply a function to each element of an event source's tracked Set.

Get "[event source]" as a string.

Create an event source that emits events for a subset of another event source’s tracked Set.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!