Created
February 17, 2014 04:02
-
-
Save amclain/9044527 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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