NATS is a simple, secure and performant communications system for digital systems, services and devices. NATS is part of the Cloud Native Computing Foundation (CNCF). See more on Github NATS.
This implementation works in both modes: Standalone and Rails based applications.
require 'boom_nats'
# require_relative "consumers/...."
BoomNats.setup do
# basic NATS service
servers "nats://127.0.0.1:4222"
# Clustering NATS
servers ["nats://nats1:4222", "nats://nats2:4222"]
# with authentication
servers "nats://user:pass@localhost:4222"
# map NATS topics to Topic classes
draw_routes do
topic "topic-name", MyTopic
topic "namespace.topic", NamespacedTopic
topic "topic-queued", QueuedTopic, queue: 'queue-name'
# many topics queueds
in_queue "queue-name" do
topic "topic-queued", QueuedTopic
end
# auto unsubscribe after N messages
topic "auto-unsubscribe", TmpTopic, options: { max: 1 }
end
end
# start application
BoomNats.application.startTo facilitate learning, you can download the default repository containing a boilerplate project with (Rails + Nats)
git clone https://github.com/alboompro/boomnats-rails-example.gitSee details on BoomNats Rails Project
Add GEM to Gemfile
gem 'boom_nats', '~> 0.1.0'Using the generators to configure app, after create rails app
rails g boom_nats:install
# create files:
# app/consumers/hello_consumer.rb
# config/initializers/boom_nats.rbCreate consumers with generators too
rails g boom_nats:consumer PaymentGetter
# creates file: app/consumers/payment_getter_consumer.rbTo facilitate learning, you can download the default repository containing a boilerplate project with (Ruby, Nats and ActiveRecord)
git clone https://github.com/alboompro/boomnats-standard-example.gitSee details on BoomNats Standard Project
Download de GEM with the following command:
gem install boom_natsWith bundle, add gem to Gemfile
gem 'boom_nats', '~> 0.1.2'Create a simple file boom_nats.rb and put this content:
require "boom_nats"
class MyTopic < BoomNats::Topic
def consume
{
hello: params["name"]
}
rescue StandardError => e
puts e.message
end
end
BoomNats.setup do
servers "nats://127.0.0.1:4222"
draw_routes do
topic "hello", MyTopic
end
end
BoomNats.application.startruby boom_nats.rb