Skip to content

Commit

Permalink
Merge pull request #198 from ably/fix/batch-publish
Browse files Browse the repository at this point in the history
Fix batch publish
  • Loading branch information
sacOO7 authored Apr 11, 2024
2 parents 2a54c9e + be40570 commit 420f97d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,25 @@ If you're using Laravel and want to support **realtime broadcasting and events**

> If you want **ably-php** as a rest dependency across service providers, check [ably-php-laravel](https://packagist.org/packages/ably/ably-php-laravel). **ably-php-laravel** is a simple wrapper over **ably-php** with laravel-specific classes. This has limited use-cases and **laravel-broadcaster** is recommended over **ably-php-laravel** for most use-cases.
### Making explicit HTTP requests to Ably Rest Endpoints / Batch publish
- The `AblyRest->Request` method can be used to make explicit HTTP requests to the [Ably REST API](https://ably.com/docs/api/rest-api).
- It automatically adds necessary auth headers based on the initial auth config and supports pagination.
- The following is an example of using the batch publish API based on the [Ably batch publish rest endpoint documentation](https://ably.com/docs/api/rest-api#batch-publish).

```php
// batch publish needs php array to be passed and serialization is handled based on useBinaryProtocol
$payload = array(
"channels" => ["channel1", "channel2", "channel3", "channel4"],
"messages" => array(
"id" => "1",
"data" => "foo"
)
);
$batchPublishPaginatedResult = $client->request("POST", "/messages", [], $payload);
```
- See the [ably rest endpoint doc](https://ably.com/docs/api/rest-api) for more information on other endpoints.
- Ably uses `msgpack` as a default encoding for messages. Read [encode using msgpack for better efficiency](https://faqs.ably.com/do-you-binary-encode-your-messages-for-greater-efficiency).
- If you want to send payload as `json`, please set `useBinaryProtocol` as `false` in `clientOptions`.

## Support, feedback and troubleshooting

Expand Down
4 changes: 2 additions & 2 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ public function request( $method, $url, $headers = [], $params = [] ) {
$this->curl->setOpt( $ch, CURLOPT_POSTFIELDS, $params );

if ($this->postDataFormat == 'json') {
array_push( $headers, 'Content-Type: application/json' );
$headers[] = 'Content-Type: application/json';
}
elseif ($this->postDataFormat == 'msgpack') {
array_push( $headers, 'Content-Type: application/x-msgpack' );
$headers[] = 'Content-Type: application/x-msgpack';
}
} else {
throw new AblyRequestException( 'Unknown $params format', -1, -1 );
Expand Down
68 changes: 68 additions & 0 deletions tests/AblyRestRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
namespace tests;
use Ably\AblyRest;
require_once __DIR__ . '/factories/TestApp.php';

class AblyRestRequestTest 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();
}

/**
* Batch publishes messages for given list of channels
* RSC19
* https://ably.com/docs/api/rest-api#batch-publish
* @throws \Ably\Exceptions\AblyRequestException
*/
public function testBatchPublishMultipleChannelsUsingPostRequest() {

$payload = array(
"channels" => ["channel1", "channel2", "channel3", "channel4"],
"messages" => array(
"id" => "1",
"data" => "foo"
)
);

$batchPublishPaginatedResult = self::$ably->request("POST","/messages", [], $payload);
$this->assertNotNull($batchPublishPaginatedResult);
$this->assertEquals(201, $batchPublishPaginatedResult->statusCode);
$this->assertTrue($batchPublishPaginatedResult->success);
$this->assertNull($batchPublishPaginatedResult->errorCode);
$this->assertNull($batchPublishPaginatedResult->errorMessage);
$this->assertTrue( $batchPublishPaginatedResult->isLast(), 'Expected not to be the last page' );

if (self::$ably->options->useBinaryProtocol) {
$this->assertEquals("application/x-msgpack", $batchPublishPaginatedResult->headers["Content-Type"]);
} else {
$this->assertEquals("application/json", $batchPublishPaginatedResult->headers["Content-Type"]);
}
$this->assertCount(4, $batchPublishPaginatedResult->items);
foreach ($batchPublishPaginatedResult->items as $key=> $item) {
$this->assertEquals("channel".($key + 1), $item->channel);
$this->assertEquals(1, $item->messageId);
}

foreach (["channel1", "channel2", "channel3", "channel4"] as $channelName) {
$channel = self::$ably->channel($channelName);
$paginatedHistory = $channel->history();
foreach ($paginatedHistory->items as $msg) {
$this->assertEquals("1", $msg->id);
$this->assertEquals("foo", $msg->data);
}
}
}
}

0 comments on commit 420f97d

Please sign in to comment.