symfony / mercure-bundle

The MercureBundle allows to easily push updates to web browsers and other HTTP clients in the Symfony full-stack framework, using the Mercure protocol.
https://symfony.com/doc/current/mercure.html
MIT License
286 stars 20 forks source link

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

Open tamcy opened 2 years ago

tamcy commented 2 years ago

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?

frizquierdo commented 2 years ago

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