|
| 1 | +--- |
| 2 | +meta: |
| 3 | + title: Using the NATS CLI |
| 4 | + description: This page explains how to use the NATS CLI with Scaleway NATS |
| 5 | +content: |
| 6 | + h1: Using the NATS CLI |
| 7 | + paragraph: This page explains how to use the NATS CLI with Scaleway NATS |
| 8 | +tags: messaging nats cli |
| 9 | +categories: |
| 10 | + - messaging |
| 11 | +dates: |
| 12 | + validation: 2024-10-18 |
| 13 | + posted: 2023-01-20 |
| 14 | +--- |
| 15 | + |
| 16 | +The NATS CLI (`nats`) is the official NATS tool for managing your NATS resources. It allows you to simply create and manage your streams, consumers and more. |
| 17 | + |
| 18 | +Check out the official [NATS CLI documentation](https://docs.nats.io/using-nats/nats-tools/nats_cli/) for installation instructions, examples and more. |
| 19 | + |
| 20 | +<Message type="tip"> |
| 21 | + You can also configure Scaleway NATS with the Terraform/OpenTofu NATS Jetstream provider using our [dedicated tutorial](https://registry.terraform.io/providers/scaleway/scaleway/latest/docs/guides/mnq_with_nats_terraform_provider). |
| 22 | +</Message> |
| 23 | + |
| 24 | +This page shows you how to get started with some basic actions via the NATS CLI. |
| 25 | + |
| 26 | +<Macro id="requirements" /> |
| 27 | + |
| 28 | +- A Scaleway account logged into the [console](https://console.scaleway.com) |
| 29 | +- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization |
| 30 | +- A Scaleway [NATS account](/nats/how-to/create-nats-account) |
| 31 | +- NATS [credentials](/nats/how-to/create-credentials/) downloaded to your machine |
| 32 | + |
| 33 | +## Installing the NATS CLI |
| 34 | + |
| 35 | +Follow instructions from the [official NATS documentation](https://docs.nats.io/using-nats/nats-tools/nats_cli#installing-nats). The installation process may differ depending on your OS. |
| 36 | + |
| 37 | +## Define a context |
| 38 | + |
| 39 | +To simplify your interactions with NATS hosted on Scaleway, we recommend that you use [contexts](https://docs.nats.io/using-nats/nats-tools/nats_cli#nats-contexts). A context is a named configuration that stores the settings (such as credentials, URLs and certificates) required to connect to NATS. By creating a context, you won't need to specify your server and credentials with each new request. |
| 40 | + |
| 41 | +### Create a context |
| 42 | + |
| 43 | +The example below creates a context named `scaleway`. |
| 44 | +- Replace `{Scaleway NATS URL}` with the URL of your NATS account (find this in the console on your NATS account **Overview** tab). |
| 45 | +- Replace `{path to your creds file}` with the path to the location where you [downloaded your `.creds` file](/nats/how-to/create-credentials/). |
| 46 | + |
| 47 | +```bash |
| 48 | +nats context save scaleway --server={Scaleway NATS URL} --creds={path to your creds file} |
| 49 | +``` |
| 50 | + |
| 51 | +### Use the saved context |
| 52 | + |
| 53 | +Enter the following command to select the `scaleway` context for use: |
| 54 | + |
| 55 | +``` |
| 56 | +nats context select scaleway |
| 57 | +``` |
| 58 | + |
| 59 | +## Create a stream |
| 60 | + |
| 61 | +To create a [stream](/nats/concepts/#stream), use the command `nats stream add` and follow the CLI guidelines. |
| 62 | + |
| 63 | +<Message type="important"> |
| 64 | + Bear in mind that: |
| 65 | + - Scaleway NATS does not support in-memory streams: choose `File` as storage backend. |
| 66 | + - Some [system limits](/nats/reference-content/limitations/) may apply by default. |
| 67 | + - If you choose a `Retention Policy` other than `Work Queue` you will be billed for the messages stored and retained. |
| 68 | + - Choosing three replicas has an impact on: |
| 69 | + - The stream storage limit (as data is replicated 3 times) |
| 70 | + - The volume of billed messages |
| 71 | +</Message> |
| 72 | + |
| 73 | +You can connect to your stream using code, developer tools or the NATS CLI (for testing purposes only). |
| 74 | + |
| 75 | +## NATS cheat sheet |
| 76 | + |
| 77 | +Use the `nats cheat` command to get a list of all possible commands. |
| 78 | + |
| 79 | +Below we provide a summary of some useful commands. |
| 80 | + |
| 81 | +### General |
| 82 | + |
| 83 | +| Action | Command | |
| 84 | +| ------------ | ------------------- | |
| 85 | +| Show a specific section of cheats | `nats cheat pub` | |
| 86 | +| List available sections | `nats cheat --sections` | |
| 87 | + |
| 88 | + |
| 89 | +### Messaging |
| 90 | + |
| 91 | +| Action | Command | |
| 92 | +| ------------ | ------------------- | |
| 93 | +| Publish message from STDIN | `echo "hello world" \| nats pub destination.subject` | |
| 94 | +| Publish 100 messages with a random body of 100 - 1000 characters | `nats cheat --sections``nats pub destination.subject "{{ Random 100 1000 }}" -H Count:{{ Count }} --count 100` | |
| 95 | +| Publish message from STDINReceive new messages received with the subject ORDERS.new | `nats sub ORDERS.new` | |
| 96 | +| Subscribe to messages, on subject source.subject | `nats sub source.subject` | |
| 97 | + |
| 98 | + |
| 99 | +### Queuing/Streaming |
| 100 | + |
| 101 | +| Action | Command | |
| 102 | +| ------------ | ------------------- | |
| 103 | +| Adding a stream | `nats stream add` | |
| 104 | +| Viewing a stream | `nats stream info STREAMNAME` | |
| 105 | +| Removing a stream | `nats stream rm STREAMNAME` | |
| 106 | +| Show a list of streams | `nats stream list` | |
| 107 | +| Get message `12345` in stream `ORDERS` | `nats stream get ORDERS 12345` | |
| 108 | +| Delete message `12345` in stream `ORDERS` | `nats stream rmm ORDERS 12345` | |
| 109 | +| Purge messages from stream `ORDERS` | `nats stream purge ORDERS` | |
| 110 | +| Mark a stream `ORDERS` as read only | `nats stream seal ORDERS` | |
| 111 | +| Add a consumer | `nats consumer add` | |
| 112 | +| View a consumer `NEW` of stream `ORDERS` | `nats consumer info ORDERS NEW` | |
| 113 | +| Remove consumer `NEW` from stream `ORDERS`| `nats consumer rm ORDERS NEW` | |
| 114 | +| Get messages from consumer `NEW` of stream `ORDERS` | `nats consumer next ORDERS NEW --ack --count=10 `, `nats consumer next ORDERS NEW --no-ack`, `nats consumer sub ORDERS NEW --ack` | |
0 commit comments