My setup is as follows: 1 Microservice that receives a request and writes a message in the queue(nsq) and a second microservice that must read the messages in the queue and do something based on them.
I am new to the nsq concept in ruby on rails. I have installed the nsq from here: http://nsq.io/overview/quick_start.html. I have also used this gem to facilitate pushing messages: https://github.com/wistia/nsq-ruby.
I have been able to queue the message. This part was easy enough.
Question:
How do I always listen in the 2nd microservice to figure out when something was pushed so I can consume it?
This is how I push messages:
require 'nsq'
class NsqService
attr_accessor :producer, :topic
def initialize(topic)
@topic = topic
@producer = producer
end
def publish(message)
producer.write(message)
end
private
def producer(nsqd='127.0.0.1:4150')
Nsq::Producer.new(
nsqd: nsqd,
topic: @topic
)
end
end
Example code on the nsq-ruby gem give the following code example:
require 'nsq'
consumer = Nsq::Consumer.new(
nsqlookupd: '127.0.0.1:4161',
topic: 'some-topic',
channel: 'some-channel'
)
# Pop a message off the queue
msg = consumer.pop
puts msg.body
msg.finish
# Close the connections
consumer.terminate
You could wrap this in a class for your service etc. You'll likely need to run some kind of middleware or separate processes to handle these connections. If there are parts of your rails code which need to interface with NSQ. While I have not used NSQ, I've used Sidekiq for background jobs and running async processes which has good instructions and examples of how to configure those middleware. For more suggestions and help, you might try contacting some of the maintainers of the ruby gem. I'm sure they can point you in the right direction.