andrewvc/em-zeromq — Issue #12: Request-response pattern not working
Author: baronlinden
Created: 2012-02-10 21:04:02+00:00
State: open

I am trying to implement a request-response pattern, but I can't get the response socket to send a message back to the request socket in its handler. I have written some very simple code to test it:

em_req.rb

require 'em-zeromq'

client_id = ARGV[0] ? ARGV[0].to_i : 1
message = ARGV[1] || "Foo"

Thread.abort_on_exception = true

class ReqHandler
attr_reader :received

def on_readable(socket, messages)
messages.each do |m|
puts "Received message from server: #{m.copy_out_string}"
end
end
end

trap('INT') do
EM.stop
end

ctx = EM::ZeroMQ::Context.new(1)

EM.run do
conn = ctx.connect(ZMQ::REQ, 'tcp://127.0.0.1:9000', ReqHandler.new, identity: "client#{client_id}")
conn.socket.send_string(message)
end

em_rep.rb

require 'em-zeromq'

Thread.abort_on_exception = true

class ResponseHandler
attr_reader :received

def on_readable(socket, messages)
identity, delimeter, message = messages.map(&:copy_out_string)
puts "Received message from #{identity}: #{message}"

socket.send_msg("re: #{message}")
end
end

trap('INT') do
EM.stop
end

ctx = EM::ZeroMQ::Context.new(1)

EM.run do
socket = ctx.bind(ZMQ::ROUTER, 'tcp://127.0.0.1:9000', ResponseHandler.new)
end

I have written similar code using the push-pull pattern and got that to work, but for request-response all I get is the response code printing "Received message from client1: Foo" but the reply never reaches the request code. I suspect it has to do with writing to the socket in the response code's handler, because the same thing happens when I use a request-router pattern. The only time it works is when I send a message from the server without sending a message from the client first using push-pull.

Any ideas about what might be causing this? I know andrewvc isn't maintaining this gem anymore, but I thought I would post this issue anyway in the hopes of other developers with similar experiences seeing this.

I am using em-zeromq 0.2.2 on Ruby 1.9.2p290.