· golang nsq

Getting Started With NSQ for Go

NSQ

I’ve been recently working on software application project at DigitasLBi where we adopted NSQ as part of our microservices architecture. With this post I’d like to share how you can get started with NSQ by sending and receiving a simple message by writing some Go code.

What is NSQ?

Per the official website, NSQ is a realtime distributed messaging platform designed to operate at scale, handling billions of messages per day.

It promotes distributed and decentralized topologies without single points of failure, enabling fault tolerance and high availability coupled with a reliable message delivery guarantee.

nsqd is the daemon that receives, queues, and delivers messages to clients.

With the chart above you can understand the basic premise of NSQ involves producers and consumers - within your code you create NSQ producers to push messages to NSQ that get queued up to be consumed by other programs within your application.

Installing NSQ (on Linux VM)

$ wget https://s3.amazonaws.com/bitly-downloads/nsq/nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz
$ tar nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz
$ sudo mv nsq-1.0.0-compat.linux-amd64.go1.8.tar.gz/bin/* /usr/local/bin

Launching NSQD

$ nsqlookupd & 
$ nsqd --lookupd-tcp-address=127.0.0.1:4160 &
$ nsqadmin --lookupd-http-address=127.0.0.1:4161 &

If done successfully you will be able to view a web UI that looks like this:

nsqadmin is a Web UI to view aggregated cluster stats in realtime and perform various administrative tasks.

Writing Your Go Program

To import the NSQ client library, use a go get command:

$ go get github.com/nsqio/go-nsq

Creating a Consumer

I like creating the consumer first so I can see the handler in action after pushing a message with a producer (see next section).

package main

import (
    "log"
    "sync"

    "github.com/nsqio/go-nsq"
)

func main() {
    wg := &sync.WaitGroup{}
    wg.Add(1)

    decodeConfig := nsq.NewConfig()
    c, err := nsq.NewConsumer("My_NSQ_Topic", "My_NSQ_Channel", decodeConfig)
    if err != nil {
        log.Panic("Could not create consumer")
    }
    //c.MaxInFlight defaults to 1

    c.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
        log.Println("NSQ message received:")
        log.Println(string(message.Body))
        return nil
    }))

    err = c.ConnectToNSQD("127.0.0.1:4150")
    if err != nil {
        log.Panic("Could not connect")
    }
    log.Println("Awaiting messages from NSQ topic \"My NSQ Topic\"...")
    wg.Wait()
}

Now run this consumer program:

$ go run consume.go

You’ll get this output:

2017/04/05 17:33:42 INF    1 [My_NSQ_Topic/My_NSQ_Channel] (127.0.0.1:4150) connecting to nsqd
2017/04/05 17:33:42 Awaiting messages from NSQ topic "My NSQ Topic"...

This should hang there waiting to receive a NSQ message from a topic you specify. Nothing will happen just yet since there aren’t any queued up messages for this particular topic. Leave this program running in a terminal window for now. In the next step we’ll push a message to it.

Creating a Producer

You can publish a message with a producer with some simple code like this:

package main

import (
  "log"

  "github.com/nsqio/go-nsq"
)

func main() {
    config := nsq.NewConfig()
    p, err := nsq.NewProducer("127.0.0.1:4150", config)
    if err != nil {
        log.Panic(err)
    }
    err = p.Publish("My_NSQ_Topic", []byte("sample NSQ message"))
    if err != nil {
        log.Panic(err)
    }
}

Now run this publisher program:

$ go run publish.go

In this terminal window, you’ll only see this message indicating your message was published to NSQ:

go run publish.go
2017/04/05 17:39:15 INF    1 (127.0.0.1:4150) connecting to nsqd

If you look at your consumer terminal window that you left running from the previous step, you’ll now see this additional output:

2017/04/05 17:33:42 Awaiting messages from NSQ topic "My NSQ Topic"...
2017/04/05 17:39:15 NSQ message received:
2017/04/05 17:39:15 sample NSQ message

Congrats - you just pushed and received your first NSQ message!

If you go back to your web UI console you’ll see your newly-created topic. If you drill into this topic, you can also see the channel that you consumed the message to, with the message counter at 1:

NSQ Topic


About Me

I'm a senior software/data engineer working in the marketing/advertising industry. I build backend applications and tie together disparate data sources to help industry-leading brands make smarter, data-driven decisions towards reaching their target audiences through media. LEARN MORE >>



comments powered by Disqus