Running

This tutorial demonstrates NATS Streaming using example Go NATS Streaming clients.

Prerequisites

Setup

Download and install the NATS Streaming Server.

Clone the following repositories:

  • NATS Streaming Server: git clone https://github.com/nats-io/nats-streaming-server.git
  • NATS Streaming Client: git clone https://github.com/nats-io/stan.go.git

Start the NATS Streaming Server

Two options:

Run the binary that you downloaded, for example: $ ./nats-streaming-server

Or, run from source:

  1. cd $GOPATH/src/github.com/nats-io/nats-streaming-server
  2. go run nats-streaming-server.go

You should see the following, indicating that the NATS Streaming Server is running:

  1. go run nats-streaming-server.go

Example output

  1. [59232] 2019/05/22 14:24:54.426344 [INF] STREAM: Starting nats-streaming-server[test-cluster] version 0.14.2
  2. [59232] 2019/05/22 14:24:54.426423 [INF] STREAM: ServerID: 3fpvAuXHo3C66Rkd4rmfFX
  3. [59232] 2019/05/22 14:24:54.426440 [INF] STREAM: Go version: go1.11.10
  4. [59232] 2019/05/22 14:24:54.426442 [INF] STREAM: Git commit: [not set]
  5. [59232] 2019/05/22 14:24:54.426932 [INF] Starting nats-server version 1.4.1
  6. [59232] 2019/05/22 14:24:54.426937 [INF] Git commit [not set]
  7. [59232] 2019/05/22 14:24:54.427104 [INF] Listening for client connections on 0.0.0.0:4222
  8. [59232] 2019/05/22 14:24:54.427108 [INF] Server is ready
  9. [59232] 2019/05/22 14:24:54.457604 [INF] STREAM: Recovering the state...
  10. [59232] 2019/05/22 14:24:54.457614 [INF] STREAM: No recovered state
  11. [59232] 2019/05/22 14:24:54.711407 [INF] STREAM: Message store is MEMORY
  12. [59232] 2019/05/22 14:24:54.711465 [INF] STREAM: ---------- Store Limits ----------
  13. [59232] 2019/05/22 14:24:54.711471 [INF] STREAM: Channels: 100 *
  14. [59232] 2019/05/22 14:24:54.711474 [INF] STREAM: --------- Channels Limits --------
  15. [59232] 2019/05/22 14:24:54.711478 [INF] STREAM: Subscriptions: 1000 *
  16. [59232] 2019/05/22 14:24:54.711481 [INF] STREAM: Messages : 1000000 *
  17. [59232] 2019/05/22 14:24:54.711485 [INF] STREAM: Bytes : 976.56 MB *
  18. [59232] 2019/05/22 14:24:54.711488 [INF] STREAM: Age : unlimited *
  19. [59232] 2019/05/22 14:24:54.711492 [INF] STREAM: Inactivity : unlimited *
  20. [59232] 2019/05/22 14:24:54.711495 [INF] STREAM: ----------------------------------

Run the publisher client

Publish several messages. For each publication you should get a result.

  1. cd $GOPATH/src/github.com/nats-io/stan.go/examples/stan-pub
  2. go run main.go foo "msg one"
  3. go run main.go foo "msg two"
  4. go run main.go foo "msg three"

Run the subscriber client

Use the --all flag to receive all published messages.

  1. cd $GOPATH/src/github.com/nats-io/stan.go/examples/stan-sub
  2. go run main.go --all -c test-cluster -id myID foo

Example output

  1. Connected to nats://localhost:4222 clusterID: [test-cluster] clientID: [myID]
  2. subscribing with DeliverAllAvailable
  3. Listening on [foo], clientID=[myID], qgroup=[] durable=[]
  4. [#1] Received on [foo]: 'sequence:1 subject:"foo" data:"msg one" timestamp:1465962202884478817 '
  5. [#2] Received on [foo]: 'sequence:2 subject:"foo" data:"msg two" timestamp:1465962208545003897 '
  6. [#3] Received on [foo]: 'sequence:3 subject:"foo" data:"msg three" timestamp:1465962215567601196

Explore other subscription options

  1. --seq <seqno> Start at seqno
  2. --all Deliver all available messages
  3. --last Deliver starting with last published message
  4. --since <duration> Deliver messages in last interval (e.g. 1s, 1hr, https://golang.org/pkg/time/#ParseDuration)
  5. --durable <name> Durable subscriber name
  6. --unsubscribe Unsubscribe the durable on exit