Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events never dispatched? #40

Closed
BonBonSlick opened this issue Mar 5, 2021 · 2 comments
Closed

Events never dispatched? #40

BonBonSlick opened this issue Mar 5, 2021 · 2 comments

Comments

@BonBonSlick
Copy link

BonBonSlick commented Mar 5, 2021

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 <dunglas@gmail.com>
 */
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?

@BonBonSlick
Copy link
Author

Well, after debugging it was ASYNC issue for messanger bus from symfony bundle


framework:
    messenger:
        transports:
            sync: 'sync://'

        routing:
            'Symfony\Component\Mercure\Update': sync

Why it was failing silently and throwing nothig, never invoking JWTTokenProvide do not know and have no time right now to debug.
Still, right now caddy server responds with 401 for that token generated in JWTProvider, need to debug it further.

@BonBonSlick
Copy link
Author

BonBonSlick commented Mar 5, 2021

Okay, that was fixed too by removing private option for Update

            $update = new Update(
                $topic,
                $eventTopicData
// here TRUE will make update private
            );

Now update is sent, but never accepted on UI with mentioned in SYmfony docs EventSourcePolyfill, polyfill just sends requests again and again but never streams events to susbcribers.
From logs we can see that events are sent to mercure hub and idspatched to topice subscribers.
https://symfony.com/doc/current/mercure.html#authorization
":"http.handlers.mercure","msg":"Update sent","subscriber":{"id":"urn:uuid:b5b53747-1567-4704-bc6b-b2841eadf34c"
Yaffle/EventSource#163
Yaffle/EventSource#79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant