EventBus

CircleCI

EventBus provides additional messaging patterns for EmpregoLigado applications. It exposes some methods for messaging-related features:

  • EventBus::Emitter.trigger(event)
  • EventBus::Listener.on(event_name)
  • Extending EventBus::Listeners::Base

Let's look at a diagram for EventBus:

EventsBus diagram

Installation

Add this line to your application's Gemfile:

gem 'event_bus_rb'

And then execute:

$ bundle

Or install it yourself as:

$ gem install event_bus_rb

And set env vars:

ENV['RABBIT_URL'] = 'amqp://guest:guest@localhost:5672'
ENV['RABBIT_EVENT_BUS_APP_NAME'] = 'EventBusExampleApp'
ENV['RABBIT_EVENT_BUS_VHOST'] = 'event_bus'
ENV['RABBIT_EVENT_BUS_TOPIC_NAME'] = 'event_bus'

Usage

Emitter

A application can trigger events based passing an EventBus::Event instance, so others systems that listens to these events based matching the routing key (used as event_name below) will receive the event payload content.

require 'event_bus_rb'

event_name = 'resource.origin.action'
body = { resource: 'full' }
event = EventBus::Event.new(event_name, body)

EventBus::Emitter.trigger(event)

EventBus::Config.broker.close_connection

See more details

Listeners

An application that uses EventBus to handle events should be treated as a daemon like any other. The Listener api handles the blocking looping system, so the client doesn`t need to think about those implementation details.

require 'event_bus_rb'

event_name = 'resource.origin.action'

puts 'Start receiving messages'

EventBus::Listener.on(event_name) do |event, _delivery_info|
  puts ""
  puts "  - Received a message from #{event.name}:"
  puts "     Message: #{event.body}"
  puts ""
end

puts  'Stop receiving messages'

EventBus::Config.broker.close_connection

See more details

Multiple events routing

If your application needs to handle with loads of events you can extends EventBus::Listeners::Base and bind events to methods.

A simplistic example can be written like so:

require 'event_bus_rb'

class CustomEventListener < EventBus::Listeners::Base
  bind :pay, 'resource.custom.pay'
  bind :receive, 'resource.custom.receive'

  def pay(event, delivery_info)
    puts "Paid #{event.body['amount']} for #{event.body['name']} ~> #{event.name}"

    channel.ack(delivery_info.delivery_tag, false)
  end

  def receive(event, delivery_info)
    puts "Received #{event.body['amount']} from #{event.body['name']} ~> #{event.name}"

    channel.ack(delivery_info.delivery_tag, false)
  end
end

See more details

You can also bind more than one routing keys to methods using * or #.

Using resource.*.pay, for example, you can bind routing keys prefixed by resource. and suffixed by .pay to a method:

# bind resource.everything.pay to pay method
bind :pay, 'resource.*.pay'

Using #, you can bind all routing keys to a method:

# bind everything to receive method
bind :receive, '#'

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

License

The gem is available as open source under the terms of the MIT License.