Define and send messages with Ruby to a variety of message brokers. The only currently supported message broker is RabbitMQ
This is a library that pairs nicely with Emque Consuming, a framework for consuming and routing messages to your code.
Add these lines to your application's Gemfile, depending on your message broker:
# for RabbitMQ, bunny is used
gem "emque-producing"
gem "bunny", "~> 1.7"
And then execute:
$ bundle
Or install it yourself as:
$ gem install emque-producing
# configure (likely in a Rails initializer)
require 'emque-producing'
Emque::Producing.configure do |c|
c.app_name = "app"
c.publishing_adapter = :rabbitmq
c.rabbitmq_options[:url] = "amqp://guest:guest@localhost:5672"
c.error_handlers << Proc.new {|ex,context|
# notify/log
}
end
# create a message class
class MyMessage
include Emque::Producing::Message
topic "topic1"
message_type "mymessage.new"
values do
attribute :first_property, Integer, :required => true
attribute :another_property, String, :required => true
end
end
# produce message
message = MyMessage.new({:first_property => 1, :another_property => "another"})
message.publish
# create a message class including changesets
class MyChangesetMessage
include Emque::Producing.message(:with_changeset => true)
topic "topic1"
message_type "mymessage.new"
# Need to override an attribute name in the changeset? Simply define
# the old name and new name here. This can be useful for ensuring
# messages are consistent across varying producers.
translate_changeset_attrs :old_attr_name => :new_attr_name
end
produced_message = TestMessageWithChangeset.new(
:updated => {:old_attr_name => "Event"},
:original => {:old_attr_name => "Game"}
)
json = produced_message.to_json
consumed_message = Oj.load(json)
expect(consumed_message["change_set"]).to eql(
{
"original"=>{"new_attr_name"=>"Game"},
"updated"=>{"new_attr_name"=>"Event"},
"delta"=>{
"new_attr_name"=>{
"original"=>"Game", "updated"=>"Event"
}
}
}
)
For a more thorough guide to creating new messages and/or message producing applications, please read the wiki entry
- Ruby 1.9.3 or higher
- RabbitMQ 3.x
- Bunny 1.4.x
To run tests...
bundle exec rspec
If you would like to test the gem as part of your client, you can update the
configuration option publish_messages
to false like so:
Emque::Producing.configure do |c|
c.publish_messages = false
...other options
end
This will prevent Emque from actually attempting to make the connection to your adapter which may be convenient in the case of CI environments.
FIRST: Read our style guides at https://github.com/teamsnap/guides/tree/master/ruby
- Fork it ( http://github.com/emque/emque-producing/fork )
- 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
Kafka would make for a good adapter to be added to emque-producing. Anyone wishing to submit a PR can use the RabbitMQ adapter as a model.