Menu

Event Subscription

Overview

You can subscribe to Crux events using crux.api/listen

(crux.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 :crux/event-type

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

Events

Event Type Trigger

:crux/indexed-tx

Crux 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

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

This parameter is a map of the following

Key Value Type Value

:crux/event-type

Keyword

:crux/indexed-tx

:crux.tx/tx-id

Long

ID of the indexed transaction

:crux.tx/tx-time

Date

Transaction time of the indexed transaction

:committed?

Boolean

Whether the transaction was committed or aborted

:crux/tx-ops

List

List of the transaction operations indexed

Example

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

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

Will print

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