Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Channel status #159

Merged
merged 11 commits into from
May 30, 2022
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ $presencePage->items[0]->clientId; // client ID of first member
$presencePage->next(); // retrieves the next page => \Ably\Models\PaginatedResult
```

### Getting the channel status
```php
$channelStatus = $channel->status(); // => \Ably\Models\Status\ChannelDetails
var_dump($channelStatus);
```

### Generate Token and Token Request

```php
Expand Down
9 changes: 9 additions & 0 deletions src/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Ably\Models\ChannelOptions;
use Ably\Models\Message;
use Ably\Models\PaginatedResult;
use Ably\Models\Status\ChannelDetails;
use Ably\Utils\Stringifiable;

/**
Expand Down Expand Up @@ -152,6 +153,14 @@ public function history( $params = [] ) {
$params );
}

/**
* Retrieves current channel active status with no. of publishers, subscribers, presenceMembers etc
* @return ChannelDetails
*/
public function status() {
return ChannelDetails::from($this->ably->get("/channels/" . $this->getName()));
}

/**
* @return string Channel's name
*/
Expand Down
135 changes: 135 additions & 0 deletions src/Models/Status/ChannelDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Ably\Models\Status;

/**
* https://docs.ably.io/client-lib-development-guide/features/#CHD1
*/
class ChannelDetails
{
/**
* @var string
*/
public $name;

/**
* @var string
*/
public $channelId;

/**
* @var ChannelStatus
*/
public $status;

/**
* @param \stdClass
* @return ChannelDetails
*/
static function from($object) {
$channelDetails = new self();
$channelDetails->name = $object->name;
$channelDetails->channelId = $object->channelId;
$channelDetails->status = ChannelStatus::from($object->status);
return $channelDetails;
}
}

/**
* https://docs.ably.io/client-lib-development-guide/features/#CHS1
*/
class ChannelStatus
{
/**
* @var bool
*/
public $isActive;

/**
* @var ChannelOccupancy
*/
public $occupancy;

/**
* @param \stdClass
* @return ChannelStatus
*/
static function from($object) {
$channelStatus = new self();
$channelStatus->isActive = $object->isActive;
$channelStatus->occupancy = ChannelOccupancy::from($object->occupancy);
return $channelStatus;
}
}

/**
* https://docs.ably.io/client-lib-development-guide/features/#CHO1
*/
class ChannelOccupancy
{
/**
* @var ChannelMetrics
*/
public $metrics;

/**
* @param \stdClass
* @return ChannelOccupancy
*/
static function from($object) {
$occupancy = new self();
$occupancy->metrics = ChannelMetrics::from($object->metrics);
return $occupancy;
}
}

/**
* https://docs.ably.io/client-lib-development-guide/features/#CHM1
*/
class ChannelMetrics
{
/**
* @var int
*/
public $connections;

/**
* @var int
*/
public $presenceConnections;

/**
* @var int
*/
public $presenceMembers;

/**
* @var int
*/
public $presenceSubscribers;

/**
* @var int
*/
public $publishers;

/**
* @var int
*/
public $subscribers;

/**
* @param \stdClass
* @return ChannelMetrics
*/
static function from($object) {
$metrics = new self();
$metrics->connections = $object->connections;
$metrics->presenceConnections= $object->presenceConnections;
$metrics->presenceMembers = $object->presenceMembers;
$metrics->presenceSubscribers= $object->presenceSubscribers;
$metrics->publishers = $object->publishers;
$metrics->subscribers = $object->subscribers;
return $metrics;
}
}
40 changes: 40 additions & 0 deletions tests/ChannelStatusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace tests;
use Ably\AblyRest;

require_once __DIR__ . '/factories/TestApp.php';


class ChannelStatusTest extends \PHPUnit\Framework\TestCase
{
protected static $testApp;
protected static $defaultOptions;
protected static $ably;

public static function setUpBeforeClass(): void
{
self::$testApp = new TestApp();
self::$defaultOptions = self::$testApp->getOptions();
self::$ably = new AblyRest(array_merge(self::$defaultOptions, [
'key' => self::$testApp->getAppKeyDefault()->string,
]));
}

public static function tearDownAfterClass(): void
{
self::$testApp->release();
}

/**
* @testdox RSL8, CHD1
*/
public function testChannelStatus()
{
$channel = self::$ably->channel('channel1');
$channelStatus = $channel->status();
self::assertNotNull($channelStatus->channelId);
self::assertEquals("channel1", $channelStatus->channelId);
self::assertEquals("channel1", $channelStatus->name);
self::assertTrue($channelStatus->status->isActive);
}
}