Menu

Events

Overview

You can subscribe to XTDB events using xtdb.api/listen

(xtdb.api/listen node
                 event-opts (1)
                 f) (2)
1 A map which contains which event to listen to along with any relevant options
2 The function to run when the event triggers

event-ops must contain ::xt/event-type

You can .close the return value from (xtdb.api/listen …​) to detach the listener, should you need to.

Events

Event Type Trigger

::xt/indexed-tx

XTDB indexing a transaction

Indexed Transaction

Event Options

Key Type Explanation

:with-tx-ops?

Boolean

Whether to include the transaction operations in the event data

Function Parameters

::xt/indexed-tx passes one parameter to the function supplied to xtdb.api/listen

This parameter is a map of the following

Key Value Type Value

::xt/event-type

Keyword

::xt/indexed-tx

::xt/tx-id

Long

ID of the indexed transaction

::xt/tx-time

Date

Transaction time of the indexed transaction

:committed?

Boolean

Whether the transaction was committed or aborted

::xt/tx-ops

List

List of the transaction operations indexed

Example

(xt/listen node {::xt/event-type ::xt/indexed-tx, :with-tx-ops? true}
  (fn [ev]
    (println "event received!")
    (clojure.pprint/pprint ev)))

(xt/submit-tx node [[::xt/put {:xt/id :ivan, :name "Ivan"}]])

Will print

event received!
{::xt/event-type ::xt/indexed-tx,
 ::xt/tx-id ...,
 ::xt/tx-time #inst "...",
 :committed? true,
 ::xt/tx-ops [[::xt/put {:xt/id :ivan, :name "Ivan"}]]}