PHPackages                             jul6art/push-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. jul6art/push-bundle

ActiveSymfony-bundle[Mail &amp; Notifications](/categories/mail)

jul6art/push-bundle
===================

Symfony real-time notification bundle

v1.0.16(5y ago)323MITPHPPHP ^7.4

Since Jan 12Pushed 3y ago1 watchersCompare

[ Source](https://github.com/jul6art/push-bundle)[ Packagist](https://packagist.org/packages/jul6art/push-bundle)[ Docs](https://github.com/jul6art/push-bundle)[ RSS](/packages/jul6art-push-bundle/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (10)Versions (18)Used By (0)

 [![logo dev in the hood](https://github.com/jul6art/symfony-skeleton/raw/master/assets/img/devinthehood.png?raw=true)](https://devinthehood.com)

 [![License](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT) [![Version](https://camo.githubusercontent.com/b373f73d3edbc464e7b6a2be59f6d0476f3cb93d119f8686e2b26cb6cd3b6cdc/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d737461626c65266d6573736167653d76312b636f6d696e672b736f6f6e26636f6c6f723d6f72616e6765)](https://github.com/jul6art/symfony-skeleton)

jul6art/push-bundle
===================

[](#jul6artpush-bundle)

Symfony real-time notification bundle
-------------------------------------

[](#symfony-real-time-notification-bundle)

> ⚠️ Work in progress so keep calm. The good news: this is maintained!

Requirements
------------

[](#requirements)

- **php ^7.4 || ^8.0**
- **symfony ^4.4 || ^5.0**
- **mercure**

Installation
------------

[](#installation)

```
composer require jul6art/push-bundle
```

Then Download the [mercure hub](https://github.com/dunglas/mercure/releases/tag/v0.3.3) depending on your operating system and install it in the root of your project. For each release, the assets section list operating systems implementations. The folder must contain the mercure bin. Rename this folder **mercure**.

EventSource Polyfill
--------------------

[](#eventsource-polyfill)

```
npm install event-source-polyfill
```

and import it on client side to make push works on IE and Edge

Generate new JWT token (Optionnal)
----------------------------------

[](#generate-new-jwt-token-optionnal)

Go to [jwt.io](http://jwt.io) and put your future mercure secret key (default it's **!ChangeMe!**) in the **verify signature** textarea and this array in the **payload** textarea

```
{
    "mercure": {
        "publish": []
    }
}
```

Because the array is empty, the Symfony app will only be authorized to publish public updates (see the [authorization](https://symfony.com/doc/current/mercure.html#authorization) section of symfony/mercure-bundle for further information).

THen store the generated token in your .env file as **MERCURE\_JWT\_TOKEN** parameter

Start mercure server
--------------------

[](#start-mercure-server)

The default token is signed with the secret key: !ChangeMe!

CORS\_ALLOWED\_ORIGINS is the client URL and port. It can be \* or a list of domains ADDR is the server url and 3000 is the port for mercure server

```
JWT_KEY='!ChangeMe!' ADDR='localhost:3000' ALLOW_ANONYMOUS=1 CORS_ALLOWED_ORIGINS="http://localhost:80" ./mercure/mercure
```

> ⚠️ By default, push messages are async so you need to launch a crawler in a terminal to dequeue messages and send it

```
bin/console messenger:consume async_priority_high --time-limit 600
```

Using with api-platform
-----------------------

[](#using-with-api-platform)

Server side

```
/**
 * @ApiResource(mercure=true)
 */
class SomeTopic {}
```

Client side

```
import {EventSourcePolyfill} from "../polyfills/Polyfills";

export default class MercureProvider {
    provide = () => {
        const publishUrl = new URL('http://publish.url:3000/hub');
        publishUrl.searchParams.append("topic", "/some_topic");
        publishUrl.searchParams.append("topic", "/some_topic/{id}");

        const es = new EventSourcePolyfill(publishUrl, {
            headers: {
                'Authorization': 'Bearer ' + YOUR_MERCURE_JWT_TOKEN
            }
        });
        es.onmessage = e => {
            const data = JSON.parse(e.data);

            const regex = /\/api\/(?\w+)\//gm;

            const match = regex.exec(data['@id']);

            if (null !== match) {
                const event = new CustomEvent(match.groups.type, { "data": data });
                document.dispatchEvent(event);
            }

        };
    }
};

// somewhere else
document.addEventListener('...', function() {
  // what you need
});
```

Using without api-platform
--------------------------

[](#using-without-api-platform)

Server side

```
use Jul6Art\PushBundle\Service\Traits\PusherAwareTrait;

/**
 * Class RequestEventSubscriber
 */
class SomeService
{
    use PusherAwareTrait;

    public function function(): void
    {
        $this->pusher->push('/some/topic', ['test' => true]);
    }
}
```

Sync (Optionnal)
----------------

[](#sync-optionnal)

```
push:
    async: false
```

Other messenger messages (Optionnal)
------------------------------------

[](#other-messenger-messages-optionnal)

```
push:
    routing:
        'PathToSomeAsyncMessage': async_priority_high
```

Can be **async\_priority\_high** or **async\_priority\_low** or **sync**

Asyncable Annotation (Optionnal)
--------------------------------

[](#asyncable-annotation-optionnal)

My Entity

```
/**
 * @ORM\Entity(repositoryClass=MyClassRepository::class)
 * @Asyncable(eventClass="App\Event\MyClassEvent")
 */
class MyClass
{

}
```

My EntityEvent

```
