2017 © Pedro PelĂĄez
 

symfony-bundle pusher-bundle

A Pusher bundle for Symfony2

image

laupifrpar/pusher-bundle

A Pusher bundle for Symfony2

  • Saturday, April 14, 2018
  • by laupifrpar
  • Repository
  • 5 Watchers
  • 53 Stars
  • 146,792 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 31 Forks
  • 6 Open issues
  • 21 Versions
  • 7 % Grown

The README.md

LopiPusherBundle

Bundle CI, (*1)

This bundle let you use Pusher simply., (*2)

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!, (*3)

This bundle is under the MIT license., (*4)

Installation

Use composer to install this bundle., (*5)

composer require laupifrpar/pusher-bundle

If you're not using Symfony Flex, then you will also need to enable Lopi\Bundle\PusherBundle\LopiPusherBundle in your config/bundles.php file., (*6)

<?php

return [
    // ...
    Lopi\Bundle\PusherBundle\LopiPusherBundle::class => ['all' => true],
    // ...
];

Configuration

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

General

To start, you'll need to setup a bit of configuration., (*8)

This is the default configuration in yml:, (*9)

# app/config/config.yml
lopi_pusher:
    # Default configuration
    scheme: http
    host: api.pusherapp.com
    port: 80
    cluster: us-east-1 # Change the cluster name
    timeout: 30
    debug: false # true if you want use the debug of all requests

You must set the url parameter :, (*10)

# app/config/config.yml
lopi_pusher:
    url: <scheme>://<key>:<secret>@<host>[:<port>]/apps/<app-id>

It will parse the URL and set, or replace the default value if exists, the various parameters scheme, key, secret, host, port and app_id, (*11)

Or you can set the various parameters separately:, (*12)

# app/config/config.yml
lopi_pusher:
    app_id: <app-id>
    key: <key>
    secret: <secret>

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

# app/config/config.yml
lopi_pusher:
    # ...
    scheme: https
    port: 443

If you want to use private or presence channels, set the parameter auth_service_id, (*14)

# app/config/config.yml
lopi_pusher:
    # ...
    auth_service_id: <the_auth_service_id>

See the section about "Private and Presense channel auth" below, (*15)

Usage!

Once you've configured the bundle, you will have access to a pusher service, which can be autowired by Pusher\Pusher typehint. From inside a controller, you can use it like this:, (*16)

use Pusher\Pusher;

class SampleController
{
    public function triggerPusherAction(Pusher $pusher)
    {
        // ...

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

        // ...
    }
}

This code will autowire \Pusher\Pusher class from the official Pusher SDK. You can find out all about it on pusher's documentation., (*17)

Private and Presence channel authentication (optional)

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

First, create an authorization service that implements Lopi\Bundle\PusherBundle\Authenticator\ChannelAuthenticatorInterface:, (*19)

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

namespace App\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:, (*20)

# config/services.yml
services:
    my_channel_authenticator: AppBundle\Pusher\ChannelAuthenticator

Then include its service id in the lopi_pusher auth_service_id configuration parameter:, (*21)

# config/packagers/lopi_pusher.yml
lopi_pusher:
    # ...

    auth_service_id: 'my_channel_authenticator'

Additionally, enable the route by adding the following to your config\routing.yml configuration:, (*22)

# 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):, (*23)

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






Reporting an issue or a feature request

Issues and feature requests are tracked in the Github issue tracker., (*24)

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., (*25)

The Versions

20/02 2018
19/09 2012