PipeRpc

PipeRpc was designed so parent und child processes can call each other's methods. It uses a protocol similar to JSON-RPC but serializes the payloads with msgpack.

Installation

Add this line to your application's Gemfile:

gem 'pipe_rpc'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pipe_rpc

Usage

require 'pipe_rpc'

request_read, request_write = IO.pipe
response_read, response_write = IO.pipe

pid = fork do
  request_write.close
  response_read.close

  class Server
    def add(a, b)
      a + b
    end
  end

  hub = PipeRpc::Hub.new(input: request_read, output: response_write)
  hub.add_server(default: Server.new)

  loop do
    hub.handle_message # blocks until message available
  end
end

begin
  request_read.close
  response_write.close

  hub = PipeRpc::Hub.new(output: request_write, input: response_read)
  client = hub.client_for(:default)

  puts client.add(3, 5)
ensure
  Process.kill 9, pid
  Process.wait pid
end