Skip to content

Instantly share code, notes, and snippets.

@amclain
Created February 17, 2014 04:02
Show Gist options
  • Save amclain/9044527 to your computer and use it in GitHub Desktop.
Save amclain/9044527 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, '' #Subscribe to everything.
sub_sock.bind socket_path
message = ''
Timeout.timeout 5 do
sub_sock.recv_string message
end
puts "SUB Message: #{message}"
sub_sock.close
end
# Publisher
pub_sock = ctx.socket ZMQ::PUB
pub_sock.connect socket_path
sleep 0.1 # Let the subscriber spin up.
pub_sock.send_string 'Hello'
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