PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/pusher/pusher-php-server/README.md

https://gitlab.com/fogteen/gasaon
Markdown | 308 lines | 212 code | 96 blank | 0 comment | 0 complexity | 270b041cc4aae7b01422cf428a934dfd MD5 | raw file
  1. # Pusher PHP Library
  2. [![Build Status](https://travis-ci.org/pusher/pusher-http-php.svg)](https://travis-ci.org/pusher/pusher-http-php)
  3. PHP library for interacting with the Pusher HTTP API.
  4. Register at <https://pusher.com> and use the application credentials within your app as shown below.
  5. ## Installation
  6. You can get the Pusher PHP library via a composer package called `pusher-php-server`. See <https://packagist.org/packages/pusher/pusher-php-server>
  7. ```bash
  8. $ composer require pusher/pusher-php-server
  9. ```
  10. Or add to `composer.json`:
  11. ```json
  12. "require": {
  13. "pusher/pusher-php-server": "^2.2"
  14. }
  15. ```
  16. and then run `composer update`.
  17. Or you can clone or download the library files.
  18. **We recommend you [use composer](http://getcomposer.org/).**
  19. ## Pusher constructor
  20. Use the credentials from your Pusher application to create a new `Pusher` instance.
  21. ```php
  22. $app_id = 'YOUR_APP_ID';
  23. $app_key = 'YOUR_APP_KEY';
  24. $app_secret = 'YOUR_APP_SECRET';
  25. $pusher = new Pusher( $app_key, $app_secret, $app_id );
  26. ```
  27. A fourth parameter `$options` parameter can also be passed. The available options are:
  28. * `scheme` - e.g. http or https
  29. * `host` - the host e.g. api.pusherapp.com. No trailing forward slash.
  30. * `port` - the http port
  31. * `timeout` - the HTTP timeout
  32. * `encrypted` - quick option to use scheme of https and port 443.
  33. * `cluster` - specify the cluster where the application is running from.
  34. For example, by default calls will be made over a non-encrypted connection. To change this to make calls over HTTPS use:
  35. ```php
  36. $pusher = new Pusher( $app_key, $app_secret, $app_id, array( 'encrypted' => true ) );
  37. ```
  38. *Note: The `$options` parameter was introduced in version 2.2.0 of the library.
  39. Previously additional parameters could be passed for each option, but this was
  40. becoming unwieldy. However, backwards compatibility has been maintained.*
  41. *Note: The `host` option overrides the `cluster` option!*
  42. ## Publishing/Triggering events
  43. To trigger an event on one or more channels use the `trigger` function.
  44. ### A single channel
  45. ```php
  46. $pusher->trigger( 'my-channel', 'my_event', 'hello world' );
  47. ```
  48. ### Multiple channels
  49. ```php
  50. $pusher->trigger( [ 'channel-1', 'channel-2' ], 'my_event', 'hello world' );
  51. ```
  52. ### Arrays
  53. Objects are automatically converted to JSON format:
  54. ```php
  55. $array['name'] = 'joe';
  56. $array['message_count'] = 23;
  57. $pusher->trigger('my_channel', 'my_event', $array);
  58. ```
  59. The output of this will be:
  60. ```json
  61. "{'name': 'joe', 'message_count': 23}"
  62. ```
  63. ### Socket id
  64. In order to avoid duplicates you can optionally specify the sender's socket id while triggering an event ([https://pusher.com/docs/duplicates](http://pusherapp.com/docs/duplicates)):
  65. ```php
  66. $pusher->trigger('my-channel','event','data','socket_id');
  67. ```
  68. ### JSON format
  69. If your data is already encoded in JSON format, you can avoid a second encoding step by setting the sixth argument true, like so:
  70. ```php
  71. $pusher->trigger('my-channel', 'event', 'data', null, false, true)
  72. ```
  73. ## Authenticating Private channels
  74. To authorise your users to access private channels on Pusher, you can use the socket_auth function:
  75. ```php
  76. $pusher->socket_auth('my-channel','socket_id');
  77. ```
  78. ## Authenticating Presence channels
  79. Using presence channels is similar to private channels, but you can specify extra data to identify that particular user:
  80. ```php
  81. $pusher->presence_auth('my-channel','socket_id', 'user_id', 'user_info');
  82. ```
  83. ### Presence example
  84. First set this variable in your JS app:
  85. ```php
  86. Pusher.channel_auth_endpoint = '/presence_auth.php';
  87. ```
  88. Next, create the following in presence_auth.php:
  89. ```php
  90. <?php
  91. if (isset($_SESSION['user_id'])) {
  92. $stmt = $pdo->prepare("SELECT * FROM `users` WHERE id = :id");
  93. $stmt->bindValue(':id', $_SESSION['user_id'], PDO::PARAM_INT);
  94. $stmt->execute();
  95. $user = $stmt->fetch();
  96. } else {
  97. die('aaargh, no-one is logged in')
  98. }
  99. header('Content-Type: application/json');
  100. $pusher = new Pusher($key, $secret, $app_id);
  101. $presence_data = array('name' => $user['name']);
  102. echo $pusher->presence_auth($_POST['channel_name'], $_POST['socket_id'], $user['id'], $presence_data);
  103. ```
  104. Note: this assumes that you store your users in a table called `users` and that those users have a `name` column. It also assumes that you have a login mechanism that stores the `user_id` of the logged in user in the session.
  105. ## Application State Queries
  106. ### Get information about a channel
  107. ```php
  108. $pusher->get_channel_info( $name );
  109. ```
  110. It's also possible to get information about a channel from the Pusher REST API.
  111. ```php
  112. $info = $pusher->get_channel_info('channel-name');
  113. $channel_occupied = $info->occupied;
  114. ```
  115. For [presence channels](https://pusher.com/docs/presence_channels) you can also query the number of distinct users currently subscribed to this channel (a single user may be subscribed many times, but will only count as one):
  116. ```php
  117. $info = $pusher->get_channel_info('presence-channel-name', array('info' => 'user_count'));
  118. $user_count = $info->user_count;
  119. ```
  120. If you have enabled the ability to query the `subscription_count` (the number of connections currently subscribed to this channel) then you can query this value as follows:
  121. ```php
  122. $info = $pusher->get_channel_info('presence-channel-name', array('info' => 'subscription_count'));
  123. $subscription_count = $info->subscription_count;
  124. ```
  125. ### Get a list of application channels
  126. ```php
  127. $pusher->get_channels()
  128. ```
  129. It's also possible to get a list of channels for an application from the Pusher REST API.
  130. ```php
  131. $result = $pusher->get_channels();
  132. $channel_count = count($result->channels); // $channels is an Array
  133. ```
  134. ### Get a filtered list of application channels
  135. ```php
  136. $pusher->get_channels( array( 'filter_by_prefix' => 'some_filter' ) )
  137. ```
  138. It's also possible to get a list of channels based on their name prefix. To do this you need to supply an $options parameter to the call. In the following example the call will return a list of all channels with a 'presence-' prefix. This is idea for fetching a list of all presence channels.
  139. ```php
  140. $results = $pusher->get_channels( array( 'filter_by_prefix' => 'presence-') );
  141. $channel_count = count($result->channels); // $channels is an Array
  142. ```
  143. This can also be achieved using the generic `pusher->get` function:
  144. ```php
  145. $pusher->get( '/channels', array( 'filter_by_prefix' => 'presence-' ) );
  146. ```
  147. ### Get user information from a presence channel
  148. ```php
  149. $response = $pusher->get( '/channels/presence-channel-name/users' )
  150. ```
  151. The `$response` is in the format:
  152. ```php
  153. Array
  154. (
  155. [body] => {"users":[{"id":"a_user_id"}]}
  156. [status] => 200
  157. [result] => Array
  158. (
  159. [users] => Array
  160. (
  161. [0] => Array
  162. (
  163. [id] => a_user_id
  164. )
  165. /* Additional users */
  166. )
  167. )
  168. )
  169. ```
  170. ### Generic get function
  171. ```php
  172. $pusher->get( $path, $params );
  173. ```
  174. Used to make `GET` queries against the Pusher REST API. Handles authentication.
  175. Response is an associative array with a `result` index. The contents of this index is dependent on the REST method that was called. However, a `status` property to allow the HTTP status code is always present and a `result` property will be set if the status code indicates a successful call to the API.
  176. ```php
  177. $response = $pusher->get( '/channels' );
  178. $http_status_code = $response[ 'status' ];
  179. $result = $response[ 'result' ];
  180. ```
  181. ## Debugging & Logging
  182. The best way to debug your applications interaction with server is to set a logger
  183. for the library so you can see the internal workings within the library and interactions
  184. with the Pusher service.
  185. You set up logging by passing an object with a `log` function to the `pusher->set_logger`
  186. function:
  187. ```php
  188. class MyLogger {
  189. public function log( $msg ) {
  190. print_r( $msg . "\n" );
  191. }
  192. }
  193. $pusher->set_logger( new MyLogger() );
  194. ```
  195. If you use the above example in code executed from the console/terminal the debug
  196. information will be output there. If you use this within a web app then the output
  197. will appear within the generated app output e.g. HTML.
  198. ## Running the tests
  199. Requires [phpunit](https://github.com/sebastianbergmann/phpunit/).
  200. * Got to the `tests` directory
  201. * Rename `config.example.php` and replace the values with valid Pusher credentials **or** create environment variables.
  202. * Some tests require a client to be connected to the app you defined in the config;
  203. you can do this by opening https://app.pusher.com/apps/<YOUR_TEST_APP_ID>/api_access in the browser
  204. * Execute `phpunit .` to run all the tests.
  205. ## Framework Integrations
  206. - **Laravel 4** - https://github.com/artdarek/pusherer
  207. - **Laravel 5** - https://github.com/vinkla/pusher
  208. ## License
  209. Copyright 2014, Pusher. Licensed under the MIT license:
  210. http://www.opensource.org/licenses/mit-license.php
  211. Copyright 2010, Squeeks. Licensed under the MIT license:
  212. http://www.opensource.org/licenses/mit-license.php