import tempfile
import pyarrow as pa
import jammi
db = jammi.connect(f"file://{tempfile.mkdtemp()}")36 Trigger streams
End-to-end publish + subscribe on a Jammi topic, plus the registration and listing surface. Uses the embedded in-process broker — no external broker needed.
When to use this pattern. You need a low-friction event bus inside your application — for fan-out to downstream consumers, fan-in from batch jobs, or replay-from-offset semantics — without bringing up a separate message broker. The same surface scales out across replicas on the Postgres broker ([broker.postgres]) by a config change at deploy time.
36.1 API surface exercised
Session.register_topic(name, *, schema)Session.list_topics()Session.publish_topic(name, *, batch)— returns the assigned offsetSession.subscribe_collect(name, *, from_offset)Session.drop_topic(name, *, if_exists=False)
subscribe_collect drains the backing table from from_offset and returns; replay_only=False with max_batches follows the live tail instead, returning once that many batches arrive.
The program is cookbook/recipes/trigger_streams/example.py; its steps run here in order.
36.2 Register a topic
A topic carries Arrow record batches of one schema. Registering it adds it to the catalog, where list_topics finds it.
schema = pa.schema(
[
pa.field("event_id", pa.int64(), nullable=False),
pa.field("payload", pa.string(), nullable=False),
]
)
topic_id = db.register_topic("events.demo", schema=schema)
assert topic_id, "register_topic returned empty id"
topics = db.list_topics()
assert "events.demo" in topics, f"events.demo missing from {topics}"
print(topics)['events.demo']
36.3 Publish a batch
The broker assigns each topic’s batches sequential offsets, from 0 for a fresh topic, and publish_topic returns the one it assigned.
batch = pa.table(
{
"event_id": pa.array([1, 2, 3], type=pa.int64()),
"payload": pa.array(["alpha", "beta", "gamma"], type=pa.string()),
},
schema=schema,
)
offset = db.publish_topic("events.demo", batch=batch)
assert offset == 0, f"expected offset 0, got {offset}"36.4 Read it back from an offset
Every published batch is also stored in the topic’s backing table, so a subscriber can replay from any offset. subscribe_collect drains that table from from_offset and returns what it read.
collected = db.subscribe_collect("events.demo", from_offset=0)
assert collected.column("event_id").to_pylist() == [1, 2, 3]
assert collected.column("payload").to_pylist() == ["alpha", "beta", "gamma"]
print(collected.to_pydict()){'event_id': [1, 2, 3], 'payload': ['alpha', 'beta', 'gamma']}
36.5 Drop the topic
drop_topic removes it from the catalog. With if_exists=True, a topic that is already gone is not an error; without it, the error names the topic.
db.drop_topic("events.demo")
assert "events.demo" not in db.list_topics(), "events.demo persisted after drop"
db.drop_topic("events.demo", if_exists=True)
try:
db.drop_topic("never.registered")
except (ValueError, RuntimeError) as missing:
assert "never.registered" in str(missing), f"drop-missing error lost topic name: {missing}"
print(missing)
else:
raise AssertionError("drop-missing must raise")topic 'never.registered' not found
db.close()