| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,123 @@ | |||
| 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) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,7 @@ | |||
| 1 | 1 | # Client. RPC call | |
| 2 | 2 | ||
| 3 | + The client's [quick tour](quick_tour.md) describes how to get the client object. | ||
| 4 | + We use you followed instructions there and have instance of `Enqueue\SimpleClient\SimpleClient` in `$client` var. | ||
| 3 | 5 | ||
| 4 | 6 | ## The client side | |
| 5 | 7 | ||
@@ -8,13 +10,10 @@ It allows you to easily send a message and wait for a reply. | |||
| 8 | 10 | ||
| 9 | 11 | ```php | |
| 10 | 12 | <?php | |
| 11 | - use Enqueue\Client\SimpleClient; | ||
| 12 | 13 | use Enqueue\Client\RpcClient; | |
| 13 | 14 | ||
| 14 | - /** @var \Enqueue\Psr\PsrContext $context */ | ||
| 15 | - | ||
| 15 | + /** @var \Enqueue\SimpleClient\SimpleClient $client */ | ||
| 16 | 16 | ||
| 17 | - $client = new SimpleClient($context); | ||
| 18 | 17 | $rpcClient = new RpcClient($client->getProducer(), $context); | |
| 19 | 18 | ||
| 20 | 19 | $replyMessage = $rpcClient->call('greeting_topic', 'Hi Thomas!', 5); | |
@@ -24,13 +23,10 @@ You can perform several requests asynchronously with `callAsync` and request rep | |||
| 24 | 23 | ||
| 25 | 24 | ```php | |
| 26 | 25 | <?php | |
| 27 | - use Enqueue\Client\SimpleClient; | ||
| 28 | 26 | use Enqueue\Client\RpcClient; | |
| 29 | 27 | ||
| 30 | - /** @var \Enqueue\Psr\PsrContext $context */ | ||
| 28 | + /** @var \Enqueue\SimpleClient\SimpleClient $client */ | ||
| 31 | 29 | ||
| 32 | - | ||
| 33 | - $client = new SimpleClient($context); | ||
| 34 | 30 | $rpcClient = new RpcClient($client->getProducer(), $context); | |
| 35 | 31 | ||
| 36 | 32 | $promises = []; | |
@@ -53,7 +49,6 @@ Of course it is possible to implement rpc server side based on transport classes | |||
| 53 | 49 | ```php | |
| 54 | 50 | <?php | |
| 55 | 51 | ||
| 56 | - use Enqueue\Client\SimpleClient; | ||
| 57 | 52 | use Enqueue\Psr\PsrMessage; | |
| 58 | 53 | use Enqueue\Psr\PsrContext; | |
| 59 | 54 | use Enqueue\Consumption\Result; | |
@@ -62,7 +57,8 @@ use Enqueue\Consumption\Extension\ReplyExtension; | |||
| 62 | 57 | ||
| 63 | 58 | /** @var \Enqueue\Psr\PsrContext $context */ | |
| 64 | 59 | ||
| 65 | - $client = new SimpleClient($this->context); | ||
| 60 | + /** @var \Enqueue\SimpleClient\SimpleClient $client */ | ||
| 61 | + | ||
| 66 | 62 | $client->bind('greeting_topic', 'greeting_processor', function (PsrMessage $message, PsrContext $context) use (&$requestMessage) { | |
| 67 | 63 | echo $message->getBody(); | |
| 68 | 64 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -167,20 +167,30 @@ Here's an example of how you can send and consume messages. | |||
| 167 | 167 | ||
| 168 | 168 | ```php | |
| 169 | 169 | <?php | |
| 170 | - use Enqueue\Client\SimpleClient; | ||
| 170 | + use Enqueue\SimpleClient\SimpleClient; | ||
| 171 | 171 | use Enqueue\Psr\PsrMessage; | |
| 172 | - use Enqueue\Psr\PsrProcessor; | ||
| 173 | - | ||
| 174 | - /** @var \Enqueue\Psr\PsrContext $psrContext */ | ||
| 175 | 172 | ||
| 176 | - $client = new SimpleClient($psrContext); | ||
| 177 | - $client->bind('foo_topic', 'processor_name', function (PsrMessage $message) { | ||
| 178 | - // process message | ||
| 179 | - | ||
| 180 | - return PsrProcessor::ACK; | ||
| 173 | + $client = new SimpleClient([ | ||
| 174 | + 'transport' => [ | ||
| 175 | + 'default' => 'amqp', | ||
| 176 | + 'amqp' => [ | ||
| 177 | + 'host' => 'localhost', | ||
| 178 | + 'port' => 5672, | ||
| 179 | + 'vhost' => '/', | ||
| 180 | + 'login' => 'guest', | ||
| 181 | + 'password' => 'guest', | ||
| 182 | + ], | ||
| 183 | + ], | ||
| 184 | + 'client' => true, | ||
| 185 | + ]); | ||
| 186 | + | ||
| 187 | + $client->setupBroker(); | ||
| 188 | + | ||
| 189 | + $client->bind('a_foo_topic', 'fooProcessor', function(PsrMessage $message) { | ||
| 190 | + // your processing logic here | ||
| 181 | 191 | }); | |
| 182 | 192 | ||
| 183 | - $client->send('foo_topic', 'Hello there!'); | ||
| 193 | + $client->send('a_bar_topic', 'aMessageData'); | ||
| 184 | 194 | ||
| 185 | 195 | // in another process you can consume messages. | |
| 186 | 196 | $client->consume(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments