1. Go to this page and download the library: Download ntavelis/mercure-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ntavelis / mercure-php example snippets
namespace App\Controller;
use Ntavelis\Mercure\Messages\Notification;
use Ntavelis\Mercure\Providers\PublisherTokenProvider;
use Ntavelis\Mercure\Publisher;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class PublishController extends AbstractController
{
/**
* @Route("/publish", name="publish")
*/
public function index()
{
$notification = new Notification(['http://localhost/books/2'], ['data' => 'new public event']);
$publisher = new Publisher(
'http://localhost:3000/.well-known/mercure',
new PublisherTokenProvider('aVerySecretKey'),
new Psr18Client()
);
$publisher->send($notification);
return new JsonResponse(['success']);
}
}
namespace App\Controller;
use Ntavelis\Mercure\Messages\PrivateNotification;
use Ntavelis\Mercure\Providers\PublisherTokenProvider;
use Ntavelis\Mercure\Publisher;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class PublishController extends AbstractController
{
/**
* @Route("/publish", name="publish")
*/
public function index()
{
$notification = new PrivateNotification(
['http://localhost/author/ntavelis/books/155'],
['data' => 'new private event']
);
$publisher = new Publisher(
'http://localhost:3000/.well-known/mercure',
new PublisherTokenProvider('aVerySecretKey'),
new Psr18Client()
);
$publisher->send($notification);
return new JsonResponse(['success']);
}
}
namespace App\Controller;
use Ntavelis\Mercure\Providers\SubscriberTokenProvider;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class SubscribeController extends AbstractController
{
/**
* @Route("/subscribe", name="subscribe")
*/
public function index(Request $request)
{
$content = $request->getContent();
$contentArray = json_decode($content, true);
$topic = $contentArray['topic'];
// TODO authorize the request
$provider = new SubscriberTokenProvider('aVerySecretKey');
$token = $provider->getToken([$topic]);
return new JsonResponse(['token' => $token]);
}
}
bash
$ composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.