Skip to content

Instantly share code, notes, and snippets.

@laser
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save laser/11303717 to your computer and use it in GitHub Desktop.

Select an option

Save laser/11303717 to your computer and use it in GitHub Desktop.
Using Redis as Message Broker for RPC
#!/usr/bin/env ruby
require 'redis'
require 'securerandom'
require 'json'
# connect to Redis
redis_client = Redis.connect(url: 'redis://localhost:6379')
# request id will be used as the name of the return queue
request = {
'id' => SecureRandom.hex,
'jsonrpc' => '2.0',
'method' => 'add',
'params' => [1, 5.1]
}
# insert our request at the head of the list
redis_client.lpush('calc', JSON.generate(request))
# pop last element off our list in a blocking fashion
channel, response = redis_client.brpop(request['id'], timeout=30)
# print it out to the console
puts "1+5.1=%.1f" % JSON.parse(response)['result'] # 1+5.1=6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment