Skip to content

Instantly share code, notes, and snippets.

@tpitale
Created October 9, 2013 04:15
Show Gist options
  • Select an option

  • Save tpitale/6896127 to your computer and use it in GitHub Desktop.

Select an option

Save tpitale/6896127 to your computer and use it in GitHub Desktop.
Celluloid-zmq PubSub messing about.
#!/usr/bin/env ruby
gem 'celluloid-zmq', '0.15.0'
require 'celluloid'
require 'celluloid/zmq'
class Publisher
include Celluloid::ZMQ
def run
link = "tcp://127.0.0.1:5555"
s1 = PubSocket.new
s1.linger = 100
s1.bind(link)
topic = "animals.dog"
payload = "Animal crackers!"
10.times do |i|
# s1.identity = "publisher-A"
puts "sending #{payload} to #{topic}"
# use the new multi-part messaging support to
# automatically separate the topic from the body
s1.write(topic, payload) #, s1.identity)
sleep 1
end
end
end
class Subscriber
include Celluloid::ZMQ
def run
link = "tcp://127.0.0.1:5555"
s2 = SubSocket.new
s3 = SubSocket.new
s4 = SubSocket.new
s2.subscribe('') # receive all
s3.subscribe('animals') # receive any starting with this string
s4.subscribe('animals.dog')
s2.connect(link)
s3.connect(link)
s4.connect(link)
puts "Waiting to read s2"
topic = ''
s2.read(topic)
body = ''
s2.read(body) if s2.more_parts?
identity = ''
# s2.read(identity) if s2.more_parts?
puts "s2 received topic [#{topic}], body [#{body}], identity [#{identity}]"
puts "Waiting to read s3"
topic = ''
s3.read(topic)
body = ''
s3.read(body) if s3.more_parts?
puts "s3 received topic [#{topic}], body [#{body}]"
puts "Waiting to read s3"
topic = ''
s4.read(topic)
body = ''
s4.read(body) if s4.more_parts?
puts "s4 received topic [#{topic}], body [#{body}]"
end
end
if __FILE__ == $0
Publisher.new.async.run
Subscriber.new.async.run
# trap("INT") {exit 0}
# trap("TERM") {exit 0}
sleep
end
@tpitale

tpitale commented Oct 9, 2013

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment