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

[Mercure] Explain how to differentiate between multiple topics in JavaScript #20574

Open
ThomasLandauer opened this issue Jan 16, 2025 · 3 comments
Labels
hasPR A Pull Request has already been submitted for this issue. Mercure

Comments

@ThomasLandauer
Copy link
Contributor

At https://symfony.com/doc/current/mercure.html#subscribing it is described how to subscribe to multiple topics:

const eventSource = new EventSource("{{ mercure(['topic1', 'topic2']) }}");

But when looking at the received data, how is it possible to know which is coming from topic1 and which from topic2?

At https://stackoverflow.com/a/71905295/1668200 it is suggested to use Update()s fifth argument $type, and in JavaScript add multiple event listeners to EventSource . Is this really the recommended way? If yes, then the recommendation would be to use the (more or less) same string for $topic and $type?

@dunglas
Copy link
Member

dunglas commented Jan 16, 2025

As stated in the Mercure spec, the recommended way is to embed the topic in the payload.

For instance, if you use an hypermedia format such as Atom, HTML or JSON-LD, you can just add the topic in an id attribute in document.
This also works for other formats such as JSON or YAML, although the key to use to store the topic will not be standardized and will be up to you.

@ThomasLandauer
Copy link
Contributor Author

OK, I added that at #20580

But what is $type used then for? Which other values are possible besides the default(?) message?

@dunglas
Copy link
Member

dunglas commented Jan 17, 2025

It's to customize the SSE types. This allows to trigger ad-hoc JS events instead of the generic message one: https://developer.mozilla.org/en-US/docs/Web/API/EventSource

ThomasLandauer added a commit to ThomasLandauer/mercure-1 that referenced this issue Jan 17, 2025
Info is taken from symfony/symfony-docs#20574 (comment)

Why is the default value `null` and not `message`?
@xabbuh xabbuh added the hasPR A Pull Request has already been submitted for this issue. label Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hasPR A Pull Request has already been submitted for this issue. Mercure
Projects
None yet
Development

No branches or pull requests

4 participants