Last active
August 29, 2015 14:00
-
-
Save laser/11303717 to your computer and use it in GitHub Desktop.
Using Redis as Message Broker for RPC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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