Skip to content

Instantly share code, notes, and snippets.

@amclain
Created February 17, 2014 03:42
Show Gist options
  • Save amclain/9044365 to your computer and use it in GitHub Desktop.
Save amclain/9044365 to your computer and use it in GitHub Desktop.
require 'ffi-rzmq'
require 'timeout'
socket_path = 'tcp://127.0.0.1:5050'
# socket_path = 'ipc://zmq_pub_sub.ipc'
ctx = ZMQ::Context.create
# Subscriber
sub_thread = Thread.new do
sub_sock = ctx.socket ZMQ::SUB
sub_sock.setsockopt ZMQ::SUBSCRIBE, '/test'
sub_sock.bind socket_path
topic = ''
body = ''
Timeout.timeout(5) do
sub_sock.recv_string topic
sub_sock.recv_string body if sub_sock.more_parts?
end
puts "SUB Topic: #{topic}\n#{body}"
sub_sock.close
end
# Publisher
pub_sock = ctx.socket ZMQ::PUB
pub_sock.connect socket_path
sleep 0.1 # Let the subscriber spin up.
1.times do
pub_sock.send_string '/test', ZMQ::SNDMORE
pub_sock.send_string 'Hello'
sleep 1
end
pub_sock.close
# Cleanup
sub_thread.join
ctx.terminate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment