CakePHP Pusher
, (*1)
A CakePHP plugin for interaction with the Pusher API., (*2)
Installation
Include the following in your composer.json file:, (*3)
{
"require": {
"tanuck/pusher": "dev-master"
}
}
Note: This plugin should install in your Plugin
directory rather than the composer vendors directory., (*4)
Configuration
Firstly, you must load the plugin:, (*5)
CakePlugin::load('CakePusher');
Include the component in your controller:, (*6)
class MyController extends AppController {
public $components = array(
'CakePusher.Pusher' => array(
'auth_key' => 'PUSHER_APP_AUTH_KEY',
'secret' => 'PUSHER_APP_SECRET',
'app_id' => 'PUSHER_APP_ID',
),
);
}
This is the most basic form taken by the Component constructor, for further configuration options see the Usage section below., (*7)
Then access the Component like so:, (*8)
$this->Pusher->trigger('my-channel', 'my-event', 'My Message');
Usage
If you wish to use just the one Pusher app in your CakePHP application, then the $components
definition above will be enough. The options available mirror those passed to the Pusher API constructor (see here form more information)., (*9)
The plugin allows you to configure and use multiple Pusher apps with the Component. This can be done by nesting the configuration arrays in the component settings, the array index of each will become the internal alias to each Pusher app. For example:, (*10)
class MyController extends AppController {
public $components = array(
'CakePusher.Pusher' => array(
'main' => array(
'auth_key' => 'PUSHER_APP_AUTH_KEY',
'secret' => 'PUSHER_APP_SECRET',
'app_id' => 'PUSHER_APP_ID',
),
'otherApp' => array(
'auth_key' => 'PUSHER_APP_AUTH_KEY',
'secret' => 'PUSHER_APP_SECRET',
'app_id' => 'PUSHER_APP_ID',
),
),
);
}
NOTE: When using more than one Pusher instance you must specifiy a main
app., (*11)
You can interact with the default main
app using the methods directly on the component or you can specify an app to use with the with
method:, (*12)
$this->Pusher->trigger('my-channel', 'my-event', 'My Message');
$this->Pusher->socketAuth('my-channel', '1234.1234');
$this->Pusher->presenceAuth('my-channel', '1234.1234', 'user-12', true);
$this->Pusher->get('/channels');
$this->Pusher->with('otherApp')->trigger('my-channel', 'my-event', 'My Message');
You can get and set the name of the default Pusher app like so:, (*13)
$this->Pusher->getDefault();
$this->Pusher->setDefault('otherApp');
And you can add additional app configurations on the fly:, (*14)
$this->Pusher->addAppConfig('myAppName', array(
'auth_key' => 'PUSHER_APP_AUTH_KEY',
'secret' => 'PUSHER_APP_SECRET',
'app_id' => 'PUSHER_APP_ID',
));
License
cakephp-pusher is offered under an MIT license., (*15)