Home

Awesome

grenache-ruby-http

<img src="logo.png" width="15%" />

Grenache is a micro-framework for connecting microservices. Its simple and optimized for performance.

Internally, Grenache uses Distributed Hash Tables (DHT, known from Bittorrent) for Peer to Peer connections. You can find more details how Grenche internally works at the Main Project Homepage

Setup

Install

gem install grenache-ruby-http

Other Requirements

Install Grenache Grape: https://github.com/bitfinexcom/grenache-grape:

npm i -g grenache-grape
// Start 2 Grapes
grape --dp 20001 --aph 30001 --bn '127.0.0.1:20002'
grape --dp 20002 --aph 40001 --bn '127.0.0.1:20001'

Examples

RPC Server / Client

This RPC Server example announces a service called rpc_test on the overlay network. When a request from a client is received, it replies with world. It receives the payload hello from the client.

The client sends hello and receives world from the server.

Internally the DHT is asked for the IP of the server and then the request is done as Peer-to-Peer request via websockets.

Grape:

grape --dp 20001 --aph 30001 --bn '127.0.0.1:20002'
grape --dp 20002 --aph 40001 --bn '127.0.0.1:20001'

Server:

require "grenache-ruby-http"

EM.run do
  Signal.trap("INT")  { EventMachine.stop }
  Signal.trap("TERM") { EventMachine.stop }

  c = Grenache::Http.new(grape_address: "http://127.0.0.1:40001/")
  port = 5001

  c.listen("test_service", port) do |req|
      "hello #{req.payload}"
  end
end

Client:

require "grenache-ruby-http"

c = Grenache::Http.new(grape_address: "http://127.0.0.1:40001/")

resp, err = c.request("test_service", "world")

puts "response: #{resp}"

Code Server Code Client

API

Class: Grenache::Http

Grenache::Http.new(options)

client.request(name, payload, options)

Sends a single request to a RPC server/worker. Example.

client.put(data, options)

Puts a value into the DHT.

client.get(hash)

Retrieves a stored value from the DHT via a hash <String>.

client.listen(key, port, options)

Sets up a worker which connects to the DHT. Listens on the given port.

Example.