GithubHelp home page GithubHelp logo

smart_que's Introduction

SmartQue

Welcome to SmartQue gem! This gem uses bunny library to connect with RabbitMq message broker to publish and consume messages with defined queues.

Installation

Add this line to your application's Gemfile:

gem 'smart-que', '~> 0.2.6'

And then execute:

$ bundle

Or install it yourself as:

$ gem install smart-que -v 0.2.6

Usage

  1. Setup RabbitMq
  2. Add smart-que gem to your Gemfile and perform bundle install
  3. Require smart_que in the application.rb file
  4. Create Publisher/Consumer classes & start publish/consume messages

SmartQue Publisher

SmartQue publisher is used to publish messages to the previously setup RabbitMq server. All messages are converted to JSON format before publishing to the queue. RabbitMq server details and queue lists can be configured as follows

# File: config/initializers/smart_que.rb
require 'smart_que'

SmartQue.configure do |f|
  f.host = ENV[:rabbit_mq][:host']
  f.port = ENV[:rabbit_mq][:port']
  f.vhost = ENV[:rabbit_mq][:vhost']
  f.username = ENV[:rabbit_mq][:username']
  f.password = ENV[:rabbit_mq][:password']
  f.logger = Rails.logger
  f.queues = [
      'default',
      'sms_otp',
      'fcm_push'
  ]
end

$publisher = SmartQue::Publisher.new

# Add this below mentioned line to your application.rb
# File : config/application.rb

require 'smart_que'

After initializing SmartQue publisher, it can be accessed anywhere in the rails application to publish messages to the RabbitMq server.

$publisher.publish('sms_otp', { number: '+919898123123', message: 'Test Message' })

SmartQue unicast

$publisher.unicast(queue_name, payload)

The unicast method will provide an option to publish message to any queue which are not predefined with initialization configuration. The queue_name parameter can be any queue name, which will be created and bound to direct exchange instantly if doesn't exist.

$publisher.unicast('sms_alert', { number: '+919898123123', message: 'Test Message' })

SmartQue multicast

$publisher.multicast(topic_name, payload)

The multicast method will provide an option to multicast message to queues which are not predefined with initialization configuration. The topic_name parameter can be any topic name and message will be consumable by any queues which subscribe to this topic name.

$publisher.multicast('weather', { message: 'Test Message' })

SmartQue broadcast

$publisher.broadcast(payload)

The broadcast method will provide an option to broadcast message to queues which are not predefined with initialization configuration. The broadcst message will be consumable by all queues which are bound to rabbitmq fanout exchange.

$publisher.broadcast({ message: 'Broadcast Test Message' })

SmartQue Consumer

The consumer class should be inherited from SmartQue::Consumer, and each consumer will be listening to a defined Queue. The queue name and run method should be defined properly in each consumer class.

class OtpSmsConsumer < SmartQue::Consumer
  QUEUE_NAME = 'sms.otp'

  def run(payload)
    # Payload: { number: '+919898123123', message: 'Test Message' }
    # Method implementation goes here
    puts payload
  end
end

Note that, queue name words are separated by . while defining it with consumer class. Consumer can be started by calling start method available for consumer instance as follows

c = OtpSmsConsumer.new
c.start

All consumer processes can be placed in a rake file, so that it can be started individually with rake tasks.

File : lib/tasks/consumer.rake

# Tasks which related to rabbitmq broker.
namespace :rabbitmq do

  desc "Run OTP sms worker"
  task :send_otp_sms => [:environment] do
    c = OtpSmsConsumer.new
    c.start
  end
end

# Task can be initiated by rake
RAILS_ENV=staging bundle exec rake rabbitmq:send_otp_sms

SmartQue Exceptions

SmartQue will not retry automatic connection recovery after rabbitmq connection failure. It raises SmartQue::ConnectionError when tcp connection failure happens. You can catch this exception to identify connection failures and can perform necessary actions upon it.

RabbitMq Web Interface

You can enable rabbitmq_management plugin so that rabbitmq server informations are viewed and managed via web console interface. Management plugin can be enabled by running this command and can be accessed through web browser

rabbitmq-plugins enable rabbitmq_management

More informations are available on RabbitMq management plugin documentations.

Development

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

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smart_que. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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

Code of Conduct

Everyone interacting in the SmartQue project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

smart_que's People

Contributors

ashiksp avatar

Watchers

 avatar  avatar

smart_que'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.