Git Product home page Git Product logo

event_bus_rb's Introduction

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.

event_bus_rb's People

Contributors

aalvesjr avatar evicacoelho avatar hernandesbsousa avatar lucasmarqs avatar pedrokunz avatar robertamadge avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

event_bus_rb's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.