Messages

Sliders

Sliders

Add a Signup Form to Your Website

Add a Signup Form to Your Website

مراحل ثبت دامنه و تنظیمات DNS

مراحل ثبت دامنه و تنظیمات DNS

چرا ترافیک وب سایت برای تجارت من مهم است؟

چرا ترافیک وب سایت برای تجارت من مهم است؟

Why is Website Traffic Important to My Business?

Why is Website Traffic Important to My Business?

ترافیک وب

ترافیک وب

Web traffic

Web traffic

8 نشانه اساسی گوگل برای رتبه بندی در سال 2019

8 نشانه اساسی گوگل برای رتبه بندی در سال 2019

مشکلات رایج در کدنویسی معتبر و رفع آنها

مشکلات رایج در کدنویسی معتبر و رفع آنها

article حل مشکل محتوای تکراری (Duplicate Content) در سایت

article حل مشکل محتوای تکراری (Duplicate Content) در سایت

رتبه بندی الکسا (Alexa) چیست و چقدر اهمیت دارد؟!

رتبه بندی الکسا (Alexa) چیست و چقدر اهمیت دارد؟!

چرا برخی سایت ها و وبلاگ ها رتبه الکسای بهتری دارند؟

چرا برخی سایت ها و وبلاگ ها رتبه الکسای بهتری دارند؟

article پیج رنک چیست و چگونه می توان آن را افزایش داد؟

article پیج رنک چیست و چگونه می توان آن را افزایش داد؟

article چگونه سایت یا وبلاگ را برای موتورهای جستجو بهینه سازی کنیم؟

article چگونه سایت یا وبلاگ را برای موتورهای جستجو بهینه سازی کنیم؟

کدام نوع وبلاگ می تواند برای تجارت  شما مفید باشد

کدام نوع وبلاگ می تواند برای تجارت شما مفید باشد

What a Blog Can Do For Your Small Business

What a Blog Can Do For Your Small Business

WhatsApp vs WhatsApp Business App: What's the Difference?

WhatsApp vs WhatsApp Business App: What's the Difference?

TinyMCE 5: Inline editor example

TinyMCE 5: Inline editor example

TinyMCE 5 - Inline editor example

TinyMCE 5 - Inline editor example

Native HTML5 Drag and Drop

Native HTML5 Drag and Drop

HTML5 Drag and Drop

HTML5 Drag and Drop

Step Organization in Java

Step Organization in Java

How to use Cucumber? – Cucumber best practices

How to use Cucumber? – Cucumber best practices

A Guide to Cucumber Best Practices

A Guide to Cucumber Best Practices

Why Responsive Design Support is the Most Important Feature You Can Add To Your Website

Why Responsive Design Support is the Most Important Feature You Can Add To Your Website

5 مزایای قدرتمند وبلاگ نویسی برای تجارت شما

5 مزایای قدرتمند وبلاگ نویسی برای تجارت شما

5 POWERFUL BENEFITS OF BLOGGING FOR YOUR SMALL BUSINESS

5 POWERFUL BENEFITS OF BLOGGING FOR YOUR SMALL BUSINESS

Angular 1 using redux architecture

Angular 1 using redux architecture

Home Login Logout

PHP RabbitMq: Getting Start

RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that Mr. or Ms. Mailperson will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office and a postman.

RabbitMQ is a message broker: it accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that Mr. or Ms. Mailperson will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office and a postman.

The major difference between RabbitMQ and the post office is that it doesn't deal with paper, instead it accepts, stores and forwards binary blobs of data ‒ messages.

RabbitMQ, and messaging in general, uses some jargon.

Entities

Note that the producer, consumer, and broker do not have to reside on the same host; indeed in most applications they don't. An application can be both a producer and consumer, too.

Producer 

Producing means nothing more than sending. A program that sends messages is a producer :

Queue

A queue is the name for a post box which lives inside RabbitMQ. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queueis only bound by the host's memory & disk limits, it's essentially a large message buffer. Many producers can send messages that go to one queue, and many consumers can try to receive data from one queue. This is how we represent a queue:

