symfony/mercure-bundle

Unable to use the `mercure()` Twig function to subscribe to two different URLs at different places

Opened this issue · 1 comments

tamcy commented

Hello,

Assume that I want to subscribe to two (or more) private updates. By intuition I'd write this:

<div data-mercure-url="{{ mercure('https://example.com/user/3', { subscribe: 'https://example.com/user/3' }) }}"></div>
...
<div data-mercure-url="{{ mercure('https://example.com/books/1', { subscribe: 'https://example.com/books/1' }) }}"></div>

But doing so will cause the following exception from Symfony\Component\Mercure\Authorization::updateCookies:

An exception has been thrown during the rendering of a template ("The "mercureAuthorization" cookie for the "default hub" has already been set. You cannot set it two times during the same request.").

I am currently working around this by specifying all subscribing URLs in one of the mercure() function call:

<div data-mercure-url="{{ mercure('https://example.com/user/3', { subscribe: ['https://example.com/user/3', 'https://example.com/books/1'] }) }}"></div>
...
<div data-mercure-url="{{ mercure('https://example.com/books/1' }}"></div>

This works, but doesn't look straightforward. Is this a limitation of the current implmentation of the bundle, or am I doing it wrongly?

Hi @tamcy, the second approach is the correct way, passing an array of topics.