The API package is hosted on packagist.org.
Usage
Install the MigratoryData client library 6.x using composer (MigratoryData client version 6 can be used with the MigratoryData server 6.0.1):
composer require migratorydata/migratorydata-client-reactphp:6.*
Import classes and define MigratoryData callback listener:
require __DIR__ . '/vendor/autoload.php';
use MigratoryData\Client\MigratoryDataClient;
use MigratoryData\Client\MigratoryDataMessage;
use MigratoryData\Client\MigratoryDataListener;
class MyListener implements MigratoryDataListener
{
public function onMessage($message)
{
echo "Got message: " . $message . "\n";
}
public function onStatus($status, $info)
{
echo "Got status: " . $status . " - " . $info . "\n";
}
}
Create the event loop:
$loop = \React\EventLoop\Factory::create();
Create a MigratoryData client and attach the event loop:
$client = new MigratoryDataClient();
$client->setLoop($loop);
Initialize and connect the MigratoryData client:
$client->setEntitlementToken("some-token");
$client->setServers(array("http://127.0.0.1:8800"));
$client->subscribe(array("/server/status"));
$client->connect();
Publish a message every second to MigratoryData server:
$loop->addPeriodicTimer(1, function () use ($client) {
try {
$client->publish(new MigratoryDataMessage("/server/status", "Msg " . time(), "closure-" . time()));
} catch (MigratoryDataException $e) {
echo($e->getDetail());
echo($e->getCause());
}
});
Start the event loop:
$loop->run();
Example client application
The client application connects to the MigratoryData server deployed at localhost:8800
subscribes and publishes a message every second on the subject /server/status
.
If you don’t have a MigratoryData server installed on your machine but there is docker installed you can run the following command to start MigratoryData server, otherwise you can download and install the latest version for your os from here.
docker pull migratorydata/server:latest
docker run --platform linux/amd64 -d --name my_migratorydata -p 8800:8800 migratorydata/server:latest
Copy the code below to a file named echo-time-client.php
:
<?php
require __DIR__ . '/vendor/autoload.php';
use MigratoryData\Client\MigratoryDataClient;
use MigratoryData\Client\MigratoryDataException;
use MigratoryData\Client\MigratoryDataMessage;
use MigratoryData\Client\MigratoryDataListener;
class MyListener implements MigratoryDataListener
{
public function onMessage($message)
{
echo "Got message: " . $message . "\n";
}
public function onStatus($status, $info)
{
echo "Got status: " . $status . " - " . $info . "\n";
}
}
$loop = \React\EventLoop\Factory::create();
$client = new MigratoryDataClient();
$client->setEntitlementToken("some-token");
$client->setLoop($loop);
$client->setListener(new MyListener());
try {
$client->setServers(array("http://127.0.0.1:8800"));
} catch (MigratoryDataException $e) {
echo($e->getDetail());
exit(1);
}
$client->subscribe(array("/server/status"));
$client->connect();
$loop->addPeriodicTimer(1, function () use ($client) {
try {
$client->publish(new MigratoryDataMessage("/server/status", "Msg " . time(), "closure-" . time()));
} catch (MigratoryDataException $e) {
echo($e->getDetail());
echo($e->getCause());
}
});
$loop->run();
Next, run the application using command:
php echo-time-client.php
To test the application, connect to the MigratoryData server via browser at http://localhost:8800
and go to Debug Console
. Subscribe and publish data to subject /server/status
and see if the application is receiving data. Also you should receive data published by the application. The application publishes a message every second on subject /server/status
.