ux icon indicating copy to clipboard operation
ux copied to clipboard

[Turbo] Add `mercure()` to `turbo_stream_listen()`

Open seb-jean opened this issue 1 year ago • 5 comments

Hi,

For generate the URL of the Hub and for automatically set the cookie required to use the authorization mechanism, we need Twig helper mercure() (PR: https://github.com/symfony/mercure/pull/62)

But how to use it with turbo_stream_listen()?

seb-jean avatar May 21 '24 09:05 seb-jean

Yes, I have the same problem. In dev mercure is working fine but in production I can't use it. I'm getting 401.

Guervyl avatar May 30 '24 00:05 Guervyl

Same problem/question here.

gremo avatar Sep 01 '24 18:09 gremo

Could you guys set up a small reproducer app, with everything needed to test (like an example you need to work that does not today) ?

smnandre avatar Sep 01 '24 21:09 smnandre

@smnandre I'll try probably tomorrow.

I'll try to explain better what I think @seb-jean is asking.

In short, mercure() helper does one important thing: it setup the cookie needed to subscribe to a topic (when authorization is needed, i.e. Mercure doesn't allow anonymous subscribers):

<script>
  const eventSource = new EventSource("{{ mercure('https://example.com/books/1', { subscribe: 'https://example.com/books/1' })|escape('js') }}", {
      withCredentials: true
  });
</script>

As you can see withCredentials is passed to the EventSource object. CORS and the cookie make the subscribe process work.

On the other hand, turbo_stream_listen does not set any cookie and does not support passing withCredentials to the Stimulus controller (let alone that the controller itself doesn't support options for the EventSource object).

So, what I think he is asking (and what I think it's needed is):

  • turbo_stream_listen should set the authorization cookie (is this possible?!)
  • turbo_stream_listen should support passing with withCredentials
  • The Stimulus controller should be modified too

See https://github.com/symfony/ux/pull/1774

gremo avatar Sep 01 '24 21:09 gremo

Yes, that's exactly it, plus the ability to subscribe to multiple topics.

seb-jean avatar Sep 02 '24 08:09 seb-jean