Skip to content

Instantly share code, notes, and snippets.

@qmx
Created May 28, 2012 04:30
Show Gist options
  • Save qmx/2817229 to your computer and use it in GitHub Desktop.
Save qmx/2817229 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'ffi-rzmq'
# Prepare our context and socket
context = ZMQ::Context.new(1)
t2=Thread.new do
begin
publisher = context.socket(ZMQ::PUSH)
publisher.connect("tcp://localhost:5558")
(1..10).each {|n|
publisher.send_string "MARIO", ZMQ::SNDMORE
publisher.send_string "It's a me! #{n}"
sleep rand(5)/10.0
}
rescue
puts $!
puts $@
end
end
t1=Thread.new do
publisher = context.socket(ZMQ::PUSH)
publisher.connect("tcp://localhost:5558")
(1..10).each {|n|
publisher.send_string "PEACH", ZMQ::SNDMORE
publisher.send_string "Peachy!"
sleep rand(5)/10.0
}
end
t1.join
t2.join
publisher = context.socket(ZMQ::PUSH)
publisher.connect("tcp://localhost:5558")
publisher.send_string "DIE"
require 'rubygems'
require 'ffi-rzmq'
# Prepare our context and socket
context = ZMQ::Context.new(1)
subscriber = context.socket(ZMQ::SUB)
subscriber.connect("tcp://localhost:5559")
subscriber.setsockopt(ZMQ::SUBSCRIBE,"MARIO")
subscriber.setsockopt(ZMQ::SUBSCRIBE,"DIE")
trap("SIGINT") {exit!}
loop do
subscriber.recv_string(topic='')
subscriber.recv_string(body='') if subscriber.more_parts?
puts "Got topic #{topic} body #{body}"
exit if topic=="DIE"
end
@bnagy
Copy link

bnagy commented May 28, 2012

Simpler

require 'ffi-rzmq'

Prepare our context and socket

context = ZMQ::Context.new(1)
subscriber = context.socket(ZMQ::SUB)
1000.times do
subscriber.setsockopt(ZMQ::SUBSCRIBE,"MARIO")
end

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