Skip to content

Commit 7d60675

Browse files
committed
[client] Add docs for rpc client
1 parent f42fbdb commit 7d60675

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

docs/client/rpc_call.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Client. RPC call
2+
3+
4+
## The client side
5+
6+
There is a handy class RpcClient shipped with the client component.
7+
It allows you to easily send a message and wait for a reply.
8+
9+
```php
10+
<?php
11+
use Enqueue\Client\SimpleClient;
12+
use Enqueue\Client\RpcClient;
13+
14+
/** @var \Enqueue\Psr\PsrContext $context */
15+
16+
17+
$client = new SimpleClient($context);
18+
$rpcClient = new RpcClient($client->getProducer(), $context);
19+
20+
$replyMessage = $rpcClient->call('greeting_topic', 'Hi Thomas!', 5);
21+
```
22+
23+
You can perform several requests asynchronously with `callAsync` and request replays later.
24+
25+
```php
26+
<?php
27+
use Enqueue\Client\SimpleClient;
28+
use Enqueue\Client\RpcClient;
29+
30+
/** @var \Enqueue\Psr\PsrContext $context */
31+
32+
33+
$client = new SimpleClient($context);
34+
$rpcClient = new RpcClient($client->getProducer(), $context);
35+
36+
$promises = [];
37+
$promises[] = $rpcClient->callAsync('greeting_topic', 'Hi Thomas!', 5);
38+
$promises[] = $rpcClient->callAsync('greeting_topic', 'Hi Thomas!', 5);
39+
$promises[] = $rpcClient->callAsync('greeting_topic', 'Hi Thomas!', 5);
40+
$promises[] = $rpcClient->callAsync('greeting_topic', 'Hi Thomas!', 5);
41+
42+
$replyMessages = [];
43+
foreach ($promises as $promise) {
44+
$replyMessages[] = $promise->getMessage();
45+
}
46+
```
47+
48+
## The server side
49+
50+
On the server side you may register a processor which returns a result object with a reply message set.
51+
Of course it is possible to implement rpc server side based on transport classes only. That would require a bit more work to do.
52+
53+
```php
54+
<?php
55+
56+
use Enqueue\Client\SimpleClient;
57+
use Enqueue\Psr\PsrMessage;
58+
use Enqueue\Psr\PsrContext;
59+
use Enqueue\Consumption\Result;
60+
use Enqueue\Consumption\ChainExtension;
61+
use Enqueue\Consumption\Extension\ReplyExtension;
62+
63+
/** @var \Enqueue\Psr\PsrContext $context */
64+
65+
$client = new SimpleClient($this->context);
66+
$client->bind('greeting_topic', 'greeting_processor', function (PsrMessage $message, PsrContext $context) use (&$requestMessage) {
67+
echo $message->getBody();
68+
69+
return Result::reply($context->createMessage('Hi there! I am John.'));
70+
});
71+
72+
$client->consume(new ChainExtension([new ReplyExtension()]));
73+
```
74+
75+
[back to index](../index.md)

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Message examples](client/message_examples.md)
1414
- [Supported brokers](client/supported_brokers.md)
1515
- [Message bus](client/message_bus.md)
16+
- [RPC call](client/rpc_call.md)
1617
* Job queue
1718
- [Run unique job](job_queue/run_unique_job.md)
1819
- [Run sub job(s)](job_queue/run_sub_job.md)

0 commit comments

Comments
 (0)