---
question: Should I publish for fanout or enqueue for a worker?
kind: task
via_web: na
via_cli: na
via_python: available
via_api: na
via_config: na
ref_python: engine.services.bus.publish
prereq: A module that declares the `bus` service. [Create a module](/capabilities/build/modules/creating-a-module) first.
next:
  - /capabilities/build/platform/consume-a-queue
  - /capabilities/build/platform/message-bus-overview
---

# Publish a message

The bus has two ways to send. `publish` is fanout: every subscriber that is listening gets a copy, and nothing is kept for one that is not. `enqueue` puts the message on a work queue: it waits until exactly one worker takes it.

## Web UI

Not applicable. Nexus does not send on the bus. In NATS mode, super admins can see what reached a JetStream stream under **System** › **Data** › **Bus**. Use the Python steps on this page.

## CLI

Not applicable. There is no `abi` command that publishes on the bus. Use the Python steps on this page. The command list is the [CLI reference](/user-guide/reference/cli).

## Python

### Declare the service

```python title="src/my_module/__init__.py"
from naas_abi_sdk import BaseModule, ModuleDependencies


class ABIModule(BaseModule):
    dependencies = ModuleDependencies(services=("bus",))
```

### Publish to subscribers

```python
import json

bus = self.engine.services.bus
await bus.publish("my_module.orders", "created", json.dumps({"id": 1}).encode())
```

The arguments are the topic, the routing key and the payload as `bytes`. Subscribers on `my_module.orders` with the routing key `created`, `*` or `#` get it. Topics are shared by every module, so prefix yours. The call returns once the broker has the message.

### Or hand it to one worker

Use a topic of its own for the work queue:

```python
await bus.enqueue("my_module.order_jobs", "created", json.dumps({"id": 1}).encode())
```

The message goes to one worker on that topic and routing key, and waits on the topic's JetStream stream until one takes it, across restarts. The worker is [Consume a queue](/capabilities/build/platform/consume-a-queue).

:::note The stream must exist
Logged in as the broker user `module`, a module may not create streams. On a topic whose stream the engine has not created, `enqueue` raises `StreamNotCreated` (`naas_abi_sdk.streams`). For work the module itself must run once, a [module job](/capabilities/build/modules/module-jobs) uses the jobs stream the engine prepares.
:::

:::warning One model per topic
Do not `publish` and `enqueue` on the same topic. The work queue of a topic is a JetStream stream that captures every subject under it, so once a queue exists, plain publishes on that topic are stored too and handed to its workers.
:::

### Send a batch

```python
await bus.publish_many("my_module.orders", [("created", b"..."), ("created", b"...")])
```

It publishes the pairs in order, one after another. A failure stops there: the messages already sent stay sent, and none is retried.

## REST API

Not applicable. No Nexus API route sends on the bus. The System routes read JetStream streams in NATS mode, and a write answers 405. Use the Python steps on this page.

## Config

Not applicable. A message is a call. The bus is JetStream on the broker that `nats.nats_url` names. See the [Configuration Reference](/capabilities/get-started/configuration#message-bus).

## Good to know

- **Errors.** A send that fails raises: a timeout, a lost connection, or `ClaimCheckTooLarge` for a payload above 256 MiB. The module's bus calls record no `BusError` event; log the failure yourself.
- **Topics starting with `evt.`** belong to the event service. Publish events with [Record an event](/capabilities/build/platform/record-an-event), not on the bus.
