Skip to content

Instantly share code, notes, and snippets.

@charbonnierg
Last active November 1, 2025 15:17
Show Gist options
  • Select an option

  • Save charbonnierg/250b96e2b207e2a03129b257ff0bf3d8 to your computer and use it in GitHub Desktop.

Select an option

Save charbonnierg/250b96e2b207e2a03129b257ff0bf3d8 to your computer and use it in GitHub Desktop.

Revisions

  1. charbonnierg revised this gist Dec 22, 2023. 1 changed file with 0 additions and 6 deletions.
    6 changes: 0 additions & 6 deletions nats-cli-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -126,9 +126,3 @@ nats reply service.requests "Message {{Count}} @ {{Time}}"
    ```bash
    nats reply service.requests --echo --sleep 10
    ```

    - To request a response from a server and show just the raw result:

    ```bash
    nats request destination.subject "hello world" -H "Content-type:text/plain" --raw
    ```
  2. charbonnierg revised this gist Dec 22, 2023. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions nats-cli-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,13 @@ Cheat sheets are available from the CLI directly:
    nats cheat
    ```

    Try out the following commands:

    ```bash
    nats cheat contexts
    nats cheat pub
    ```

    ## Contexts

    - Create a non-default context:
  3. charbonnierg revised this gist Dec 22, 2023. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions nats-cli-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    # NATS Cheatsheet

    Cheat sheets are available from the CLI directly:

    ```bash
    nats cheat
    ```

    ## Contexts

    - Create a non-default context:
  4. charbonnierg created this gist Dec 22, 2023.
    121 changes: 121 additions & 0 deletions nats-cli-cheatsheet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,121 @@
    # NATS Cheatsheet

    ## Contexts

    - Create a non-default context:

    ```bash
    nats context add development --server nats.dev.example.net:4222
    ```

    - Create a new default context:

    ```bash
    nats context add development --server nats.dev.example.net:4222 --select
    ```

    - Update (you can choose the editor of your choice)

    ```bash
    EDITOR=vim nats context edit development
    ```

    - List contexts

    ```
    nats context ls
    ```

    - View context

    ```bash
    nats context info development
    ```

    - View context in JSON format:

    ```bash
    nats context info development --json
    ```

    - Validate all connections are valid and that connections can be established:

    ```bash
    nats context validate --connect
    ```

    - Select a new default context:

    ```bash
    nats context select development
    ```
    ## Publish
    - Publish using the default context:
    ```bash
    nats pub subject body
    ```
    > Where `subject` is the message subject, and `body`` is the message body

    - Publish using a different context than the default context:

    ```bash
    nats pub --context development subject body
    ```

    - To publish 100 messages with a random body between 100 and 1000 characters:

    ```bash
    nats pub destination.subject "{{ Random 100 1000 }}" -H Count:{{ Count }} --count 100
    ```

    - To publish messages from STDIN:

    ```bash
    echo "hello world" | nats pub destination.subject
    ```

    ## Requests

    - To send a request and wait for response:

    ```bash
    nats req subject request
    ```

    > Where `subject` is the subject where request will be sent, and `request` is the request payload.

    - To request a response from a server and show just the raw result:

    ```bash
    nats request destination.subject "hello world" -H "Content-type:text/plain" --raw
    ```

    ## Replies

    - To set up a responder that runs an external command with the 3rd subject token as argument:

    ```bash
    nats reply "service.requests.>" --command "service.sh {{2}}"
    ```

    - To set up basic responder:

    ```bash
    nats reply service.requests "Message {{Count}} @ {{Time}}"
    ```

    - To setup an echo responder:

    ```bash
    nats reply service.requests --echo --sleep 10
    ```

    - To request a response from a server and show just the raw result:

    ```bash
    nats request destination.subject "hello world" -H "Content-type:text/plain" --raw
    ```