spox codes

Krakow: NSQ for Ruby

2014-03-08

Krakow is a Ruby client library for NSQ. The current 0.2.0 release pulls it out of its initial “alpha” phase, providing a feature complete client library. The road to finalizing a stable 1.0 release will consist of tight release iterations to fill out the tests, stablize the core, and add in some extra features like non-standard distributions and custom contexts for SSL.

Some cool things about this library include Celluloid backed Consumers and Producers, Celluloid::IO backed connections, with auto reconnection on connection failures. Full connection feature support which includes Snappy and Deflate compression as well as TLS.

All this talk. Who cares about talk, lets see how to use it! OK!

Lets start with a producer:

1
2
3
4
5
6
producer = Krakow::Producer.new(
:host => 'HOST',
:port => 'PORT',
:topic => 'target'
)
producer.write('KRAKOW!', 'KRAKOW!')

Awesome! Now lets consume those messages:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
consumer = Krakow::Consumer.new(
:nsqlookupd => 'http://HOST:PORT',
:topic => 'target',
:channel => 'ship'
)

consumer.queue.size
# => 2

2.times do
msg = consumer.queue.pop
puts "Received: #{msg}"
consumer.confirm(msg.message_id)
end

# => "Received: KRAKOW"
# => "Received: KRAKOW"

Pretty great!