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.

ModeCaller sendsWorker sends
requestOne request, as data of the callOne response
pushA stream, then finishOne response, after the caller finished
pullOne request, as data of the callA stream, then finish
duplexA stream, then finishA 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:

123 B
{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:

62 B
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:

FieldContents
ownersOpen owners
subscriptionsOpen subscriptions
registrationsWorker registrations
rpcsLive exchanges
queuedItems waiting in all queues

Operations

OperationReturnsSummary
Events.statusdocumentevents.status() returns the event runtime's owned status document.
Events.open_ownerdocumentevents.open_owner() opens an event owner and returns an owned document with its id.
Events.close_ownernoneevents.close_owner(owner) ends everything an owner holds and cancels its exchanges.
Events.publishdocumentevents.publish(event) delivers one temporary message to every matching subscription and returns an owned count document.
Events.subscribedocumentevents.subscribe(owner, pattern) starts a subscription of an owner and returns an owned document with its id.
Events.receivedocumentevents.receive(options) returns the oldest queued event of a subscription as an owned document.
Events.unsubscribenoneevents.unsubscribe(owner, subscription) ends a subscription and drops its queued events.
Events.registerdocumentevents.register(owner, mode, pattern) registers an owner as a worker for exchanges and returns an owned document with the registration id.
Events.unregisternoneevents.unregister(owner, registration) ends a worker registration.
Events.acceptdocumentevents.accept(options) takes the oldest waiting exchange of a registration and returns an owned document describing it.
Events.calldocumentevents.call(request) opens an exchange with the earliest matching worker and returns an owned document with the caller side's handle.
Events.sendnoneevents.send(request) sends one message on an exchange side to the other party.
Events.finishnoneevents.finish(owner, handle) ends the sending direction of an exchange side.
Events.cancelnoneevents.cancel(owner, handle) cancels an exchange and drops the caller's side.
Events.readdocumentevents.read(options) returns the oldest notice of an exchange side as an owned document.

Events.status

python28 B
def status(self) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.

Errors

StatusWhen
"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

python35 B
result = await db.events().status()
Result / 85 B
{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

python32 B
def open_owner(self) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.

Errors

StatusWhen
"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

python39 B
result = await db.events().open_owner()
Result / 9 B
{owner 1}

C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release

Events.close_owner

python41 B
def close_owner(self, owner: int) -> None

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
ownerintValueRequiredThe owner that opened it, from open_owner.

Errors

StatusWhen
"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

python79 B
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

python46 B
def publish(self, event: Document) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
eventDocumentBorrowed for the callRequiredA borrowed object {subject data}. subject is a literal subject; data is any native value.

Errors

StatusWhen
"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

python242 B
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)
Result / 26 B
{delivered 1
 dropped   0}

C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release

Events.subscribe

python57 B
def subscribe(self, owner: int, pattern: str) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
ownerintValueRequiredThe owner that opened it, from open_owner.
patternstrValueRequiredA regular expression for the complete subject.

Errors

StatusWhen
"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

python99 B
await (await db.events().open_owner()).close()
result = await db.events().subscribe(1, "orders/.*")
Result / 16 B
{subscription 2}

C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release

Events.receive

python48 B
def receive(self, options: Document) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
optionsDocumentBorrowed for the callAbsent selects operation defaultsA borrowed object {owner subscription maxWait}. maxWait is an optional nonnegative native duration.

Errors

StatusWhen
"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

python389 B
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)
Result / 70 B
{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

python60 B
def unsubscribe(self, owner: int, subscription: int) -> None

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
ownerintValueRequiredThe owner that opened it, from open_owner.
subscriptionintValueRequiredThe id subscribe returned.

Errors

StatusWhen
"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

python142 B
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

python67 B
def register(self, owner: int, mode: str, pattern: str) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
ownerintValueRequiredThe owner that opened it, from open_owner.
modestrValueRequiredrequest, push, pull or duplex.
patternstrValueRequiredA regular expression for the complete subject.

Errors

StatusWhen
"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

python109 B
await (await db.events().open_owner()).close()
result = await db.events().register(1, "request", "orders/.*")
Result / 16 B
{registration 2}

C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release

Events.unregister

python59 B
def unregister(self, owner: int, registration: int) -> None

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
ownerintValueRequiredThe owner that opened it, from open_owner.
registrationintValueRequiredThe id register returned.

Errors

StatusWhen
"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

python151 B
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

python47 B
def accept(self, options: Document) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
optionsDocumentBorrowed for the callAbsent selects operation defaultsA borrowed object {owner registration maxWait}. maxWait is an optional nonnegative native duration.

Errors

StatusWhen
"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

python466 B
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)
Result / 86 B
{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

python45 B
def call(self, request: Document) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
requestDocumentBorrowed for the callRequiredA borrowed object {owner mode subject data}.

Errors

StatusWhen
"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

python320 B
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)
Result / 20 B
{handle 5
 rpc    4}

C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release

Events.send

python41 B
def send(self, request: Document) -> None

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
requestDocumentBorrowed for the callRequiredA borrowed object {owner handle data}.

Errors

StatusWhen
"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

python576 B
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

python49 B
def finish(self, owner: int, handle: int) -> None

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
ownerintValueRequiredThe owner that opened it, from open_owner.
handleintValueRequiredThe side handle: from call for the caller, from accept for the worker.

Errors

StatusWhen
"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

python329 B
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

python49 B
def cancel(self, owner: int, handle: int) -> None

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
ownerintValueRequiredThe owner that opened it, from open_owner.
handleintValueRequiredThe side handle: from call for the caller, from accept for the worker.

Errors

StatusWhen
"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

python329 B
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

python45 B
def read(self, options: Document) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfEventsBorrowed for the callRequiredThe events owner for this operation. Keep it alive during the call.
optionsDocumentBorrowed for the callAbsent selects operation defaultsA borrowed object {owner handle maxWait}. maxWait is an optional nonnegative native duration.

Errors

StatusWhen
"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

python707 B
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)
Result / 36 B
{kind message
 data {accepted true}}

C exports: stardust_db_plugin_call, stardust_result_take_document, stardust_result_error, stardust_result_release