Difficulty: Beginner
Estimated Time: 10 minutes

Build Your First NATS Publisher and Subscriber

NATS is a simple, secure, performant message bus. You can easily build an "at-most-once" or "fire-and-forget" message network with minimal latency.

  1. You'll create a secure connection.
  2. You'll create the subscriber.
  3. You'll create the publisher.
  4. You'll make the subscriber more robust with error handling.

Simple NATS Publish and Subscribe

Step 1 of 3

Create a secure connection to a NATS server

Get the go-nats library.

go get github.com/nats-io/go-nats

Now, let's start coding.

In nats-client.go:

package main

import (
    "fmt"
    "runtime"

    "github.com/nats-io/go-nats"  
)

func main() {
    nc, err := nats.Connect("tls://demo.nats.io:4443")
    defer nc.Close()

    if err != nil {
        fmt.Printf("Connection Error: %#v", err)
    }

    runtime.Goexit()
}

Build it and run it.

go build ./...

./tutorial

App