Docs used
https://symfony.com/doc/current/mercure.html#configuration
https://mercure.rocks/spec
https://caddyserver.com/docs/json/apps/http/servers/routes/handle/mercure/
https://mercure.rocks/docs/hub/install
###> symfony/mercure-bundle ###
MERCURE_PUBLISH_URL=https://remote.mercure.api/mercure
MERCURE_ALLOW_ANONYMOUS=1
MERCURE_ADDR=:3000
MERCURE_LISTENED_URI=https://remote.mercure.api/.well-known/mercure
MERCURE_SUBSCRIPTIONS_URI=https://remote.mercure.api/.well-known/mercure/subscriptions
MERCURE_CORS_ALLOWED_ORIGINS=symfchat.loc
MERCURE_PUBLISH_ALLOWED_ORIGINS=symfchat.loc
MERCURE_ACCESS_CONTROL_ALLOW_ORIGIN=http://127.0.0.1
ACCESS_CONTROL_ALLOW_ORIGIN=http://127.0.0.1
#MERCURE_ACCESS_CONTROL_ALLOW_ORIGIN=symfchat.loc
SUBSCRIPTIONS_URI=https://remote.mercure.api.com/.well-known/mercure
MERCURE_SUBSCRIPTIONS=1
MERCURE_PUBLISHER_JWT_KEY=ABC
MERCURE_SUBSCRIBER_JWT_KEY=DEFG
App\Mercure\JwtProvider:
arguments:
$secret: '%env(MERCURE_PUBLISHER_JWT_KEY)%'
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Serializer\SerializerInterface;
...
$key = $_ENV['MERCURE_PUBLISHER_JWT_KEY'];
$jwt = JWT::encode([], $key);
$eventTopicData = $this->serializer->serialize([
'event' => 'new_message',
'topic' => $topic,
'jwt' => $jwt,
'Authorization' => 'Bearer ' . $jwt,
'payload' => ['test'],
], "json");
$update = new Update(
$topic,
$eventTopicData,
true // false does not work too
);
$this->messageBus->dispatch($update);
<?php
namespace App\Mercure;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key;
class JwtProvider
{
private $secret;
public function __construct(string $secret)
{
$this->secret = $secret;
// this one called
}
public function __invoke(): string
{
// but never triggered, no errors
return (new Builder())
->withClaim('mercure', ['publish' => []])
->getToken(new Sha256(), new Key($this->secret));
}
}
It is built somewhere here during symfony bundle init, but it is never invoked!
/**
* @author Kévin Dunglas <[email protected]>
*/
final class MercureExtension extends Extension
{
caddyfile config
{
auto_https disable_redirects
}
caddy.tamilchatz.com
file_server
log {
output stdout
format json
}
route {
mercure {
transport_url local://local
publisher_jwt ABC
subscriber_jwt DEFG
cors_origins *
anonymous
subscriptions
}
caddy logs, we see only new susbcribers and handling GET requests
"level":"info","ts":1614956486.2640092,"logger":"http.handlers.mercure","msg":"New subscriber","subscriber":{"id":"urn:uuid:fa88b1d0-e24f-4b48-a416-88a3cc36b7bf","last_e>
"level":"info","ts":1614956486.2656085,"logger":"http.handlers.mercure","msg":"New subscriber","subscriber":{"id":"urn:uuid:7929b9f7-c4eb-44fe-a3c6-22403dd4d7bf","last_e>
"level":"info","ts":1614956486.2683454,"logger":"http.handlers.mercure","msg":"New subscriber","subscriber":{"id":"urn:uuid:95f8180f-2f01-4cd4-9f52-cc0fd2de15d7","last_e>
level":"info","ts":1614956486.570968,"logger":"http.log.access.log0","msg":"handled request","request":{"
,"request":{"remote_addr":"ip-123123","proto":"HTTP/2.0","method":"OPTIONS","host":"remove.mercure.api","uri":"/.well-known/mercure/subscriptions/test"
framework:
messenger:
transports:
# https://symfony.com/doc/current/messenger.html#transport-configuration
async: '%env(MESSENGER_TRANSPORT_DSN)%'
routing:
'Symfony\Component\Mercure\Update': async
###> symfony/messenger ###
MESSENGER_TRANSPORT_DSN=redis://localhost:6379/messages
MESSENGER_FAILED_TRANSPORT_DSN=redis://127.0.0.1:6379/messages_failed
MESSENGER_NUMBER_OF_WORKER=4
###< symfony/messenger ###
So, from what I understand, something wrong with dispatching events.
It happened after upgrading legacy mercure binary to the latets build with caddy GO lang server and moving it to separate server.
Subscriptions seems to be working, but events are never dispatched now.
Spent a lot of time trying to figure out is this mercure, caddy or symfony bundle, or transport (Redis) issue.
But because JwtToken is never applied, could be smth wrong with the bundle during invocation or dispatching.
Caddy server does not receives any updates, post requests according to logs.
Tell me please what could be the issue?