When preparing response for request Skynet launch Events Listeners whitch can add their own fields to response. Returned by response fields are received by request sender and available in its own response object.
In code, the response object is creating this way:
use Skynet\Data\SkynetResponse;
$response = new SkynetResponse();
Look at an example:
Code below creates new response object and fills field called 'foo' with value 'bar':
use Skynet\Data\SkynetResponse;
$response = new SkynetResponse();
$response->set('foo', 'bar');
Code below will receive value of field 'foo' from response:
$foo = $response->get('foo');
Let's see how it works:
Sender cluster sends a prepared request to another cluster sending field 'foo' to it:
On request sender cluster side:
use Skynet\Data\SkynetRequest;
$request = new SkynetRequest();
$request->set('foo', 'bar');
Request above is sending to receiver cluster, at next the receiver cluster gets field 'foo' from own request object.
On request receiver side:
use Skynet\Data\SkynetRequest;
$request = new SkynetRequest();
$foo = $request->get('foo');
And now, the receiver can prepare response for this request, e.g.:
On request receiver (and response sender) side:
use Skynet\Data\SkynetRequest;
use Skynet\Data\SkynetResponse;
$request = new SkynetRequest();
$response = new SkynetResponse();
if($request->get('foo') == 'bar')
{
$response->set('bar', 'foo');
}
At next, response is received by request sender and request sender have access to this response by own response object:
On request sender (and response receiver) side:
use Skynet\Data\SkynetResponse;
$response = new SkynetResponse();
if($response->get('bar') == 'foo')
{
/* do something */
}
The Response object works in 2 ways:
- on "receiver" side cluster generates response for request from sender cluster
- on "sender" side response is received and request sender can get data from this request received from response sender via own response object
In Event Listeners you will be using response object via:
$this->response
Response is preparing once when request arrives and response for it is generated.
You can generate response only from code via Event Listeners.