Post on 01-Sep-2014
description
ØMQ &SERVICES
ØMQ &SERVICES
part i: zeromq
wtf is zmq?
Zeromq is what bsd sockets may have looked like, if they were designed today.”
“
Zeromq is a communication library.
# C & C++void *context = zmq_init(1);
# rubycontext = ZMQ::Context.new(1)
# php$context = new ZMQContext(1);
# etc.
polyglot
atomic
finite&
messaging patterns
request/reply
client server
blah?
blah!
request/reply
server
clientserver
client
client
client
request/reply
server
clientserver
client
client
client
request/reply
server
clientserver
client
client
client
push/pull
pusher pullerblah!
push/pull
STEP 2
node
node
node
node
STEP 1 STEP 3
node
node
node
node
push/pull
STEP 2
node
node
node
node
STEP 1 STEP 3
node
node
node
push/pull
STEP 2
node
node
node
node
STEP 1 STEP 3
node
node
pub/sub
subscriber
subscriber
subscriber
subscriber
publisher
pub/sub
subscriber
subscriber
subscriber
subscriber
publisher
pub/sub
subscriber
subscriber
subscriber
subscriber
publisher
irl
client server
subscriber
worker worker subscriber
REQ
PUSHPUSH
PUSH PUB
PUB
node
node
node
node node
examples
a chat service
chat server
seankenneth mark
a chat service
chat server
seankenneth mark
pub pub pub
push
a chat service
chat server
seankenneth markhi!hi!hi!
a chat service
chat server
seankenneth markhi!hi!hi!
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needpub = context.socket(ZMQ::PUB)pull = context.socket(ZMQ::PULL)
# bind the socketspub.bind('tcp://*:1338')pull.bind('tcp://*:1337')
# wait for input, and forward to all subscriberswhile body = pull.recv payload = JSON.parse(body) pub.send "#{payload['user']}> #{payload['message'].cyan}"end
server.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needpub = context.socket(ZMQ::PUB)pull = context.socket(ZMQ::PULL)
# bind the socketspub.bind('tcp://*:1338')pull.bind('tcp://*:1337')
# wait for input, and forward to all subscriberswhile body = pull.recv payload = JSON.parse(body) pub.send "#{payload['user']}> #{payload['message'].cyan}"end
server.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needpub = context.socket(ZMQ::PUB)pull = context.socket(ZMQ::PULL)
# bind the socketspub.bind('tcp://*:1338')pull.bind('tcp://*:1337')
# wait for input, and forward to all subscriberswhile body = pull.recv payload = JSON.parse(body) pub.send "#{payload['user']}> #{payload['message'].cyan}"end
server.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needpub = context.socket(ZMQ::PUB)pull = context.socket(ZMQ::PULL)
# bind the socketspub.bind('tcp://*:1338')pull.bind('tcp://*:1337')
# wait for input, and forward to all subscriberswhile body = pull.recv payload = JSON.parse(body) pub.send "#{payload['user']}> #{payload['message'].cyan}"end
server.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needpub = context.socket(ZMQ::PUB)pull = context.socket(ZMQ::PULL)
# bind the socketspub.bind('tcp://*:1338')pull.bind('tcp://*:1337')
# wait for input, and forward to all subscriberswhile body = pull.recv payload = JSON.parse(body) pub.send "#{payload['user']}> #{payload['message'].cyan}"end
server.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needsub = context.socket(ZMQ::SUB)sub.setsockopt(ZMQ::SUBSCRIBE, '')push = context.socket(ZMQ::PUSH)
# bind the socketssub.connect("tcp://#{server}:1338")push.connect("tcp://#{server}:1337")
# wait for some inputwhile line = gets.chomp push.send(line) unless line == '' # dump buffered messages puts buffered_msg while (buffered_msg = sub.recv(ZMQ::NOBLOCK))end
client.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needsub = context.socket(ZMQ::SUB)sub.setsockopt(ZMQ::SUBSCRIBE, '')push = context.socket(ZMQ::PUSH)
# bind the socketssub.connect("tcp://#{server}:1338")push.connect("tcp://#{server}:1337")
# wait for some inputwhile line = gets.chomp push.send(line) unless line == '' # dump buffered messages puts buffered_msg while (buffered_msg = sub.recv(ZMQ::NOBLOCK))end
client.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needsub = context.socket(ZMQ::SUB)sub.setsockopt(ZMQ::SUBSCRIBE, '')push = context.socket(ZMQ::PUSH)
# bind the socketssub.connect("tcp://#{server}:1338")push.connect("tcp://#{server}:1337")
# wait for some inputwhile line = gets.chomp push.send(line) unless line == '' # dump buffered messages puts buffered_msg while (buffered_msg = sub.recv(ZMQ::NOBLOCK))end
client.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needsub = context.socket(ZMQ::SUB)sub.setsockopt(ZMQ::SUBSCRIBE, '')push = context.socket(ZMQ::PUSH)
# bind the socketssub.connect("tcp://#{server}:1338")push.connect("tcp://#{server}:1337")
# wait for some inputwhile line = gets.chomp push.send(line) unless line == '' # dump buffered messages puts buffered_msg while (buffered_msg = sub.recv(ZMQ::NOBLOCK))end
client.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needsub = context.socket(ZMQ::SUB)sub.setsockopt(ZMQ::SUBSCRIBE, '')push = context.socket(ZMQ::PUSH)
# bind the socketssub.connect("tcp://#{server}:1338")push.connect("tcp://#{server}:1337")
# wait for some inputwhile line = gets.chomp push.send(line) unless line == '' # dump buffered messages puts buffered_msg while (buffered_msg = sub.recv(ZMQ::NOBLOCK))end
client.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needsub = context.socket(ZMQ::SUB)sub.setsockopt(ZMQ::SUBSCRIBE, '')push = context.socket(ZMQ::PUSH)
# bind the socketssub.connect("tcp://#{server}:1338")push.connect("tcp://#{server}:1337")
# wait for some inputwhile line = gets.chomp push.send(line) unless line == '' # dump buffered messages puts buffered_msg while (buffered_msg = sub.recv(ZMQ::NOBLOCK))end
client.rb
a chat service# create the contextcontext = ZMQ::Context.new(1)
# create the two sockets we needsub = context.socket(ZMQ::SUB)sub.setsockopt(ZMQ::SUBSCRIBE, '')push = context.socket(ZMQ::PUSH)
# bind the socketssub.connect("tcp://#{server}:1338")push.connect("tcp://#{server}:1337")
# wait for some inputwhile line = gets.chomp push.send(line) unless line == '' # dump buffered messages puts buffered_msg while (buffered_msg = sub.recv(ZMQ::NOBLOCK))end
client.rb
demo$ git clone https://github.com/ \ ChartBoost/zmq-examples.git
$ bundle install
$ ruby chat/client.rb
demo$ git clone https://github.com/ \ ChartBoost/zmq-examples.git
$ bundle install
$ ruby chat/client.rb
thanks@KOB — KENNETH@CHARTBOOST.COM