Consumer

Consuming has a similar meaning to receiving. A consumer is a program that mostly waits to receive messages:

using the php-amqplib Client

In this part of the tutorial we'll write two programs in PHP; a producer that sends a single message, and a consumer that receives messages and prints them out. We'll gloss over some of the detail in the php-amqplib API, concentrating on this very simple thing just to get started. It's a "Hello World" of messaging.

In the diagram below, "P" is our producer and "C" is our consumer. The box in the middle is a queue - a message buffer that RabbitMQ keeps on behalf of the consumer.

RabbitMQ speaks multiple protocols. This tutorial covers AMQP 0-9-1, which is an open, general-purpose protocol for messaging. There are a number of clients for RabbitMQ in many different languages. We'll use the php-amqplib in this tutorial, and Composer for dependency management.

Add a composer.json file to your project:

{
    "require": {
        "php-amqplib/php-amqplib": ">=2.6.1"
    }
}

Provided you have Composer installed and functional, you can run the following:

composer.phar install

Now we have the php-amqplib library installed, we can write some code.

Sending

We'll call our message publisher (sender) send.php and our message receiver receive.php. The publisher will connect to RabbitMQ, send a single message, then exit.

In send.php, we need to include the library and use the necessary classes:

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

then we can create a connection to the server:

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

The connection abstracts the socket connection, and takes care of protocol version negotiation and authentication and so on for us. Here we connect to a broker on the local machine - hence the localhost. If we wanted to connect to a broker on a different machine we'd simply specify its name or IP address here.

Next we create a channel, which is where most of the API for getting things done resides.

To send, we must declare a queue for us to send to; then we can publish a message to the queue:

$channel->queue_declare('hello', false, false, false, false);

$msg = new AMQPMessage('Hello World!');
$channel->basic_publish($msg, '', 'hello');

echo " [x] Sent 'Hello World!'\n";

Declaring a queue is idempotent - it will only be created if it doesn't exist already. The message content is a byte array, so you can encode whatever you like there.

Lastly, we close the channel and the connection;

$channel->close();
$connection->close();


Receiving

That's it for our publisher. Our receiver listening for messages from RabbitMQ, so unlike the publisher which publishes a single message, we'll keep it running to listen for messages and print them out.

The code (in receive.php) has almost the same include and uses as send:

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;

Setting up is the same as the publisher; we open a connection and a channel, and declare the queue from which we're going to consume. Note this matches up with the queue that sendpublishes to.

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

$channel->queue_declare('hello', false, false, false, false);

echo " [*] Waiting for messages. To exit press CTRL+C\n";

Note that we declare the queue here, as well. Because we might start the consumer before the publisher, we want to make sure the queue exists before we try to consume messages from it.

We're about to tell the server to deliver us the messages from the queue. We will define a PHP callable that will receive the messages sent by the server. Keep in mind that messages are sent asynchronously from the server to the clients.

$callback = function ($msg) {
  echo ' [x] Received ', $msg->body, "\n";
};

$channel->basic_consume('hello', '', false, true, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}

Our code will block while our $channel has callbacks. Whenever we receive a message our $callback function will be passed the received message.

Here's the whole receive.php class


Putting it all together

Now we can run both scripts. In a terminal, run the consumer (receiver):

php receive.php

then, run the publisher (sender):

php send.php

The consumer will print the message it gets from the sender via RabbitMQ. The receiver will keep running, waiting for messages (Use Ctrl-C to stop it), so try running the sender from another terminal.


مصطفی برمشوری

Lates posts
Most visited posts

Built with love to give everyone an opportunity to say something to the world
Copyright © 2018 All rights reserved - SITE123 

Terms - Privacy - Abuse

LocalTheme

Help

سوالی دارید؟ با ما مطرح کنید !پشتیبانی آنلاین است.در حال حاضر پشتیبانی غیرفعال است.با تیم ویرا وب صحبت کنیدبا تیم ویرا وب صحبت کنید