Skip to content

nixtrace/rcom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rcom

Redis inter-service messaging: request-response, publish-subscribe and task queues. A thin, minimal layer on top of Redis-rb.

Installation.

  • Install a local Redis server and start it:
# OSX example
$ brew install redis
$ redis-server
  • Add rcom to your Gemfile:
gem 'rcom'

Usage.

Rcom supports the request-response, publish-subscribe and task queue patterns for inter-service messaging. Publishers are non-blocking, subscribers/consumers are blocking and should be run as independent processes. Processes communicate using MessagePack internally.

Node.

A node represents a Redis connection to a server address specified with an ENV variable.

#.env
LOCAL=redis://localhost
node = Rcom::Node.new('local').connect

Topics.

One service might need to update many different services about an event, following the publish-subscribe pattern. You can publish and subscribe to topics on a node, specifying a channel.

  • Publisher.
message = {
  id: 1,
  key: 'xxxccc'
}

node = Rcom::Node.new('local').connect
topic = Rcom::Topic.new(node: node, channel: 'users')

topic.publish(message)
  • Subscriber.
node = Rcom::Node.new('local').connect
topic = Rcom::Topic.new(node: node, channel: 'users')

topic.subscribe do |message|
  p message
end

Tasks.

A service might need to push consuming tasks into a queue and forget about them. Tasks will be processed by consumers listening to the queue.

  • Publisher.
message = {
  id: 1,
  key: 'xxxccc'
}

node = Rcom::Node.new('local').connect
messages = Rcom::Task.new(node: node, channel: 'messages')

messages.publish(message)
  • Consumer.
node = Rcom::Node.new('local').connect
messages = Rcom::Task.new(node: node, channel: 'messages')

messages.subscribe do |message|
  sleep 1
  p message
end

RPC, requests and responses.

In some cases services need real-time informations from other services that can't be asynchronously processed. A service can request informations on a channel. The other service listening on the same channel will reply to the request.

  • Request.
node = Rcom::Node.new('local').connect
service = Rcom::Request.new(node: node, channel: 'auth')

service.get_key(user: 1)
  • Response.
node = Rcom::Node.new('local').connect

class Server
  def get_key(params)
    user = params[:user]
    return nil unless user == 1
    return 'xxxccc'
  end
end

service = Rcom::Response.new(
  node: node,
  channel: 'auth',
  server: Server.new
)

service.serve

Test.

  • Be sure you have a local Redis server running.

  • run tests with:

bundle exec rake test:spec

Contributing.

  1. Fork it ( https://github.com/badshark/rcom/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License.

MIT

About

Redis inter-service messaging.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages