PHP code example of milpa / mercure

1. Go to this page and download the library: Download milpa/mercure 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/ */

    

milpa / mercure example snippets


use Milpa\Mercure\MercureService;

$mercure = new MercureService(
    hubUrl: 'https://hub.internal/.well-known/mercure',      // server-side POST target
    publicUrl: 'https://hub.example.com/.well-known/mercure', // what the browser connects to
    publisherKey: $_ENV['MERCURE_PUBLISHER_JWT_KEY'],
    subscriberKey: $_ENV['MERCURE_SUBSCRIBER_JWT_KEY'],
);

// Server-side: push an update. Mints a 60s publisher JWT, POSTs form-encoded
// topic + JSON data, throws RuntimeException on cURL failure or HTTP >= 400.
$mercure->publish('conversations/42/messages', [
    'event' => 'message.created',
    'body' => 'hello',
]);

// Browser-side: mint a 5-minute subscriber JWT scoped to the topics this
// visitor may listen to, and hand the browser the public URL to connect to.
$jwt = $mercure->generateSubscriberJwt(['conversations/42/messages']);
$publicUrl = $mercure->getPublicUrl();