# NATS Fundamentals Workshop — Lab Guide (Part I)

A follow-along lab guide for the NATS Fundamentals workshop. Work through the parts in order.

---

## Part 1 — Install the NATS CLI

### Install manually

1. Download the binary for your OS: <https://github.com/nats-io/natscli/releases/tag/v0.4.0>
2. Extract it.
3. Put it in your `PATH`.
4. Verify:

```bash
nats --version
```

### Install the NATS CLI using cURL

**Linux**

```bash
curl -L https://github.com/nats-io/natscli/releases/download/v0.4.0/nats-0.4.0-linux-amd64.zip -o nats-cli.zip
unzip nats-cli.zip
sudo mv nats-0.4.0-linux-amd64/nats /usr/local/bin/
nats --version
```

**macOS (Apple chip)**

```bash
curl -L https://github.com/nats-io/natscli/releases/download/v0.4.0/nats-0.4.0-darwin-arm64.zip -o nats-cli.zip
unzip nats-cli.zip
sudo mv nats-0.4.0-darwin-arm64/nats /usr/local/bin/
nats --version
```

**Windows (PowerShell)**

```powershell
Invoke-WebRequest -Uri https://github.com/nats-io/natscli/releases/download/v0.4.0/nats-0.4.0-windows-amd64.zip -OutFile nats-cli.zip
Expand-Archive nats-cli.zip -DestinationPath nats-cli
move nats-cli\nats-0.4.0-windows-amd64\nats.exe C:\Windows\System32\
nats --version
```

---

## Part 2 — Run the NATS CLI

### Configure the NATS CLI

Create a context to connect to the remote NATS server:

```bash
nats context add rethink --server nats://52.53.177.42:4222 --select
```

Verify the context is created and selected:

```bash
nats context ls
```

Check the connection to the server:

```bash
nats server check connection
```

### Publish / subscribe

```bash
nats sub SUBJECT.NAME.STRING
nats pub SUBJECT.NAME.STRING "PAYLOAD"
```

### Publish / subscribe with wildcard

```bash
nats sub "SUBJECT.*.STRING"
nats pub SUBJECT.NAME.STRING "PAYLOAD"
```

### Fan out

```bash
nats sub SUBJECT.NAME.STRING
nats sub "SUBJECT.*.STRING"
nats sub "SUBJECT.NAME.*"
nats pub SUBJECT.NAME.STRING "PAYLOAD"
```

### Queue group

```bash
nats sub SUBJECT.NAME.STRING --queue UNIQUE_GROUP_NAME
nats sub SUBJECT.NAME.STRING --queue UNIQUE_GROUP_NAME
nats sub SUBJECT.NAME.STRING --queue UNIQUE_GROUP_NAME
nats pub SUBJECT.NAME.STRING "Payload"
```

### Request / reply with header

```bash
nats reply SUBJECT.NAME.STRING "Response message"
nats request SUBJECT.NAME.STRING "Request message" --header Key:Value
```

### NATS service

```bash
nats service serve rethink
nats request rethink.echo "Hello, NATS!"
```

### Create a stream

```bash
nats stream add STREAM_NAME --subjects SUBJECT.NAME.STRING --defaults
```

### Publish a message to the stream

```bash
nats pub SUBJECT.NAME.STRING "PAYLOAD"
nats stream info STREAM_NAME
```

### Create a consumer

```bash
nats consumer add STREAM_NAME CONSUMER_NAME --defaults
nats consumer info STREAM_NAME CONSUMER_NAME
```

### Consume the message

```bash
nats consumer next STREAM_NAME CONSUMER_NAME
nats stream info STREAM_NAME
```

---

## Part 3 — Install the NATS Server

### Install the server manually

1. Download: <https://github.com/nats-io/nats-server/releases/tag/v2.14.2>
2. Extract.
3. Put in `PATH`.
4. Verify:

```bash
nats-server --version
```

### Install the NATS Server

**Linux**

```bash
curl -L https://github.com/nats-io/nats-server/releases/download/v2.14.2/nats-server-v2.14.2-linux-amd64.tar.gz -o nats-server.tar.gz
tar -xzf nats-server.tar.gz
sudo mv nats-server-v2.14.2-linux-amd64/nats-server /usr/local/bin/
nats-server --version
```

**macOS (Apple chip)**

```bash
curl -L https://github.com/nats-io/nats-server/releases/download/v2.14.2/nats-server-v2.14.2-darwin-arm64.tar.gz -o nats-server.tar.gz
tar -xzf nats-server.tar.gz
sudo mv nats-server-v2.14.2-darwin-arm64/nats-server /usr/local/bin/
nats-server --version
```

**Windows (PowerShell)**

```powershell
Invoke-WebRequest -Uri https://github.com/nats-io/nats-server/releases/download/v2.14.2/nats-server-v2.14.2-windows-amd64.zip -OutFile nats-server.zip
Expand-Archive nats-server.zip -DestinationPath nats-server
move nats-server\nats-server-v2.14.2-windows-amd64\nats-server.exe C:\Windows\System32\
nats-server --version
```

**macOS (Intel chip)**

Change `arm64` to `amd64` in the URL:

```bash
curl -L https://github.com/nats-io/nats-server/releases/download/v2.14.2/nats-server-v2.14.2-darwin-amd64.tar.gz -o nats-server.tar.gz
```
