rpc.post_response

← rpc.post_response

Ashterix

docs: rewrite JsonRpcBundle docs, add SDK docs, move them under docs/api

en/docs/api/json-rpc-bundle/functionality/events/post_response.md new file +101 āˆ’0

1 + # rpc.post_response
2 +
3 + ::::::block class="floatinginfobox floatinginfobox--fit"
4 + :::::columns border zebra
5 + ::::row
6 + :::col width="40%"
7 + **Event name**
8 + :::
9 + :::col
10 + `rpc.post_response`
11 + :::
12 + ::::
13 + ::::row
14 + :::col width="40%"
15 + **Constant**
16 + :::
17 + :::col
18 + `RpcEvent::POST_RESPONSE`
19 + :::
20 + ::::
21 + ::::row
22 + :::col width="40%"
23 + **Class**
24 + :::
25 + :::col
26 + `RpcPostResponseEvent`
27 + :::
28 + ::::
29 + ::::row
30 + :::col width="40%"
31 + **Properties**
32 + :::
33 + :::col
34 + `RpcResponse $response`, `RpcRequest $rpcRequest`, `Service $service`
35 + :::
36 + ::::
37 + :::::
38 + ::::::
39 +
40 + The client has already received the response. This is the event for heavy work that must not delay the response: auditing, sending emails, cache invalidation, integrations.
41 +
42 + ## When it is dispatched
43 +
44 + Once per **successfully** executed request:
45 +
46 + | Source | Moment |
47 + | --- | --- |
48 + | HTTP | `kernel.terminate`, after `fastcgi_finish_request()` (under PHP-FPM the client does not wait) |
49 + | Console, batch child processes | `console.terminate` |
50 + | Inline batch | right after each item |
51 + | `RpcExecutor` (adapters) | `kernel.terminate` of the main request |
52 +
53 + **Not dispatched** for an error response, a cached response and for messages from Symfony Messenger.
54 +
55 + An exception in a listener is logged and does not affect other listeners or other requests.
56 +
57 + ## Built-in listeners
58 +
59 + | Priority | Listener | What it does |
60 + | --- | --- | --- |
61 + | 1000 | `CacheRelationsEventListener` | Invalidates and warms up the cache according to [`#[RPC\CacheRelation]`](/docs/api/json-rpc-bundle/add_rpc_service/cache) |
62 + | 10 | `AsyncServerEventListener::processAsync` | **Executes** [notifications](/docs/api/json-rpc-bundle/functionality/notifications) and requests with [`$rpc.callback`](/docs/api/json-rpc-bundle/functionality/callback) (or hands them to a detached process) |
63 +
64 + :::danger
65 + Do not stop propagation with a priority above 10: notifications and callback requests will then never be executed.
66 + :::
67 +
68 + For notifications and callback, `$event->response` is a placeholder; after priority 10 the real result is available in `$event->rpcRequest->getResponseObject()`.
69 +
70 + ## What can be changed
71 +
72 + Nothing in the response — it has already been sent. Only side effects.
73 +
74 + ## Usage examples
75 +
76 + ### Change auditing
77 +
78 + ```php
79 + #[AsEventListener(RpcEvent::POST_RESPONSE)]
80 + final class AuditListener
81 + {
82 + public function __construct(private AuditLog $audit, private ApiGroupContext $groups) {}
83 +
84 + public function __invoke(RpcPostResponseEvent $event): void
85 + {
86 + $this->audit->record(
87 + method: $event->service->name,
88 + params: $event->rpcRequest->getParams(),
89 + group: $this->groups->getGroup(),
90 + );
91 + }
92 + }
93 + ```
94 +
95 + ### Post-response integration
96 +
97 + Send a webhook to a partner or sync data with a CRM without delaying the client. For very long operations, it is better to hand the work off to a Symfony Messenger queue.
98 +
99 + :::info
100 + At this point the Symfony HTTP request has already been popped from the `RequestStack`. If you need its headers, take them from `RequestCarrier` (`getCarrier()->getHttpRequest()` for `IRpcHttpCarrier`).
101 + :::