Streams

    You can get prompted interactively for missing information as above, or do it all on one command. Pressing ? in the CLI will help you map prompts to CLI options:

    1. nats str add ORDERS --subjects "ORDERS.*" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1 --discard old --dupe-window="0s" --replicas 1

    Additionally one can store the configuration in a JSON file, the format of this is the same as $ nats str info ORDERS -j | jq .config:

    1. $ nats str add ORDERS --config orders.json

    Listing

    We can confirm our Stream was created:

    1. $ nats str ls
    2. Streams:
    3. ORDERS

    Querying

    Information about the configuration of the Stream can be seen, and if you did not specify the Stream like below, it will prompt you based on all known ones:

    1. $ nats str info ORDERS
    2. Information for Stream ORDERS created 2021-02-27T16:49:36-07:00
    3. Configuration:
    4. Subjects: ORDERS.*
    5. Acknowledgements: true
    6. Retention: File - Limits
    7. Replicas: 1
    8. Discard Policy: Old
    9. Duplicate Window: 2m0s
    10. Maximum Messages: unlimited
    11. Maximum Bytes: unlimited
    12. Maximum Age: 1y0d0h0m0s
    13. Maximum Message Size: unlimited
    14. Maximum Consumers: unlimited
    15. State:
    16. Bytes: 0 B
    17. FirstSeq: 0
    18. LastSeq: 0
    19. Active Consumers: 0

    Most commands that show data as above support -j to show the results as JSON:

    A stream can be copied into another, which also allows the configuration of the new one to be adjusted via CLI flags:

    1. $ nats str cp ORDERS ARCHIVE --subjects "ORDERS_ARCVHIVE.*" --max-age 2y
    2. Stream ORDERS was created
    3. Information for Stream ORDERS created 2021-02-27T16:52:46-07:00
    4. Configuration:
    5. Subjects: ORDERS_ARCHIVE.*
    6. Acknowledgements: true
    7. Retention: File - Limits
    8. Replicas: 1
    9. Discard Policy: Old
    10. Duplicate Window: 2m0s
    11. Maximum Messages: unlimited
    12. Maximum Bytes: unlimited
    13. Maximum Age: 2y0d0h0m0s
    14. Maximum Message Size: unlimited
    15. Maximum Consumers: unlimited
    16. State:
    17. Messages: 0
    18. Bytes: 0 B
    19. FirstSeq: 0
    20. LastSeq: 0
    21. Active Consumers: 0

    Editing

    A stream configuration can be edited, which allows the configuration to be adjusted via CLI flags. Here I have an incorrectly created ORDERS stream that I fix:

    1. $ nats str info ORDERS -j | jq .config.subjects
    2. [
    3. "ORDERS.new"
    4. ]
    5. $ nats str edit ORDERS --subjects "ORDERS.*"
    6. Stream ORDERS was updated
    7. Configuration:
    8. Subjects: ORDERS.*
    9. ....

    Additionally, one can store the configuration in a JSON file, the format of this is the same as $ nats str info ORDERS -j | jq .config:

    1. $ nats str edit ORDERS --config orders.json

    Publishing Into a Stream

    Now let’s add some messages to our Stream. You can use nats pub to add messages, pass the --wait flag to see the publish ack being returned.

    You can publish without waiting for acknowledgement:

    1. $ nats pub ORDERS.scratch hello
    2. Published [sub1] : 'hello'

    Keep checking the status of the Stream while doing this and you’ll see its stored messages increase.

    1. $ nats str info ORDERS
    2. Information for Stream ORDERS
    3. ...
    4. Statistics:
    5. Messages: 3
    6. Bytes: 147 B
    7. FirstSeq: 1
    8. LastSeq: 3
    9. Active Consumers: 0

    After putting some throwaway data into the Stream, we can purge all the data out - while keeping the Stream active:

    To delete all data in a stream use purge:

    1. $ nats str purge ORDERS -f
    2. ...
    3. State:
    4. Messages: 0
    5. Bytes: 0 B
    6. FirstSeq: 1,000,001
    7. LastSeq: 1,000,000
    8. Active Consumers: 0

    Deleting A Message

    A single message can be securely removed from the stream:

    1. $ nats str rmm ORDERS 1 -f

    Deleting Sets

    Finally, for demonstration purposes, you can also delete the whole Stream and recreate it so then we’re ready for creating the Consumers:

    1. $ nats str rm ORDERS -f
    2. $ nats str add ORDERS --subjects "ORDERS.*" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1 --discard old --dupe-window="0s" --replicas 1