Results in Callback

Last modified by Ashterix on 2024/05/17 10:56

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.

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.

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

  1. 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:

Request /api
1
2
3
4
5
6
7
8
{
  "id": "example_order",
  "method": "OrderService.processOrder",
  "params": {
     "orderId": "12345",
     "$rpc.callback": "https://example.com/order/callback"
   }
}

Currently, the DSN that you can specify in this parameter must only contain the HTTPS protocol!

The HTTP protocol is not supported for data security reasons.

Future plans include expanding the list of supported protocols.

In response, the user immediately receives a confirmation.

Response from /api
1
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.

Callback Request
1
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

  1. 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.