Powered By GitBook
Consumers
Messages are read or consumed from the Stream by Consumers. We support pull and push-based Consumers and the example scenario has both, let's walk through that.

Creating Pull-Based Consumers

The NEW and DISPATCH Consumers are pull-based, meaning the services consuming data from them have to ask the system for the next available message. This means you can easily scale your services up by adding more workers and the messages will get spread across the workers based on their availability.
Pull-based Consumers are created the same as push-based Consumers, you just don't specify a delivery target.
1
$ nats con ls ORDERS
2
No Consumers defined
Copied!
We have no Consumers, lets add the NEW one:
I supply the --sample options on the CLI as this is not prompted for at present, everything else is prompted. The help in the CLI explains each:
1
$ nats con add --sample 100
2
? Select a Stream ORDERS
3
? Consumer name NEW
4
? Delivery target
5
? Start policy (all, last, 1h, msg sequence) all
6
? Filter Stream by subject (blank for all) ORDERS.received
7
? Maximum Allowed Deliveries 20
8
Information for Consumer ORDERS > NEW
9
10
Configuration:
11
12
Durable Name: NEW
13
Pull Mode: true
14
Subject: ORDERS.received
15
Deliver All: true
16
Deliver Last: false
17
Ack Policy: explicit
18
Ack Wait: 30s
19
Replay Policy: instant
20
Maximum Deliveries: 20
21
Sampling Rate: 100
22
23
State:
24
25
Last Delivered Message: Consumer sequence: 1 Stream sequence: 1
26
Acknowledgment floor: Consumer sequence: 0 Stream sequence: 0
27
Pending Messages: 0
28
Redelivered Messages: 0
Copied!
This is a pull-based Consumer (empty Delivery Target), it gets messages from the first available message and requires specific acknowledgement of each and every message.
It only received messages that originally entered the Stream on ORDERS.received. Remember the Stream subscribes to ORDERS.*, this lets us select a subset of messages from the Stream.
A Maximum Delivery limit of 20 is set, this means if the message is not acknowledged it will be retried but only up to this maximum total deliveries.
Again this can all be done in a single CLI call, lets make the DISPATCH Consumer:
1
$ nats con add ORDERS DISPATCH --filter ORDERS.processed --ack explicit --pull --deliver all --sample 100 --max-deliver 20
Copied!
Additionally, one can store the configuration in a JSON file, the format of this is the same as $ nats con info ORDERS DISPATCH -j | jq .config:
1
$ nats con add ORDERS MONITOR --config monitor.json
Copied!

Creating Push-Based Consumers

Our MONITOR Consumer is push-based, has no ack and will only get new messages and is not sampled:
1
$ nats con add
2
? Select a Stream ORDERS
3
? Consumer name MONITOR
4
? Delivery target monitor.ORDERS
5
? Start policy (all, last, 1h, msg sequence) last
6
? Acknowledgement policy none
7
? Replay policy instant
8
? Filter Stream by subject (blank for all)
9
? Maximum Allowed Deliveries -1
10
Information for Consumer ORDERS > MONITOR
11
12
Configuration:
13
14
Durable Name: MONITOR
15
Delivery Subject: monitor.ORDERS
16
Deliver All: false
17
Deliver Last: true
18
Ack Policy: none
19
Replay Policy: instant
20
21
State:
22
23
Last Delivered Message: Consumer sequence: 1 Stream sequence: 3
24
Acknowledgment floor: Consumer sequence: 0 Stream sequence: 2
25
Pending Messages: 0
26
Redelivered Messages: 0
Copied!
Again you can do this with a single non-interactive command:
1
$ nats con add ORDERS MONITOR --ack none --target monitor.ORDERS --deliver last --replay instant --filter ''
Copied!
Additionally one can store the configuration in a JSON file, the format of this is the same as $ nats con info ORDERS MONITOR -j | jq .config:
1
$ nats con add ORDERS --config monitor.json
Copied!

Listing

You can get a quick list of all the Consumers for a specific Stream:
1
$ nats con ls ORDERS
2
Consumers for Stream ORDERS:
3
4
DISPATCH
5
MONITOR
6
NEW
Copied!

Querying

All details for a Consumer can be queried, lets first look at a pull-based Consumer:
1
$ nats con info ORDERS DISPATCH
2
Information for Consumer ORDERS > DISPATCH
3
4
Configuration:
5
6
Durable Name: DISPATCH
7
Pull Mode: true
8
Subject: ORDERS.processed
9
Deliver All: true
10
Deliver Last: false
11
Ack Policy: explicit
12
Ack Wait: 30s
13
Replay Policy: instant
14
Sampling Rate: 100
15
16
State:
17
18
Last Delivered Message: Consumer sequence: 1 Stream sequence: 1
19
Acknowledgment floor: Consumer sequence: 0 Stream sequence: 0
20
Pending Messages: 0
21
Redelivered Messages: 0
Copied!
More details about the State section will be shown later when discussing the ack models in depth.

Consuming Pull-Based Consumers

Pull-based Consumers require you to specifically ask for messages and ack them, typically you would do this with the client library Request() feature, but the nats utility has a helper:
First, we ensure we have a message:
1
$ nats pub ORDERS.processed "order 1"
2
$ nats pub ORDERS.processed "order 2"
3
$ nats pub ORDERS.processed "order 3"
Copied!
We can now read them using nats:
1
$ nats con next ORDERS DISPATCH
2
--- received on ORDERS.processed
3
order 1
4
5
Acknowledged message
6
7
$ nats con next ORDERS DISPATCH
8
--- received on ORDERS.processed
9
order 2
10
11
Acknowledged message
Copied!
You can prevent ACKs by supplying --no-ack.
To do this from code you'd send a Request() to $JS.API.CONSUMER.MSG.NEXT.ORDERS.DISPATCH:
1
$ nats req '$JS.API.CONSUMER.MSG.NEXT.ORDERS.DISPATCH' ''
2
Published [$JS.API.CONSUMER.MSG.NEXT.ORDERS.DISPATCH] : ''
3
Received [ORDERS.processed] : 'order 3'
Copied!
Here nats req cannot ack, but in your code you'd respond to the received message with a nil payload as an Ack to JetStream.

Consuming Push-Based Consumers

Push-based Consumers will publish messages to a subject and anyone who subscribes to the subject will get them, they support different Acknowledgement models covered later, but here on the MONITOR Consumer we have no Acknowledgement.
1
$ nats con info ORDERS MONITOR
2
...
3
Delivery Subject: monitor.ORDERS
4
...
Copied!
The Consumer is publishing to that subject, so let's listen there:
1
$ nats sub monitor.ORDERS
2
Listening on [monitor.ORDERS]
3
[#3] Received on [ORDERS.processed]: 'order 3'
4
[#4] Received on [ORDERS.processed]: 'order 4'
Copied!
Note the subject here of the received message is reported as ORDERS.processed this helps you distinguish what you're seeing in a Stream covering a wildcard, or multiple subjects, subject space.
This Consumer needs no ack, so any new message into the ORDERS system will show up here in real-time.
Last modified 6mo ago