项目作者: migratorydata

项目描述 :
MigratoryData Client SDK for ReactPHP
高级语言: PHP
项目地址: git://github.com/migratorydata/migratorydata-client-reactphp.git
创建时间: 2020-07-06T10:16:16Z
项目社区:https://github.com/migratorydata/migratorydata-client-reactphp

开源协议:Other

下载


MigratoryData Client for ReactPHP 5.x & 6.x

Below you can find a tutorial and usage example. For more information please refer to MigratoryData Documentation 6.x.

Usage

Install the MigratoryData client library 5.x using composer (MigratoryData client version 5 can be used with MigratoryData Server 5.0.*):

  1. composer require migratorydata/migratorydata-client-reactphp:5.*

Install the MigratoryData client library 6.x using composer (MigratoryData client version 6 can be used with the MigratoryData server 6.0.1 or later):

  1. composer require migratorydata/migratorydata-client-reactphp:6.*

Import classes and define MigratoryData callback listener:

  1. require __DIR__ . '/vendor/autoload.php';
  2. use MigratoryData\Client\MigratoryDataClient;
  3. use MigratoryData\Client\MigratoryDataMessage;
  4. use MigratoryData\Client\MigratoryDataListener;
  5. class MyListener implements MigratoryDataListener
  6. {
  7. public function onMessage($message)
  8. {
  9. echo "Got message: " . $message . "\n";
  10. }
  11. public function onStatus($status, $info)
  12. {
  13. echo "Got status: " . $status . " - " . $info . "\n";
  14. }
  15. }

Create the event loop:

  1. $loop = \React\EventLoop\Factory::create();

Create a MigratoryData client and attach the event loop:

  1. $client = new MigratoryDataClient();
  2. $client->setLoop($loop);

Initialize and connect the MigratoryData client:

  1. $client->setEntitlementToken("some-token");
  2. $client->setServers(array("http://127.0.0.1:8800"));
  3. $client->subscribe(array("/server/status"));
  4. $client->connect();

Publish a message every second to MigratoryData server:

  1. $loop->addPeriodicTimer(1, function () use ($client) {
  2. try {
  3. $client->publish(new MigratoryDataMessage("/server/status", "Msg " . time(), "closure-" . time()));
  4. } catch (MigratoryDataException $e) {
  5. echo($e->getDetail());
  6. echo($e->getCause());
  7. }
  8. });

Start the event loop:

  1. $loop->run();

Example client application

Copy the code below to a file named echo-time-client.php and run it using the following command:

  1. php echo-time-client.php

Example for PHP React Client API

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.

  1. docker pull migratorydata/server:latest
  2. docker run -d --name my_migratorydata -p 8800:8800 migratorydata/server:latest
  1. <?php
  2. require __DIR__ . '/vendor/autoload.php';
  3. use MigratoryData\Client\MigratoryDataClient;
  4. use MigratoryData\Client\MigratoryDataException;
  5. use MigratoryData\Client\MigratoryDataMessage;
  6. use MigratoryData\Client\MigratoryDataListener;
  7. class MyListener implements MigratoryDataListener
  8. {
  9. public function onMessage($message)
  10. {
  11. echo "Got message: " . $message . "\n";
  12. }
  13. public function onStatus($status, $info)
  14. {
  15. echo "Got status: " . $status . " - " . $info . "\n";
  16. }
  17. }
  18. $loop = \React\EventLoop\Factory::create();
  19. $client = new MigratoryDataClient();
  20. $client->setEntitlementToken("some-token");
  21. $client->setLoop($loop);
  22. $client->setListener(new MyListener());
  23. try {
  24. $client->setServers(array("http://127.0.0.1:8800"));
  25. } catch (MigratoryDataException $e) {
  26. echo($e->getDetail());
  27. exit(1);
  28. }
  29. $client->subscribe(array("/server/status"));
  30. $client->connect();
  31. $loop->addPeriodicTimer(1, function () use ($client) {
  32. try {
  33. $client->publish(new MigratoryDataMessage("/server/status", "Msg " . time(), "closure-" . time()));
  34. } catch (MigratoryDataException $e) {
  35. echo($e->getDetail());
  36. echo($e->getCause());
  37. }
  38. });
  39. $loop->run();