---
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: engine.services.bus.dequeue
prereq: A module that declares the `bus` service, and messages sent with `enqueue`. See [Publish a message](/capabilities/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

### Open the queue

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

`dequeue` returns a durable pull subscription. Every `dequeue` on the same topic and routing key shares one consumer: start several to share the work, and each message still goes to one of them. Like `enqueue`, it raises `StreamNotCreated` when the topic's stream does not exist. Keep queue topics apart from the topics you `publish` on: see [Publish a message](/capabilities/build/platform/publish-a-message?via=python).

### Fetch and acknowledge

```python
import json

from nats.errors import TimeoutError

while True:
    try:
        messages = await queue.fetch(10, timeout=5)
    except TimeoutError:
        continue  # nothing waiting
    for msg in messages:
        try:
            order = json.loads(msg.data)
            ...  # do the work
        except Exception:
            await msg.nak()  # back on the queue, for this or another worker
            continue
        await msg.ack()  # done: no other worker gets it
```

`fetch(batch, timeout)` waits up to `timeout` seconds for at most `batch` messages. `msg.data` is the payload as `bytes`, read back from the claim-check store when it was too large for the broker. A message neither acknowledged nor nacked comes back after the broker's acknowledgement wait. Run the loop in a task the module owns, and stop it in `on_unloaded`.

### Or listen without a queue

```python
subscription = await bus.subscribe("my_module.orders", "#")
async for msg in subscription.messages:
    ...  # msg.data is the payload
```

`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, on the broker that `nats.nats_url` names: see [Message bus](/capabilities/build/platform/message-bus-overview#backend).
