---
question: Should I dequeue a message or subscribe for fanout?
kind: task
via_web: na
via_cli: na
via_python: available
via_api: na
via_config: na
ref_python: bus.dequeue
prereq: An in-engine module that lists `BusService` in its dependencies, and messages sent with `enqueue`. See [Publish a message](/user-guide/build/platform/publish-a-message?via=python).
next:
  - /capabilities/build/platform/publish-a-message
  - /capabilities/build/platform/message-bus-overview
---

# Consume a queue

`dequeue` is the work queue side: each message goes to one consumer, and stays queued until a consumer finishes with it. `subscribe` is the fanout side: every subscriber gets every message published while it listens.

## Web UI

Not applicable. Nexus does not consume from the bus. In NATS mode, super admins can open a JetStream stream under **System** › **Data** › **Bus**, read its stored messages, and delete one so no consumer gets it. Use the Python steps on this page.

## CLI

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

## Python

### Write the callback

```python
import json

def handle(payload: bytes) -> None:
    order = json.loads(payload)
    ...  # do the work
```

The callback receives the payload as `bytes`. When it returns, the message is acknowledged: no other consumer gets it.

### Start the consumer

```python
consumer = self.engine.services.bus.dequeue("my_module.order_jobs", "created", handle)
```

`dequeue` returns the thread that runs the loop. Every `dequeue` on the same topic and routing key shares one queue: start several to share the work, and each message still goes to one of them. Keep queue topics apart from the topics you `publish` on: see [Publish a message](/user-guide/build/platform/publish-a-message?via=python).

### Handle failures

An exception in the callback puts the message back on the queue, for this or another consumer, and ends this consumer's loop. Catch what you can retry later, and call `dequeue` again to restart a consumer that stopped. Raise `StopIteration` to stop the loop on purpose: the current message counts as done.

### Or listen without a queue

```python
self.engine.services.bus.subscribe("my_module.orders", "#", handle)
```

`subscribe` does not compete for messages and does not keep any: it only sees what is sent while it listens. Use it to react, not to do work that must happen once. The routing key is a pattern: `*` matches one segment, a trailing `#` the rest.

## REST API

Not applicable. No Nexus API route consumes from the bus. In NATS mode the System routes list and read stored stream messages, for super admins. Use the Python steps on this page.

## Config

Not applicable. A consumer is a call. Which bus backs it is set by the `nats:` block, or `services.bus` without NATS: see [Message bus](/user-guide/build/platform/message-bus-overview#backends).
