public
Created — forked from technoweenie/pinger.rb

ZeroMQ pub/sub demo

  • Download Gist
pub.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# usage: ruby pub.rb CHAN USERNAME
#
# ruby pub.rb rubyonrails technoweenie
#
#
# binds a PUB socket to tcp://*:5555
 
require 'rubygems'
require 'zmq'
 
context = ZMQ::Context.new
chan = ARGV[0]
user = ARGV[1]
pub = context.socket ZMQ::PUB
pub.bind 'tcp://*:5555'
 
context = ZMQ::Context.new
chan = ARGV[0]
user = ARGV[1]
pub = context.socket ZMQ::PUB
pub.bind 'tcp://*:5555'
 
while msg = STDIN.gets
msg.strip!
pub.send "#{chan} #{user} #{msg}"
end
sub.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# usage: ruby sub.rb
#
# Connects a SUB socket to tcp://*:5555.
# Subscribes to rubyonrails and ruby-lang.
 
require 'rubygems'
require 'zmq'
 
context = ZMQ::Context.new
chans = %w(rubyonrails ruby-lang)
sub = context.socket ZMQ::SUB
 
sub.connect 'tcp://127.0.0.1:5555'
chans.each { |ch| sub.setsockopt ZMQ::SUBSCRIBE, ch }
 
while line = sub.recv
chan, user, msg = line.split ' ', 3
puts "##{chan} [#{user}]: #{msg}"
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.