htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Trigger HTMX with `server-sent-events`when event and data correspond to specefic Data

Open gungun974 opened this issue 2 years ago • 1 comments

I've been extensively using HTMX in my project, and it has greatly simplified many aspects of my backend development, as well as contributing positively to my mental health. My primary use case involves the server-sent-events extension, which I leverage to listen for server events and trigger automatic refetch of page content for the users. (While I'm aware that SSE can directly swap HTML, in my project, it's more efficient to reload a fragment of the page)

Everything works seamlessly, but I've encountered a limitation when trying to listen for specific events like edit. I want to check if the data from the event matches a specific ID.

Currently, I haven't found a way to achieve this other than creating a special event with both the name and ID inside, like edit-4562. This workaround is not ideal, and the data field of the event is not used.

I'm curious if others have encountered similar use cases and whether it's feasible to add functionality for comparing the data of an event with a predefined value.

Personally I think a syntax like hx-trigger="sse:event:value" could be easy to use and maintain in the Extension but I don't know how hx-trigger handle : when already using one.

As per my tradition with FOSS projects, I'm happy to contribute to this enhancement if needed.

gungun974 avatar Dec 17 '23 16:12 gungun974

I personnally do exactly what you described, i.e. adding the ID of the item in the event name

This workaround is not ideal, and the data field of the event is not used.

What's wrong with that though? Essentially you're going to write hx-trigger="sse:edit-4562" instead of hx-trigger="sse:event:4562". And your server is going to fire an SSE event edit-4562 instead of edit with the data 4562, I get it but that's not specifically "uglier", "dirtier" or even longer to read/write, so I'm unsure what bothers you that much with this approach, as it does what you want to achieve without any changes / additional parsing logic to add to htmx to support it.

Let me know if I'm missing something there!

Telroshan avatar Dec 17 '23 20:12 Telroshan