|
| 1 | +# Simple client. Quick tour. |
| 2 | + |
| 3 | +The simple client library takes Enqueue client classes and Symfony components and makes an easy to use client facade. |
| 4 | +It reduces the boiler plate code you have to write to start using the Enqueue client features. |
| 5 | + |
| 6 | +* [Install](#install) |
| 7 | +* [Configure](#configure) |
| 8 | +* [Producer message](#produce-message) |
| 9 | +* [Consume messages](#consume-messages) |
| 10 | + |
| 11 | +## Install |
| 12 | + |
| 13 | +```bash |
| 14 | +$ composer require enqueue/simple-client enqueue/amqp-ext |
| 15 | +``` |
| 16 | + |
| 17 | +## Configure |
| 18 | + |
| 19 | +```php |
| 20 | +<?php |
| 21 | +use Enqueue\SimpleClient\SimpleClient; |
| 22 | + |
| 23 | +include __DIR__.'/vendor/autoload.php'; |
| 24 | + |
| 25 | +$client = new SimpleClient([ |
| 26 | + 'transport' => [ |
| 27 | + 'default' => 'amqp', |
| 28 | + 'amqp' => [ |
| 29 | + 'host' => 'localhost', |
| 30 | + 'port' => 5672, |
| 31 | + 'vhost' => '/', |
| 32 | + 'login' => 'guest', |
| 33 | + 'password' => 'guest', |
| 34 | + ], |
| 35 | + ], |
| 36 | + 'client' => [ |
| 37 | + 'app_name' => 'plain_php', |
| 38 | + ], |
| 39 | +]); |
| 40 | +``` |
| 41 | + |
| 42 | +## Produce message |
| 43 | + |
| 44 | +```php |
| 45 | +<?php |
| 46 | + |
| 47 | +/** @var \Enqueue\SimpleClient\SimpleClient $client */ |
| 48 | + |
| 49 | +$client->send('a_bar_topic', 'aMessageData'); |
| 50 | + |
| 51 | +// or an array |
| 52 | + |
| 53 | +$client->send('a_bar_topic', ['foo', 'bar']); |
| 54 | + |
| 55 | +// or an json serializable object |
| 56 | +$client->send('a_bar_topic', new class() implements \JsonSerializable { |
| 57 | + public function jsonSerialize() { |
| 58 | + return ['foo', 'bar']; |
| 59 | + } |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +## Consume messages |
| 64 | + |
| 65 | +```php |
| 66 | +<?php |
| 67 | + |
| 68 | +use Enqueue\Psr\PsrMessage; |
| 69 | + |
| 70 | +/** @var \Enqueue\SimpleClient\SimpleClient $client */ |
| 71 | + |
| 72 | +$client->bind('a_bar_topic', 'a_processor_name', function(PsrMessage $psrMessage) { |
| 73 | + // processing logic here |
| 74 | +}); |
| 75 | + |
| 76 | +$client->consume(); |
| 77 | +``` |
| 78 | + |
| 79 | +## Cli commands |
| 80 | + |
| 81 | +```php |
| 82 | +#!/usr/bin/env php |
| 83 | +<?php |
| 84 | + |
| 85 | +// bin/enqueue.php |
| 86 | + |
| 87 | +use Enqueue\Symfony\Client\ConsumeMessagesCommand; |
| 88 | +use Enqueue\Symfony\Client\Meta\QueuesCommand; |
| 89 | +use Enqueue\Symfony\Client\Meta\TopicsCommand; |
| 90 | +use Enqueue\Symfony\Client\ProduceMessageCommand; |
| 91 | +use Enqueue\Symfony\Client\SetupBrokerCommand; |
| 92 | +use Symfony\Component\Console\Application; |
| 93 | + |
| 94 | +/** @var \Enqueue\SimpleClient\SimpleClient $client */ |
| 95 | + |
| 96 | +$application = new Application(); |
| 97 | +$application->add(new SetupBrokerCommand($client->getDriver())); |
| 98 | +$application->add(new ProduceMessageCommand($client->getProducer())); |
| 99 | +$application->add(new QueuesCommand($client->getQueueMetaRegistry())); |
| 100 | +$application->add(new TopicsCommand($client->getTopicMetaRegistry())); |
| 101 | +$application->add(new ConsumeMessagesCommand( |
| 102 | + $client->getQueueConsumer(), |
| 103 | + $client->getDelegateProcessor(), |
| 104 | + $client->getQueueMetaRegistry(), |
| 105 | + $client->getDriver() |
| 106 | +)); |
| 107 | + |
| 108 | +$application->run(); |
| 109 | +``` |
| 110 | + |
| 111 | +and run to see what is there: |
| 112 | + |
| 113 | +```bash |
| 114 | +$ php bin/enqueue.php |
| 115 | +``` |
| 116 | + |
| 117 | +or consume messages |
| 118 | + |
| 119 | +```bash |
| 120 | +$ php bin/enqueue.php enqueue:consume -vvv --setup-broker |
| 121 | +``` |
| 122 | + |
| 123 | +[back to index](../index.md) |
0 commit comments