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
declare(strict_types=1);
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\Attribute\Route;
class PublishController extends AbstractController
{
#[Route('/publish', name: 'publish')]
public function index(): JsonResponse
{
$notification = new Notification(
topics: ['http://localhost/books/2'],
data: ['message' => 'new public event'],
);
$publisher = new Publisher(
mercureHubUrl: 'http://localhost:3000/.well-known/mercure',
tokenProvider: new PublisherTokenProvider('your-very-secret-key-at-least-32-chars'),
client: new Psr18Client(),
);
$publisher->send($notification);
return new JsonResponse(['success']);
}
}
declare(strict_types=1);
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\Attribute\Route;
class PublishController extends AbstractController
{
#[Route('/publish', name: 'publish')]
public function index(): JsonResponse
{
$notification = new PrivateNotification(
topics: ['http://localhost/author/ntavelis/books/155'],
data: ['message' => 'new private event'],
);
$publisher = new Publisher(
mercureHubUrl: 'http://localhost:3000/.well-known/mercure',
tokenProvider: new PublisherTokenProvider('your-very-secret-key-at-least-32-chars'),
client: new Psr18Client(),
);
$publisher->send($notification);
return new JsonResponse(['success']);
}
}
declare(strict_types=1);
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\Attribute\Route;
class SubscribeController extends AbstractController
{
#[Route('/subscribe', name: 'subscribe', methods: ['POST'])]
public function index(Request $request): JsonResponse
{
$content = json_decode($request->getContent(), true);
$topic = $content['topic'];
// TODO: authorize the request before issuing a token
$provider = new SubscriberTokenProvider('your-very-secret-key-at-least-32-chars');
$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.