Results in Callback
My library provides the ability to use a callback after request execution. This is achieved using the parameter $rpc.callback, which contains a DSN (Data Source Name).
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.
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.
Algorithm
- Receive the request with the $rpc.callback parameter.
2. Validate the DSN.
3. Return an immediate acknowledgment to the client that the request has been accepted.
4. Process the request.
5. Send the response to the DSN specified in $rpc.callback.
Example
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.
Using $rpc.callback, 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.
The request might look like this:
2
3
4
5
6
7
8
"id": "example_order",
"method": "OrderService.processOrder",
"params": {
"orderId": "12345",
"$rpc.callback": "https://example.com/order/callback"
}
}
In response, the user immediately receives a confirmation.
2
3
4
5
6
7
8
9
10
"id": "example_order",
"result": {
"callback": {
"url": "https://example.com/order/callback",
"status": true,
"data": []
}
}
}
After processing the request, a message will be sent to the DSN specified as the callback.
2
3
4
5
6
7
8
9
"id": "example_order",
"result": {
"orderId": "12345",
"status": "completed",
"totalAmount": 99.99,
"message": "Your order has been processed successfully."
}
}
Benefits
- Immediate acknowledgment: The client receives a quick response that the request has been accepted, without waiting for its complete processing.
2. Asynchronous processing: The request is processed asynchronously, and the result is sent to the specified callback.
3. Flexibility: The ability to specify different types of callbacks, such as webhooks or websockets, to receive results.
This functionality improves the efficiency of interaction with the API, providing quick acknowledgment of request acceptance and convenient receipt of processing results.