Wiki source code of Результати у Callback
Last modified by Ashterix on 2024/05/17 10:56
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{box cssClass="floatinginfobox" width="300px" title="**Content**"}} | ||
2 | {{toc/}} | ||
3 | {{/box}} | ||
4 | |||
5 | {{info}} | ||
6 | (% class="wikigeneratedid" %) | ||
7 | Sometimes, you might need to receive a response from requests that can take a long time to process. Handling them synchronously might not be a good idea because it leads to client blocking, response delays, and overall system performance degradation. In such cases, the callback functionality after request execution becomes particularly useful. | ||
8 | |||
9 | (% class="wikigeneratedid" %) | ||
10 | Instead of waiting for all operations to complete, the client can receive an immediate acknowledgment that the request has been accepted. After processing the request, the server will send the result to the specified callback URL, allowing the client to continue working without delays. | ||
11 | {{/info}} | ||
12 | |||
13 | (% class="wikigeneratedid" id="HCallback43F45644143B44F43243843A43E43D43043D43D44F43243843A43E43D43043D43D44F43243843A43E43D43043D43D44F43243843A43E43D43043D43D44F43243843A43E43D43043D43D44F43243843A43E43D43043D43D44F43243843A43E43D43043D43D44F43243843A43E43D43043D43D44F43}" %) | ||
14 | My library provides the ability to use a callback after request execution. This is achieved using the parameter {{code language="none"}}$rpc.callback{{/code}}, which contains a DSN (Data Source Name). | ||
15 | |||
16 | When the server receives a request with this parameter, it interprets it as a method for asynchronous invocation. Therefore, after validating the DSN specified as the callback, it immediately returns a response confirming that the request will be sent to the callback. | ||
17 | |||
18 | This functionality allows you to specify where the response will be sent after the request is processed. This could be a POST request to a webhook that transmits the response object in the body, or a websocket. | ||
19 | |||
20 | = Algorithm = | ||
21 | |||
22 | 1. Receive the request with the {{code language="none"}}$rpc.callback{{/code}} parameter. | ||
23 | 2. Validate the DSN. | ||
24 | 3. Return an immediate acknowledgment to the client that the request has been accepted. | ||
25 | 4. Process the request. | ||
26 | 5. Send the response to the DSN specified in {{code language="none"}}$rpc.callback{{/code}}. | ||
27 | |||
28 | = Example = | ||
29 | |||
30 | Imagine we manage an online store where users can place orders. Each order needs to be processed, including checking product availability, calculating the total cost, and confirming payment. These operations can take some time. | ||
31 | |||
32 | Using {{code language="none"}}$rpc.callback{{/code}}, we can significantly improve the user experience. For example, after a user confirms an order, we can immediately return a message indicating that the order has been received and is being processed, without waiting for all operations to complete. | ||
33 | |||
34 | (% class="row" %) | ||
35 | ((( | ||
36 | (% class="col-xs-12 col-sm-6" %) | ||
37 | ((( | ||
38 | The request might look like this: | ||
39 | |||
40 | {{code language="json" layout="LINENUMBERS" title="Request /api"}} | ||
41 | { | ||
42 | "id": "example_order", | ||
43 | "method": "OrderService.processOrder", | ||
44 | "params": { | ||
45 | "orderId": "12345", | ||
46 | "$rpc.callback": "https://example.com/order/callback" | ||
47 | } | ||
48 | } | ||
49 | {{/code}} | ||
50 | |||
51 | (% class="box warningmessage" %) | ||
52 | ((( | ||
53 | Currently, the DSN that you can specify in this parameter must only contain the HTTPS protocol! | ||
54 | ))) | ||
55 | |||
56 | (% class="box errormessage" %) | ||
57 | ((( | ||
58 | The HTTP protocol is not supported for data security reasons. | ||
59 | ))) | ||
60 | |||
61 | (% class="box infomessage" %) | ||
62 | ((( | ||
63 | Future plans include expanding the list of supported protocols. | ||
64 | ))) | ||
65 | ))) | ||
66 | |||
67 | (% class="col-xs-12 col-sm-6" %) | ||
68 | ((( | ||
69 | In response, the user immediately receives a confirmation. | ||
70 | |||
71 | {{code language="json" layout="LINENUMBERS" title="Response from /api"}} | ||
72 | { | ||
73 | "id": "example_order", | ||
74 | "result": { | ||
75 | "callback": { | ||
76 | "url": "https://example.com/order/callback", | ||
77 | "status": true, | ||
78 | "data": [] | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | {{/code}} | ||
83 | |||
84 | After processing the request, a message will be sent to the DSN specified as the callback. | ||
85 | |||
86 | {{code language="json" layout="LINENUMBERS" title="Callback Request"}} | ||
87 | { | ||
88 | "id": "example_order", | ||
89 | "result": { | ||
90 | "orderId": "12345", | ||
91 | "status": "completed", | ||
92 | "totalAmount": 99.99, | ||
93 | "message": "Your order has been processed successfully." | ||
94 | } | ||
95 | } | ||
96 | {{/code}} | ||
97 | ))) | ||
98 | ))) | ||
99 | |||
100 | = Benefits = | ||
101 | |||
102 | 1. **Immediate acknowledgment**: The client receives a quick response that the request has been accepted, without waiting for its complete processing. | ||
103 | 2. **Asynchronous processing**: The request is processed asynchronously, and the result is sent to the specified callback. | ||
104 | 3. **Flexibility**: The ability to specify different types of callbacks, such as webhooks or websockets, to receive results. | ||
105 | |||
106 | This functionality improves the efficiency of interaction with the API, providing quick acknowledgment of request acceptance and convenient receipt of processing results. |