laupifrpar/pusher-bundle

A Pusher bundle for Symfony2

Installs: 47 257

Dependents: 1

Stars: 38

Watchers: 3

Forks: 22

Open Issues: 3

Type: symfony-bundle

1.2.8 2016-03-09 18:51 UTC

README

Build Status SensioLabsInsight Latest Stable Version Latest Unstable Version Bitdeli Badge

This bundle let you use Pusher simply.

Pusher (Documentation) is a simple hosted API for adding realtime bi-directional functionality via WebSockets to web and mobile apps, or any other Internet connected device. It's super powerful, and a ton of fun!

This bundle is under the MIT license.

Installation

Use composer to install this bundle.

composer require laupifrpar/pusher-bundle

Then update your AppKernel.php file to register the new bundle:

// in app/AppKernel::registerBundles()

$bundles = array(
    // ...
    new Lopi\Bundle\PusherBundle\LopiPusherBundle(),
    // ...
);

Configuration

If you do not have a Pusher account, sign up and make a note of your API key before continuing.

General

To start, you'll need to setup a bit of configuration:

This is the default configuration in yml:

# app/config/config.yml
lopi_pusher:
    app_id: <your_app_id>
    key: <your_key>
    secret: <your_secret>

    # Default configuration
    debug: false # true if you want use the debug of all requests
    host: http://api.pusherapp.com
    port: 80
    timeout: 30

    # optional if you want to use private or presence channels
    # see the section about "Private and Presense channel auth" below
    auth_service_id: <the_auth_service_id>

By default, calls will be made over a non-encrypted connection. To change this to make calls over HTTPS, simply:

# app/config/config.yml
lopi_pusher:
    # ...

    host: https://api.pusherapp.com
    port: 443

Usage!

Once you've configured the bundle, you will have access to a lopi_pusher.pusher service. From inside a controller, you can use it like this:

public function triggerPusherAction()
{
    /** @var \Pusher $pusher */
    $pusher = $this->container->get('lopi_pusher.pusher');

    $data['message'] = 'hello world';
    $pusher->trigger('test_channel', 'my_event', $data);

    // ...
}

The lopi_pusher.pusher returns an instance of the \Pusher class from the official Pusher SDK. You can find out all about it on pusher's documentation.

Private and Presence channel authentication (optional)

If you'd like to use private or presence, you need to add an authorization service.

First, create an authorization service that implements Lopi\Bundle\PusherBundle\Authenticator\ChannelAuthenticatorInterface:

<?php
// src/AppBundle/Pusher/ChannelAuthenticator.php

namespace AppBundle\Pusher

use Lopi\Bundle\PusherBundle\Authenticator\ChannelAuthenticatorInterface;

class ChannelAuthenticator implements ChannelAuthenticatorInterface
{
    public function authenticate($socketId, $channelName)
    {
        // logic here

        return true;
    }
}

Next, register it as service like normal:

# app/config/services.yml
services:
    my_channel_authenticator:
        class: AppBundle\Pusher\ChannelAuthenticator
        arguments: []

Then include its service id in the lopi_pusher auth_service_id configuration parameter:

# app/config/config.yml
lopi_pusher:
    # ...

    auth_service_id: my_channel_authenticator

Additionally, enable the route by adding the following to your app\config\routing.yml configuration:

# app\config\routing.yml
lopi_pusher:
    resource: "@LopiPusherBundle/Resources/config/routing.xml"
    prefix:   /pusher

In some Symfony configurations, you may need to manually specify the channel_auth_endpoint: (not required in most setups):

{# app/Resources/views/base.html.twig #}

<script type="text/javascript">
    Pusher.channel_auth_endpoint = "{{ path('lopi_pusher_bundle_auth') }}";
</script>

Reporting an issue or a feature request

Issues and feature requests are tracked in the Github issue tracker.

When reporting a bug, it may be a good idea to reproduce it in a basic project built using the Symfony Standard Edition to allow developers of the bundle to reproduce the issue by simply cloning it and following some steps.