Skip to content

Instantly share code, notes, and snippets.

@freegenie
Created March 10, 2012 22:18
Show Gist options
  • Save freegenie/2013593 to your computer and use it in GitHub Desktop.
Save freegenie/2013593 to your computer and use it in GitHub Desktop.
EventMachine + ZeroMQ Pipeline Pattern
# vim: ft=ruby
source :rubygems
gem 'eventmachine'
gem 'em-zeromq', :path => '~/Work/em-zeromq'
gem 'sinatra'
gem 'zmq'
require 'rubygems'
require 'bundler/setup'
require 'eventmachine'
require 'em-zeromq'
class Puller < EM::ZeroMQ::Connection
def on_readable messages
puts messages.join("|")
end
end
WebAppPull = EM::ZeroMQ::Context.new
EM.run do
WebAppPull.socket(ZMQ::PULL, Puller) do |socket|
socket.set_hwm(1)
socket.bind("tcp://*:#{ENV['port']}")
end
end
require 'rubygems'
require 'zmq'
require 'sinatra'
count = 0
context = ZMQ::Context.new 1
sock = context.socket(ZMQ::PUSH)
sock.setsockopt(ZMQ::HWM, 1)
sock.connect('tcp://0.0.0.0:4444')
sock.connect('tcp://0.0.0.0:4445')
sock.connect('tcp://0.0.0.0:4446')
sleep 1
get '/' do
count = count.succ
message = "Hello World! #{count}"
puts sock.send(message, ZMQ::NOBLOCK)
message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment