nmq

A lightweight, distributed and reliable message queue based on Redis

Get Started

Download

go get github.com/inuggets/nmq

Usage

import "github.com/inuggets/nmq"

// Broker
broker := nmq.NewRedisBroker(&nmq.RedisOptions{
    URL:      "localhost:6379",
    Password: "",
})

// Producer
producer := nmq.NewProducer(broker)

// Produce
err := producer.Produce("topic", "message")
if err != nil {
    log.Fatal(err)
}

// Consumer
consumer := nmq.NewConsumer(broker, "topic")
for {
    message, err := consumer.Consume()
    if err != nil {
        log.Fatal(err)
    }
    
    if m == nil {
        time.Sleep(3 * time.Second) // If no message, waiting for a while
        continue
    } else {
        // Do something
        ...
         
        // Ack (will remove message from queue permanently)
        err := consumer.Ack(m)
        if err != nil {
            log.Default().Println(err)
        }
        
        // Or back to queue (will be consumed again)
        err := consumer.Back(m)
        if err != nil {
            log.Default().Println(err)
        }
    }
}

License

nmq is under the MIT license. See the LICENSE for detail.

GitHub

View Github