Skip to content

Commit 55e33e6

Browse files
authored
Merge pull request #209 from pusher/add-get-users-info-method
Add get_users_info method
2 parents 9646aa4 + 26bc343 commit 55e33e6

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ var_dump($subscription_counts);
306306

307307
### Get user information from a presence channel
308308

309+
```php
310+
$results = $pusher->get_users_info( 'presence-channel-name' );
311+
$users_count = count($results->users); // $users is an Array
312+
```
313+
314+
This can also be achieved using the generic `pusher->get` function:
315+
309316
```php
310317
$response = $pusher->get( '/channels/presence-channel-name/users' )
311318
```

src/Pusher.php

+20
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,26 @@ public function get_channels($params = array())
662662
return false;
663663
}
664664

665+
/**
666+
* Fetch user ids currently subscribed to a presence channel.
667+
*
668+
* @param string $channel The name of the channel
669+
*
670+
* @throws PusherException Throws exception if curl wasn't initialized correctly
671+
*
672+
* @return array|bool
673+
*/
674+
public function get_users_info($channel)
675+
{
676+
$response = $this->get('/channels/'.$channel.'/users');
677+
678+
if ($response['status'] === 200) {
679+
return json_decode($response['body']);
680+
}
681+
682+
return false;
683+
}
684+
665685
/**
666686
* GET arbitrary REST API resource using a synchronous http client.
667687
* All request signing is handled automatically.

tests/acceptance/channelQueryTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public function testFilterByPrefixOneChannel()
6767
$this->assertEquals(1, count($channels), 'channels have a single test-channel present. For this test to pass you must have the "Getting Started" page open on the dashboard for the app you are testing against');
6868
}
6969

70+
public function testUsersInfo()
71+
{
72+
$response = $this->pusher->get_users_info('presence-channel-test');
73+
74+
// print_r( $response );
75+
76+
$this->assertObjectHasAttribute('users', $response, 'class has users attribute');
77+
}
78+
7079
public function test_providing_info_parameter_with_prefix_query_fails_for_public_channel()
7180
{
7281
$options = array(

0 commit comments

Comments
 (0)