1

I try to integrate Mercure to symfony 5 project. And there are two trouble.

  1. I try to send update to private, but it's not work, but if try to send to not private everything works.

Command that run mercure:

./bin/mercure --jwt-key='homesphere_secret_token' --addr='localhost:3000' --allow-anonymous --cors-allowed-origins='*'

.env file

MERCURE_PUBLISH_URL=http://localhost:3000/.well-known/mercure
MERCURE_JWT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdfX0.P0f5r123SLTru4DiE4X9q0EIoKahds-nI8jpo8uKKQQ
MERCURE_SECRET_KEY=homesphere_secret_token

Backend that gives url and jwt token

    $user = $this->getUser();

    $hubUrl = $this->getParameter('mercure.default_hub');
    $link = new Link('mercure', $hubUrl);

    $token = JWT::encode([
        'mercure' => [
            'publish' => [sprintf("/chat/%s", $user->getId())],
        ],
    ], $this->getParameter('mercure_secret_key'));

Update code

    $message = $serializer
        ->serialize(['message' => $message], 'json');
    $update = new Update(
        sprintf("/chat/%s", $user->getId()),
        $message,
        true // if false then work
    );
    $publisher($update);

Frontend part

const hub = new URL('http://localhost:3000/.well-known/mercure');
hub.searchParams.append('topic', '/chat/1');
const eventSource = new EventSourcePolyfill(hub.href, {
  'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiXC9jaGF0XC8xIl19fQ.yg3DodPjaPzWVIOhKMo30xXzS3L4oPckbL9pcA4tMck'
});
eventSource.onmessage = event => {
  console.log(JSON.parse(event.data));
}
  1. When frontend receives message by update then next requests start rising CORS trouble image

could anyone help me with these, please?

1 Answers1

1

Try with --cors-allowed-origins='http://localhost:8000'