Up and Running with RabbitMQ – A Message Broker

Hi, today we are going to learn about RabbitMQ, a message broker software.

What is Message Borker?

Message broker is an intermediary program module that translates a message from the formal messaging protocol of the sender to the formal messaging protocol of the receive

– source wikipedia

It act as an intermediate between sender and receiver.

What is RabbitMQ?

RabbitMQ is an open source message broker software which implements Advanced Message Queuing Protocol (AMQP). It can act as pub-sub, routing and queuing system.

RabbitMQ Message Flow

rmq-message-flow

Producer: Creates a message to send it to exchange

Broker: It consist of three parts

  • Exchange: Receive published messages and send it to appropriate queue on the basis of routing key.
  • Binding: It is a link between  queue and exchange
  • Routing key: It helps in selecting queue i.e. which message is will be send to which queue.

Consumer: Consumer consumes the message from the queue they are bind.

Installation

Development Environment

  • Ubuntu 14.04 LTS
  • Python 2.7
  • PIP (python package manager)

Follow the steps to start using RabbitMQ

# Create Directory and enter it

# Create and activate virtual environment

Don’t know how to install virtual environment, check the official doc at docs.python-guide.org.

Download requirements.txt file to install all packages in your virtual environment using following command or you can go step by step with the tutorial.

# Install RabbitMQ

Once you install rabbitmq server, it will starts running automatically

# Enable RabbiMQ management plugin

It will give you a WebUI to manage and monitor rabbitmq. By default it runs on port 15672, you can access it in your browser by going to url http://127.0.0.1:15672/. Default username and password for WebUI is guest and guest respectively.

# Install pika

Pika is a pure-Python implementation of the AMQP 0-9-1 protocol that tries to stay fairly independent of the underlying network support library.

– source python.org

# Create Producer (message.py)

Producer sends a string message to “messages” queue.

# Create Consumer (worker.py)

Consumer will receive message and process it. Here, consume is listening on “messages” queue.

# Run Producer and Consumer

Run producer and consumer in two different terminal in order to see sending and receiving of messages simultaneously.

sending-message-and-message-in-queue

When we run message.py, message got inserted into queue but since there is no consumer listening on that queue therefor it shows one message is ready for publishing as “Ready” equals to 1.

Click on messages in above UI and we can get stats about individual queues. As you can see 1 message which we sent earlier is still in queue.

message-queue

# Start consumer

message-received-by-consumer

As soon as we start our consumer worker.py, it will receive message from queue and now you can see ready count is 0.

Sending Multiple Messages

Create a loop to send 10 bulk message in queue.

Now start two instances of your worker.py

multiple-worker

When you start two instances of worker.py, you can see message got evenly distributed on the basis of Round Robin Algorithm.Also, same message is not delivered to multiple workers.

It’s a simple example, now we send dict as message.

Create a new file dict-message.py

In order to read message now, we have to change our worker a little bit.

Create a new file dict-message-worker.py

dict-message-sending-receving

We use json to send and receive message. As you can see when we send dict as message in a new queue “dict_messages” , consumer listening to that queue start receiving message instantly.

Now you have fair amount of knowledge of RabbiMQ you can use it in any appropriate project like for send notification, storing logs, sending bulk emails and many more.

For further reading check rabbitmq official docs at rabbitmq.com.

That’s all for today, hope you enjoy and do try at home.

Download source code Up-and-Running-with-RabbitMQ-A-Message-Broker-tusharsharma.in.zip

 

Traits in PHP

Hi all, today we will learn about “traits in php”.  As we know, PHP is a single inheritance language i.e. a class defined in PHP can’t inherit more than one class. “Traits” are introduced to solve this problem.

traits-in-php

What are Traits?

“A Trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way.” source php.net

  • Trait is just like a helper class which group reusable code.
  • It helps to follow DRY (Don’t Repeat Yourself).
  • You cannot instantiate a Trait on its own.

Trait vs Interface

When we use Interface then the class which is implementing have to implement all the method but in Trait we can define many function and can use a few. In trait, we can give definitions of a function for ex. a function can perform some task and return value.

Example

Let’s create two files main.class.php and child.class.php where child class inherits main class.

Now we will create a trait file to use in child class, so let’s create a trait file as message.trait.php

Now let’s create an object of child class and call showMessage method and get output as follows

“Hi from parent class Main::showMessage”

Now, to use trait we have to include file in child and use keyword “use” and updated file look as follows.

Create object of child class and call method showMessage and you can see output as follows

“Hi friends you are learning traits in php” which comes from showMessage function from trait.

Now create an object and call method sum with params 1 and 2 and you will get output as follows:

“Method called Child::sum Sum of 1 and 2 is 3” i.e. we can easily override trait function.

That’s it from traits, hope it will be a great add-on in your next project.

 

Top 5 Firefox Addons for a Web Developer

top-5-firefox-addons-for-a-web-developer

Web development is not an easy task but thanks to Firefox addons that makes development faster and easier. Today I am gonna talk about some of the firefox addons which I used for web development.

Firebug

Firebug is used to edit HTML, Css and JavaScript within browser. It is also used for profiling requests, checking request time, cookie and js debugging, inspecting dom element, profiling ajax requests and many more. Get Addon

firebug

Web Developer Toolbar

This is one of the best plugin for a Web Developer, it is used for following purpose:

  • Validating Css, HTML and Links
  • View Responsive layouts
  • Checking external links, iframes, floating elements
  • Rulers and line guides can be used to check proper alignment
  • Magnifier for deep look into web page
  • Forms profiling
  • Detailed information of images
  • Creating and handling cookies
  • Disabling JS, cookies, java and meta redirect
    Get Addon

web-developer-toolbar

Nimbus

It is a screen capture and editing tool. You can capture visible screen, whole web page or a section of page. It also gives you the feature of editing images like adding text and pointing out specific elements using arrows, rectangle and circles. This is not all, you can share with others without login with just one click. Get Addon

nimbusnimbus-sharing

Wappalyzer

It is used to uncover the technogies used on the website. It can detect several CMS, server side languages, JS and CSS libraries, analytics code and many more.

Get Addon

wappalyzer

JSONView

It prettifies json in the browser. It comes very handly while creating web application using JSON Api’s. Install plugin and check for changes here.

Get Addon

json-view

That all for today guyz, I hope these “Top 5 Firefox Addons for a Web Developer” will help you in increasing you productivity.