Events
The events plugin carries temporary messages and request/response exchanges between the callers of one open database.
Use it to tell a live consumer that facts changed, to wake a worker, or to exchange streaming results without writing a fact for each message.
Enable it at open with plugins {events {}}. It has no settings.
See plugin selection for the open options.
Follow Checkers with the events plugin for a runnable progression through pub/sub, request, pull, push and duplex.
Events are not database state. A publication or an exchange message never enters the store, and commits never publish an event. The plugin does not retry, acknowledge or replay a message. Record anything that must survive as facts first, then publish an event that tells consumers to read those facts. Only definitions of publication subjects and exchange routes are stored. Events travel inside the process through the C API and the bindings; they use no network.
Owners
A caller opens an event owner with open_owner.
Subscriptions, worker registrations and exchange sides belong to the owner that created them.
Only that owner may use or end them; another owner's id reports wrong_owner.
close_owner ends everything the owner holds and cancels its exchanges: each other party reads a cancel notice.
Closing the database closes every owner and interrupts waiting calls with closed.
Publish and subscribe
A publisher sends one native value to a literal subject with publish.
A subject is a non-empty, slash-separated name with no empty segment, such as orders/created.
Publishing needs no owner, and a publication with no matching subscriber succeeds.
An owner starts a subscription with subscribe.
Its pattern is a regular expression that Stardust anchors to the complete subject.
Thus orders/.* matches orders/created, while created does not.
The owner pulls events with receive, which waits up to maxWait.
A subscription queues at most 1024 events or 8 MB, and queued bytes count against the database memory budget.
When a queue is full, or the budget refuses a delivery, Stardust drops that delivery without delaying the publisher or other subscribers.
The publisher learns the number of deliveries and drops from the result of publish.
A subscription reports the deliveries it missed in the dropped member of the event it returns next.
A publication holds at most 1 MB of data.
unsubscribe ends a subscription and drops its queued events.
Exchanges
An exchange connects one caller to one worker. A worker registers a mode and a subject pattern with register.
A caller opens that mode on a literal subject with call. The registration with the lowest id among the matches serves the call, and a call with no matching worker reports no_worker.
| Mode | Caller sends | Worker sends |
|---|---|---|
request | One request, as data of the call | One response |
push | A stream, then finish | One response, after the caller finished |
pull | One request, as data of the call | A stream, then finish |
duplex | A stream, then finish | A stream, then finish |
The worker takes the next exchange with accept, which returns the worker side's handle and, for request and pull, the request.
Each side sends with send, tells the other party it sent everything with finish, and reads with read.
read returns a message, a finish or a cancel notice. Finishing closes only the finishing side's sending direction, so it can still read.
cancel ends the exchange at once, and the other party reads cancel.
Closing either owner cancels the exchange.
Unregistering a worker cancels its exchanges that are still waiting for acceptance.
Exchanges that the worker already accepted continue after unregistering.
Each side queues at most 1024 messages or 8 MB against the memory budget; a send that does not fit reports delivery_failed and cancels the exchange, because dropping a message would corrupt it.
A completed or cancelled exchange leaves its sides readable until their queues drain, then they disappear.
Definitions
A stored definition of the event type fixes the request and response of a route.
Write it with patch_definition under the events kind at <mode>/<route>, for example request/orders/create.
Its document holds a JSON Schema for each direction:
{request {type object required [customer] properties {customer {type string}}}
response {type object required [accepted]}}The first path segment is the mode, which fixes who may stream. Use {} for a direction that carries any value.
A call with a definition checks its request, and every later message of the exchange against the schemas as they were when the call opened.
A message that breaks its schema reports invalid_payload and leaves the exchange running. A route without a definition is unchecked.
Definitions are ordinary stored definitions: history, backups and restores carry them.
A database that stores an event definition refuses to open without the events plugin, so its contracts cannot be skipped.
The SDK generation plugin renders typed publish, subscribe, caller and worker code for the stored routes in each language's output folder.
A publication definition lives at publish/<subject>, for example publish/orders/created.
Its document is the payload schema:
type object
required [order]
properties {order {type integer}}Each publication uses the subject's current schema before it changes any subscriber queue.
Invalid data reports invalid_payload, including when there are no subscribers.
Subjects without definitions remain unchecked.
Generated publish and subscribe helpers bind the subject and payload type; subscriptions match the exact subject.
Event results preserve empty objects and arrays inside payloads.
Runtime status
Call status to inspect these fields:
| Field | Contents |
|---|---|
owners | Open owners |
subscriptions | Open subscriptions |
registrations | Worker registrations |
rpcs | Live exchanges |
queued | Items waiting in all queues |
Operations
| Operation | Returns | Summary |
|---|---|---|
| Events.status | document | events.status() returns the event runtime's owned status document. |
| Events.open_owner | document | events.open_owner() opens an event owner and returns an owned document with its id. |
| Events.close_owner | none | events.close_owner(owner) ends everything an owner holds and cancels its exchanges. |
| Events.publish | document | events.publish(event) delivers one temporary message to every matching subscription and returns an owned count document. |
| Events.subscribe | document | events.subscribe(owner, pattern) starts a subscription of an owner and returns an owned document with its id. |
| Events.receive | document | events.receive(options) returns the oldest queued event of a subscription as an owned document. |
| Events.unsubscribe | none | events.unsubscribe(owner, subscription) ends a subscription and drops its queued events. |
| Events.register | document | events.register(owner, mode, pattern) registers an owner as a worker for exchanges and returns an owned document with the registration id. |
| Events.unregister | none | events.unregister(owner, registration) ends a worker registration. |
| Events.accept | document | events.accept(options) takes the oldest waiting exchange of a registration and returns an owned document describing it. |
| Events.call | document | events.call(request) opens an exchange with the earliest matching worker and returns an owned document with the caller side's handle. |
| Events.send | none | events.send(request) sends one message on an exchange side to the other party. |
| Events.finish | none | events.finish(owner, handle) ends the sending direction of an exchange side. |
| Events.cancel | none | events.cancel(owner, handle) cancels an exchange and drops the caller's side. |
| Events.read | document | events.read(options) returns the oldest notice of an exchange side as an owned document. |
Events.status
def status(self) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
Errors
| Status | When |
|---|---|
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
"out_of_memory" | native memory is exhausted |
Notes
The document counts owners, subscriptions, registrations, exchanges and queued items: see runtime status.
Examples
Count owners, subscriptions and queued items
result = await db.events().status(){owners 0
subscriptions 0
registrations 0
rpcs 0
queued 0}C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.open_owner
def open_owner(self) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
Errors
| Status | When |
|---|---|
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
"out_of_memory" | native memory is exhausted |
Notes
Subscriptions, registrations and exchange sides belong to an owner. See owners.
Examples
Open an event owner
result = await db.events().open_owner(){owner 1}C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.close_owner
def close_owner(self, owner: int) -> NoneParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
owner | int | Value | Required | The owner that opened it, from open_owner. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the request is not an object with only an integer owner |
"not_found" | the owner does not exist or ended |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
Examples
Close an owner and everything it holds
await (await db.events().open_owner()).close()
await db.events().close_owner(1)C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.publish
def publish(self, event: Document) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
event | Document | Borrowed for the call | Required | A borrowed object {subject data}. subject is a literal subject; data is any native value. |
Errors
| Status | When |
|---|---|
"invalid_argument" | event is not an object with exactly subject and data, the subject is malformed (code invalid_subject), or data exceeds 1 MB (code too_large) |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
"out_of_memory" | native memory is exhausted |
Notes
A publication is never stored or replayed. See publishing.
Examples
Publish a temporary message
await (await db.events().open_owner()).close()
await (await db.events().subscribe(1, "orders/.*")).close()
async with await stardust.parse("{subject orders/created data {order 42}}", "dust") as doc:
result = await db.events().publish(doc){delivered 1
dropped 0}C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.subscribe
def subscribe(self, owner: int, pattern: str) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
owner | int | Value | Required | The owner that opened it, from open_owner. |
pattern | str | Value | Required | A regular expression for the complete subject. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the request is malformed, or the pattern is empty or not a valid regular expression (code invalid_pattern) |
"not_found" | the owner does not exist |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
"out_of_memory" | native memory is exhausted |
Notes
A pattern is a regular expression anchored to the complete subject. See subscriptions.
Examples
Subscribe to every order subject
await (await db.events().open_owner()).close()
result = await db.events().subscribe(1, "orders/.*"){subscription 2}C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.receive
def receive(self, options: Document) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
options | Document | Borrowed for the call | Absent selects operation defaults | A borrowed object {owner subscription maxWait}. maxWait is an optional nonnegative native duration. |
Errors
| Status | When |
|---|---|
"invalid_argument" | options is not an object with owner and subscription (integers) and optionally a valid maxWait, or the subscription belongs to another owner (code wrong_owner) |
"not_found" | the owner or the subscription does not exist |
"timeout" | maxWait passed before an event arrived |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
"out_of_memory" | native memory is exhausted |
Notes
receive waits up to maxWait. Without it the call checks once and reports timeout when no event is queued.
Examples
Receive the oldest queued event
await (await db.events().open_owner()).close()
await (await db.events().subscribe(1, "orders/.*")).close()
async with await stardust.parse("{subject orders/created data {order 42}}", "dust") as doc:
await (await db.events().publish(doc)).close()
async with await stardust.parse("{owner 1 subscription 2 maxWait {#dur PT1S}}", "dust") as doc:
result = await db.events().receive(doc){subject orders/created
sequence 1
dropped 0
data {order 42}}C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.unsubscribe
def unsubscribe(self, owner: int, subscription: int) -> NoneParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
owner | int | Value | Required | The owner that opened it, from open_owner. |
subscription | int | Value | Required | The id subscribe returned. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the request is malformed, or the subscription belongs to another owner (code wrong_owner) |
"not_found" | the owner or the subscription does not exist or ended |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
Examples
End a subscription
await (await db.events().open_owner()).close()
await (await db.events().subscribe(1, "orders/.*")).close()
await db.events().unsubscribe(1, 2)C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.register
def register(self, owner: int, mode: str, pattern: str) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
owner | int | Value | Required | The owner that opened it, from open_owner. |
mode | str | Value | Required | request, push, pull or duplex. |
pattern | str | Value | Required | A regular expression for the complete subject. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the request is malformed, the mode is unknown (code invalid_mode) or the pattern is empty or invalid (code invalid_pattern) |
"not_found" | the owner does not exist |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
"out_of_memory" | native memory is exhausted |
Notes
The earliest matching registration serves each call. See exchanges.
Examples
Register an owner as a worker
await (await db.events().open_owner()).close()
result = await db.events().register(1, "request", "orders/.*"){registration 2}C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.unregister
def unregister(self, owner: int, registration: int) -> NoneParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
owner | int | Value | Required | The owner that opened it, from open_owner. |
registration | int | Value | Required | The id register returned. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the request is malformed, or the registration belongs to another owner (code wrong_owner) |
"not_found" | the owner or the registration does not exist |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
Examples
End a worker registration
await (await db.events().open_owner()).close()
await (await db.events().register(1, "request", "orders/.*")).close()
await db.events().unregister(1, 2)C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.accept
def accept(self, options: Document) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
options | Document | Borrowed for the call | Absent selects operation defaults | A borrowed object {owner registration maxWait}. maxWait is an optional nonnegative native duration. |
Errors
| Status | When |
|---|---|
"invalid_argument" | options is not an object with owner and registration (integers) and optionally a valid maxWait, or the registration belongs to another owner (code wrong_owner) |
"not_found" | the owner or the registration does not exist |
"timeout" | maxWait passed before an exchange arrived |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
"out_of_memory" | native memory is exhausted |
Notes
accept waits up to maxWait. The result carries the worker side's handle: see exchanges.
Examples
Take the oldest waiting exchange as the worker
await (await db.events().open_owner()).close()
await (await db.events().open_owner()).close()
await (await db.events().register(2, "request", "orders/.*")).close()
async with await stardust.parse("{owner 1 mode request subject orders/create data {customer Ada}}", "dust") as doc:
await (await db.events().call(doc)).close()
async with await stardust.parse("{owner 2 registration 3 maxWait {#dur PT1S}}", "dust") as doc:
result = await db.events().accept(doc){handle 6
rpc 4
mode request
subject orders/create
data {customer Ada}}C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.call
def call(self, request: Document) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
request | Document | Borrowed for the call | Required | A borrowed object {owner mode subject data}. |
Errors
| Status | When |
|---|---|
"invalid_argument" | request is malformed, the mode or subject is invalid (codes invalid_mode and invalid_subject), data is missing for request or pull, present for push or duplex, or exceeds 1 MB, or data does not match the stored request schema (code invalid_payload) |
"not_found" | the owner does not exist, or no registered worker matches (code no_worker) |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
"out_of_memory" | native memory is exhausted |
Notes
A stored definition for the route checks every message. See exchanges and definitions.
Examples
Open a request exchange with a worker
await (await db.events().open_owner()).close()
await (await db.events().open_owner()).close()
await (await db.events().register(2, "request", "orders/.*")).close()
async with await stardust.parse("{owner 1 mode request subject orders/create data {customer Ada}}", "dust") as doc:
result = await db.events().call(doc){handle 5
rpc 4}C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.send
def send(self, request: Document) -> NoneParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
request | Document | Borrowed for the call | Required | A borrowed object {owner handle data}. |
Errors
| Status | When |
|---|---|
"invalid_argument" | request is malformed, the side may not send now (code wrong_direction), the handle belongs to another owner (code wrong_owner), or data does not match its schema (code invalid_payload) |
"not_found" | the owner or the side does not exist, or the exchange ended and the side drained |
"conflict" | the side already finished (code already_finished), or the other party's queue is full (code delivery_failed) |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
Notes
A message is checked against the stored schemas of its route. See exchanges.
Examples
Answer a request as the worker
await (await db.events().open_owner()).close()
await (await db.events().open_owner()).close()
await (await db.events().register(2, "request", "orders/.*")).close()
async with await stardust.parse("{owner 1 mode request subject orders/create data {customer Ada}}", "dust") as doc:
await (await db.events().call(doc)).close()
async with await stardust.parse("{owner 2 registration 3}", "dust") as doc:
await (await db.events().accept(doc)).close()
async with await stardust.parse("{owner 2 handle 6 data {accepted true}}", "dust") as doc:
await db.events().send(doc)C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.finish
def finish(self, owner: int, handle: int) -> NoneParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
owner | int | Value | Required | The owner that opened it, from open_owner. |
handle | int | Value | Required | The side handle: from call for the caller, from accept for the worker. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the request is malformed, the side cannot finish in this mode (code wrong_direction), or the handle belongs to another owner (code wrong_owner) |
"not_found" | the owner or the side does not exist |
"conflict" | the side already finished (code already_finished), or the other party's queue is full (code delivery_failed) |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
Examples
Finish the caller's stream
await (await db.events().open_owner()).close()
await (await db.events().open_owner()).close()
await (await db.events().register(2, "push", "uploads/.*")).close()
async with await stardust.parse("{owner 1 mode push subject uploads/a}", "dust") as doc:
await (await db.events().call(doc)).close()
await db.events().finish(1, 5)C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.cancel
def cancel(self, owner: int, handle: int) -> NoneParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
owner | int | Value | Required | The owner that opened it, from open_owner. |
handle | int | Value | Required | The side handle: from call for the caller, from accept for the worker. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the request is malformed, or the handle belongs to another owner (code wrong_owner) |
"not_found" | the owner or the side does not exist |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
Examples
Cancel an exchange
await (await db.events().open_owner()).close()
await (await db.events().open_owner()).close()
await (await db.events().register(2, "push", "uploads/.*")).close()
async with await stardust.parse("{owner 1 mode push subject uploads/a}", "dust") as doc:
await (await db.events().call(doc)).close()
await db.events().cancel(1, 5)C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release
Events.read
def read(self, options: Document) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Events | Borrowed for the call | Required | The events owner for this operation. Keep it alive during the call. |
options | Document | Borrowed for the call | Absent selects operation defaults | A borrowed object {owner handle maxWait}. maxWait is an optional nonnegative native duration. |
Errors
| Status | When |
|---|---|
"invalid_argument" | options is not an object with owner and handle (integers) and optionally a valid maxWait, or the handle belongs to another owner (code wrong_owner) |
"not_found" | the owner or the side does not exist, or it drained after the exchange ended |
"timeout" | maxWait passed before a notice arrived |
"unavailable" | the database does not enable the events plugin |
"closed" | the database is closed |
"out_of_memory" | native memory is exhausted |
Notes
A message, the other party's finish or a cancel arrives as kind message, finish or cancel.
Examples
Read the worker's response
await (await db.events().open_owner()).close()
await (await db.events().open_owner()).close()
await (await db.events().register(2, "request", "orders/.*")).close()
async with await stardust.parse("{owner 1 mode request subject orders/create data {customer Ada}}", "dust") as doc:
await (await db.events().call(doc)).close()
async with await stardust.parse("{owner 2 registration 3}", "dust") as doc:
await (await db.events().accept(doc)).close()
async with await stardust.parse("{owner 2 handle 6 data {accepted true}}", "dust") as doc:
await db.events().send(doc)
async with await stardust.parse("{owner 1 handle 5 maxWait {#dur PT1S}}", "dust") as doc:
result = await db.events().read(doc){kind message
data {accepted true}}C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release