The normal way to use JetStream is through the NATS client libraries which expose a set of JetStream functions that you can use directly in your programs. But that is not the only way you can interact with the JetStream infrastructure programmatically. Just like core NATS has a wire protocol on top of TCP, the JetStream enabled nats-server(s) expose a set of Services over core NATS.
All of these subjects are found as constants in the NATS Server source, so for example the subject $JS.API.STREAM.LIST is represented by api.JSApiStreamList constant in the nats-server source. Tables below will reference these constants and payload related data structures.
Note that if the resources you're trying to access have a JetStream domain associated with them, then the subject prefix will be $JS.{domain}.API rather than $JS.API.
The APIs used for administrative tools all respond with standardised JSON and these include errors.
Here the responses include a type which can be used to find the JSON Schema for each response.
Non-admin APIs - like those for adding a message to the stream will respond with -ERR or +OK with an optional reason after.
All of the admin actions the nats CLI can do fall in the sections below. The API structure is kept in the api package in the jsm.go repository.
Subjects that end in T like api.JSApiConsumerCreateT are formats and would need to have the Stream Name and in some cases also the Consumer name interpolated into them. In this case t := fmt.Sprintf(api.JSApiConsumerCreateT, streamName) to get the final subject.
The command nats events will show you an audit log of all API access events which includes the full content of each admin request, use this to view the structure of messages the nats command sends.
The API uses JSON for inputs and outputs, all the responses are typed using a type field which indicates their Schema. A JSON Schema repository can be found in nats-io/jsm.go/schemas.
| Subject | Constant | Description | Request Payload | Response Payload |
|---|---|---|---|---|
$JS.API.INFO |
api.JSApiAccountInfo |
Retrieves stats and limits about your account | empty payload | api.JetStreamAccountStats |
| Subject | Constant | Description | Request Payload | Response Payload |
|---|---|---|---|---|
$JS.API.STREAM.LIST |
api.JSApiStreamList |
Paged list known Streams including all their current information | api.JSApiStreamListRequest |
api.JSApiStreamListResponse |
$JS.API.STREAM.NAMES |
api.JSApiStreamNames |
Paged list of Streams | api.JSApiStreamNamesRequest |
api.JSApiStreamNamesResponse |
$JS.API.STREAM.CREATE.* |
api.JSApiStreamCreateT |
Creates a new Stream | api.StreamConfig |
api.JSApiStreamCreateResponse |
$JS.API.STREAM.UPDATE.* |
api.JSApiStreamUpdateT |
Updates an existing Stream with new config | api.StreamConfig |
api.JSApiStreamUpdateResponse |
$JS.API.STREAM.INFO.* |
api.JSApiStreamInfoT |
Information about config and state of a Stream | empty payload, Stream name in subject | api.JSApiStreamInfoResponse |
$JS.API.STREAM.DELETE.* |
api.JSApiStreamDeleteT |
Deletes a Stream and all its data | empty payload, Stream name in subject | api.JSApiStreamDeleteResponse |
$JS.API.STREAM.PURGE.* |
api.JSApiStreamPurgeT |
Purges all of the data in a Stream, leaves the Stream | empty payload, Stream name in subject | api.JSApiStreamPurgeResponse |
$JS.API.STREAM.MSG.DELETE.* |
api.JSApiMsgDeleteT |
Deletes a specific message in the Stream by sequence, useful for GDPR compliance | api.JSApiMsgDeleteRequest |
api.JSApiMsgDeleteResponse |
$JS.API.STREAM.MSG.GET.* |
api.JSApiMsgGetT |
Retrieves a specific message from the stream | api.JSApiMsgGetRequest |
api.JSApiMsgGetResponse |
$JS.API.STREAM.SNAPSHOT.* |
api.JSApiStreamSnapshotT |
Initiates a streaming backup of a streams data | api.JSApiStreamSnapshotRequest |
api.JSApiStreamSnapshotResponse |
$JS.API.STREAM.RESTORE.* |
api.JSApiStreamRestoreT |
Initiates a streaming restore of a stream | {} |
api.JSApiStreamRestoreResponse |
| Subject | Constant | Description | Request Payload | Response Payload |
|---|---|---|---|---|
$JS.API.CONSUMER.CREATE.<stream> |
api.JSApiConsumerCreateT |
Create an ephemeral consumer | api.ConsumerConfig |
api.JSApiConsumerCreateResponse |
$JS.API.CONSUMER.DURABLE.CREATE.<stream>.<consumer> |
api.JSApiDurableCreateT |
Create a consumer | api.ConsumerConfig |
api.JSApiConsumerCreateResponse |
$JS.API.CONSUMER.CREATE.<stream>.<consumer>.<filter> |
api.JSApiConsumerCreateExT |
Create a consumer (server 2.9+) | api.CreateConsumerRequest |
api.JSApiConsumerCreateResponse |
$JS.API.CONSUMER.LIST.<stream> |
api.JSApiConsumerListT |
Paged list of known consumers including their current info for a given stream | api.JSApiConsumerListRequest |
api.JSApiConsumerListResponse |
$JS.API.CONSUMER.NAMES.<stream> |
api.JSApiConsumerNamesT |
Paged list of known consumer names for a given stream | api.JSApiConsumerNamesRequest |
api.JSApiConsumerNamesResponse |
$JS.API.CONSUMER.INFO.<stream>.<consumer> |
api.JSApiConsumerInfoT |
Information about a specific consumer by name | empty payload | api.JSApiConsumerInfoResponse |
$JS.API.CONSUMER.DELETE.<stream>.<consumer> |
api.JSApiConsumerDeleteT |
Deletes a Consumer | empty payload | api.JSApiConsumerDeleteResponse |
$JS.FC.<stream>.> |
N/A | Consumer to subscriber flow control replies for PUSH consumer. Also used for sourcing and mirroring, which are implemented as PUSH consumers. If this subject is not forwarded, the consumer my stall under high load. |
empty payload | N/A |
$JSC.R.<uid> |
N/A | Reply subject used by source and mirror consumer create request | Consumer info | N/A |
$JS.S.<uid> |
N/A | Default delivery subject for sourced streams. Can be overwritten by the deliver attribute in the source configuration. |
Message data | N/A |
$JS.M.<uid> |
N/A | Default delivery subject for mirroed streams. Can be overwritten by the deliver attribute in the source configuration. |
Message data | N/A |
$JS.ACK.<stream>.> |
N/A | Acknowledgments for PULL consumers. When this subject is not forwarded, PULL consumers in acknowledgment modes all or explicit will fail. |
empty payload | reply subject |
Sourcing and mirroring streams use 3 inbound and 2 outbound subjects to establish and control the data flow. When setting permissions or creating export/import agreements all 5 subjects may need to be considered.
Notes:
There are two variants to the consumer-create subject depending on the number of filters.
In some setup a domain prefix may be present e.g. $JS.<domain>.API.CONSUMER.CREATE.<stream>.>
| Subject | Direction | Description | Reply |
|---|---|---|---|
$JS.API.CONSUMER.CREATE.<stream>.> and/or $JS.API.CONSUMER.CREATE.<stream> |
outbound | Create an ephemeral consumer to deliver pending messages. Note that this subject may be prefixed with a JetStream domain $JS.<domain>.API.CONSUMER.CREATE.<stream>.<consumer>. The consumer create comes in 2 flavors depending on the number of filter subjects:* $JS.API.CONSUMER.CREATE.<stream> - When there is no filter or there are multiple filters. * $JS.API.CONSUMER.CREATE.<stream>.<consumer>.<filter subject> - When there is exactly one filter subject |
service request with $JSC.R.<uid> as reply subject |
$JS.FC.<stream>.> |
outbound | Flow control messages. Will on slow routes or when the target cannot keep up with the message flow. | service request with $JSC.R.<uid> as reply subject |
$JSC.R.<uid> |
inbound | Reply to consumer creation request | reply message to service request |
$JS.S.<uid> (source) OR $JS.M.<uid> (mirror) OR <custom deliver subject> |
inbound | Message data and heartbeats | message stream |
The stream from which data is sourced/mirrored MAY NOT be reachable. It may not have been created yet OR the route may be down. This does not prevent the source/mirror agreement from being created.
The target stream will try to create a consumer every 10s to 60s. (This value may change in the future or may be configurable). Note that delivery may therefore only resume after a short delay.
For active consumers heartbeats are sent at a rate of 1/s.
$JS.FC.<stream>.> - The flow control subject is NOT prefixed with a JetStream domain. This creates a limitation where identically named streams in different domains cannot be reliably sourced/mirrored into the same account. Please create unique stream names to avoid this limitation.When using the subjects-based ACL, please note the patterns in the subjects grouped by purpose below.
General information
Stream Admin
Consumer Admin
Consumer message flow
Optional Events and Advisories :
This design allows you to easily create ACL rules that limit users to a specific Stream or Consumer and to specific verbs for administration purposes. For ensuring only the receiver of a message can Ack it we have response permissions ensuring you can only Publish to Response subject for messages you received.
Messages that need acknowledgment will have a Reply subject set, something like $JS.ACK.ORDERS.test.1.2.2, this is the prefix defined in api.JetStreamAckPre followed by <stream>.<consumer>.<delivered count>.<stream sequence>.<consumer sequence>.<timestamp>.<pending messages>.
JetStream and the consumer (including sourced and mirrored streams) may exchange flow control messages. A message with the header: NATS/1.0 100 FlowControl Request must be replied to, otherwise the consumer may stall. The reply subjects looks like: $JS.FC.orders.6i5h0GiQ.ep3Y
In all of the Synadia maintained API's you can simply do msg.Respond(nil) (or language equivalent) which will send nil to the reply subject.
If you have a pull-based Consumer you can send a standard NATS Request to $JS.API.CONSUMER.MSG.NEXT.<stream>.<consumer>, here the format is defined in api.JetStreamRequestNextT and requires populating using fmt.Sprintf().
Here we ask for just 1 message - nats req only shows 1 - but you can fetch a batch of messages by varying the argument. This combines well with the AckAll Ack policy.
The above request for the next message will stay in the server for as long as the client is connected and future pulls from the same client will accumulate on the server, meaning if you ask for 1 message 100 times and 1000 messages arrive you'll get sent 100 messages not 1.
This is often not desired, pull consumers support a mode where a JSON document is sent describing the pull request.
This requests 10 messages and asks the server to keep this request for 7 seconds, this is useful when you poll the server frequently and do not want the pull requests to accumulate on the server. Set the expire time to now + your poll frequency.
Here we see a second format of the Pull request that will not store the request on the queue at all but when there are no messages to deliver will send a nil bytes message with a Status header of 404, this way you can know when you reached the end of the stream for example. A 409 is returned if the Consumer has reached MaxAckPending limits.
If you know the Stream sequence of a message, you can fetch it directly, this does not support acks. Do a Request() to $JS.API.STREAM.MSG.GET.ORDERS sending it the message sequence as payload. Here the prefix is defined in api.JetStreamMsgBySeqT which also requires populating using fmt.Sprintf().
The Subject shows where the message was received, Data is base64 encoded and Time is when it was received.
Samples are published to a specific subject per Consumer, something like $JS.EVENT.METRIC.CONSUMER_ACK.<stream>.<consumer> you can subscribe to that and get api.ConsumerAckMetric messages in JSON format. The prefix is defined in api.JetStreamMetricConsumerAckPre.