Download the PHP package zfr/zfr-pusher without Composer
On this page you can find all versions of the php package zfr/zfr-pusher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package zfr-pusher
ZfrPusher, a Pusher PHP Library
Introduction
This is an unofficial Pusher PHP client for interacting with the REST Pusher API. Contrary to the official client, ZfrPusher is based on modern tools and with a better architecture.
Dependencies
- Guzzle library: >= 3.5
Integration with frameworks
To make this library even more easier to use, here are various libraries:
- ZfrPusherModule: this is a Zend Framework 2 module
Want to do an integration for another framework? Open an issue, and I'll open a repository for you!
Installation
We recommend you to use Composer to install ZfrPusher. Just add the following line into your composer.json
file:
Then, update your dependencies by typing: php composer.phar update
.
Tutorial
ZfrPusher is separated into two ways: the client object (ZfrPusher\Client\PusherClient
class) which allows
to execute Guzzle commands manually, and the service object (ZfrPusher\Service\PusherService
class) which is a thin
layer around the client that aims to simplify usage and error handling.
This is how you create a Pusher service:
Once you have access to the service, you can perform any operations.
Triggering events
To trigger an event to one or more channels, use the trigger
method. First parameter can either be a single channel (string),
or multiple channels (an array of strings):
trigger
method also supports a fourth parameter, which is the socket id to exclude a specific socket from receiving
the message (more information here):
Finally, trigger
method also supports a fifth parameter which is used to make an asynchronous trigger. This means
that it immediately returns to the client, without waiting for the response. By default, all trigger requests are
done synchronously:
Pusher service also provides a shortcut for doing asynchronous requests with the triggerAsync
method, as shown above:
Channel(s) information
You can fetch information about a single channel using the getChannelInfo
method, with an optional array of information
you want to retrieve (currently, Pusher API only supports user_count and subscription_count values:
You can use the method getChannelsInfo
to get information about multiple channels, optionally filtered by name. Like
getChannelInfo
, this method accepts an optional second parameter which is an array of information to retrieve.
Presence channel users
You can retrieve all the users in a presence channel user using the getPresenceUsers
method:
Authenticate private channels
To authenticate a user against a private channel, call the authenticatePrivate
method, with channel name and socket id.
This method returns an array whose key is 'auth' and whose value is the signed authentication string. It's up to you
to encode this as a JSON string (typically done in a controller in a MVC architecture) to return it to the client:
Authenticate presence channels
To authenticate a user against a presence channel, call the authenticatePresence
method, with channel name, socket id
and user data. This method returns an array that contains values for auth
and channel_data
keys. It's up to you to
encode this as a JSON string (typically done in a controller in a MVC architecture) to return it to the client:
General authentication
For ease of use, service also has a generic authenticate
method that choose the right method according to channel name:
Advanced use
Error handling
When using the Pusher service, all exceptions that may occurred are handled, so that you can easily filter Pusher
errors. All Pusher exceptions implement the ZfrPusher\Exception\ExceptionInterface
:
Service instantiate concrete exceptions based on the error status code:
ZfrPusher\Service\Exception\UnauthorizedException
: thrown when Pusher REST API returns a 401 error (not authorized).ZfrPusher\Service\Exception\ForbiddenException
: thrown when Pusher REST API returns a 403 error (when the application may be disabled, or when you have reached your messages quota).ZfrPusher\Service\UnknownResourceException
: thrown when Pusher REST API returns a 404 error (may occur when you ask information about an unknown channel, for instance)ZfrPusher\Service\RuntimeException
: thrown for any other errors.
In all cases, you can find more information about the error by calling .
Usage example:
Debug applications
In the official Pusher PHP client, you can attach a logger directly to the client through a set_logger
method. While
simple, this was a bad way of doing it as it was hard-coded into the client (and your logger had to have a log
method,
so your own logger may not have it). Furthermore, the places where logging occurred were hardcoded also.
Instead, ZfrPusher client takes advantage of an event manager to do this. For instance, let's say we want to log
every URL BEFORE the request is sent. Let's first create a subscriber. A subscriber implements the interface
Symfony\Component\EventDispatcher\EventSubscriberInterface
. In the getSubscribedEvents
method, we attach a listener
for the event request.before_send
(you can find a complete list of available hooks here):
Next, we need to attach the subscriber to the client:
And voilà, now all the URL will be logged.
Directly use the client
While the Pusher service is convenient, you may want to directly use the Pusher client instead, so that you can have better control of how requests are sent. You can do this:
When using the client directly, the exceptions thrown when errors occurred are Guzzle exceptions, not Pusher exceptions. Therefore it is harder to filter Pusher only exceptions. If you want this feature, please use the service instead, or write your own wrapper around the Pusher client.