Using the Client

Using the AlphaRPC client is quite simple. It consists of three steps:

  • Configure the Client.
  • Perform a request.
  • (Optional:) Fetch the response.

Also see check the example client.

Configure the Client

You start with configuring the Client:

use AlphaRPC\Client\Client;

$client = new Client();

// Tell it where to find the Client Handler

Perform a request

Then, you have to perform a request. You have two choices here:

  • A synchronous request
  • An asynchronous request

Synchronous request

In case of a synchronous request, you do:

$params = array('Hello World!');
$response = $client->request('reverse', $params);

Now, you will have the response in the $response variable.

Asynchronous request

In case of an asynchronous request, you need two steps:

$params = array('Hello World!');

// Start the request.
$request = $client->startRequest('reverse', $params);

// Do something else.

// Fetch the response (and wait for it if it is not yet available).
$waitForIt = true;
$response = $client->fetchResponse($request, $waitForIt);

Now, again you will have the response in the $response variable again.

What about that $waitForIt variable?

Now, you may have noticed that we included a $waitForIt parameter when fetching the response. With this flag, you can indicate whether you want to wait until the response is available.

If you set this parameters to false and the response is not yet available, you will receive a AlphaRPC\Common\TimeoutException.

You can safely try to fetch the response again.

NOTE: fetching a response is not necessary! If you just want to send a request to a Worker, without caring about the result (in the client), you may choose not to fetch the response. That effectively makes your request a background taks.

Creating a Worker

If you want to run your own actions, see Creating a Worker.