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

feature request: trigger an SSE start with the hx-trigger #27

Open
GrimalDev opened this issue Jun 2, 2024 · 5 comments
Open

feature request: trigger an SSE start with the hx-trigger #27

GrimalDev opened this issue Jun 2, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@GrimalDev
Copy link

Hello,

I found in the documentation that it is possible to trigger http callbacks on a specific sse event.
But I do not find anything (and don't seem to be able to get it to work) about using the hx-trigger tag to start sse requests on a user event (like a click to start the server sent events).

forgive me if I missed something in the documentation, but it isn't clear to me why in all my tests, my sse event are just triggered out of the box on page load.

Thank a lot for your help all.

@Telroshan
Copy link
Collaborator

Hello, I'm not sure to understand the issue, sorry if that's off the mark;

using the hx-trigger tag to start sse requests on a user event (like a click to start the server sent events)

It looks like you want to send messages from the client to the server, but please note SSE (Server sent events), is unidirectional, and can only happen from the server to the client.
If you want bidirectional communication, you'll probably be more interested in the websockets extension, which will allow you to send messages from your client to your server.

it isn't clear to me why in all my tests, my sse event are just triggered out of the box on page load.

That sounds weird, SSE events shouldn't trigger unless your server sends those very SSE events in the first place. Would you mind sharing some code to investigate on what's happening here?

@GrimalDev
Copy link
Author

GrimalDev commented Jun 3, 2024

Hello, I'm not sure to understand the issue, sorry if that's off the mark;

using the hx-trigger tag to start sse requests on a user event (like a click to start the server sent events)

It looks like you want to send messages from the client to the server, but please note SSE (Server sent events), is unidirectional, and can only happen from the server to the client. If you want bidirectional communication, you'll probably be more interested in the websockets extension, which will allow you to send messages from your client to your server.

it isn't clear to me why in all my tests, my sse event are just triggered out of the box on page load.

That sounds weird, SSE events shouldn't trigger unless your server sends those very SSE events in the first place. Would you mind sharing some code to investigate on what's happening here?

Thank you for your reply!
I can understand the confusion here, I'm gonna simplify. The http request to the see endpoint is triggered on page load. I cannot get to send the request on a button click (for example).

@Telroshan
Copy link
Collaborator

Telroshan commented Jun 3, 2024

Ooh ok I get it, you want to initiate the SSE connection upon user interaction.

Is there any reason not to have it initialize the SSE connection automatically on page load though? You could just have it active & idle, until you actually send events from your server.

Other than that, there's no current built-in way to do this that I know of, but you could achieve this by

  • If your click makes an htmx request, your endpoint could include a <div sse-connect="/url"> element in its response, which would initiate the SSE connection once swapped in by htmx. If it's a pure client side interaction though, and your button doesn't fetch anything from the server, ofc you can forget this one.
  • With a little custom JS snippet, add the sse-connect attribute on the go, and call htmx.process on the element to have it initialize the SSE connection

The same ideas apply if you want to instead act on the hx-ext attribute, which you could use to say enable the SSE extension altogether on click (note this will affect all events, not just the connection initialization).

Basically, sse-connect is an attribute that instructs to initiate a SSE connection to the given endpoint, so if you don't want the SSE connection on load, remove this attribute, or avoid having a hx-ext="sse" parent element, to later on introduce any of those 2 when you want it to happen

@GrimalDev
Copy link
Author

Ooh ok I get it, you want to initiate the SSE connection upon user interaction.

Is there any reason not to have it initialize the SSE connection automatically on page load though? You could just have it active & idle, until you actually send events from your server.

Other than that, there's no current built-in way to do this that I know of, but you could achieve this by

  • If your click makes an htmx request, your endpoint could include a <div sse-connect="/url"> element in its response, which would initiate the SSE connection once swapped in by htmx. If it's a pure client side interaction though, and your button doesn't fetch anything from the server, ofc you can forget this one.
  • With a little custom JS snippet, add the sse-connect attribute on the go, and call htmx.process on the element to have it initialize the SSE connection

The same ideas apply if you want to instead act on the hx-ext attribute, which you could use to say enable the SSE extension altogether on click (note this will affect all events, not just the connection initialization).

Basically, sse-connect is an attribute that instructs to initiate a SSE connection to the given endpoint, so if you don't want the SSE connection on load, remove this attribute, or avoid having a hx-ext="sse" parent element, to later on introduce any of those 2 when you want it to happen

Thank you for your detailed explanation, I will do something like adding the sse-connect tag on button click. But still, It seems like a lot of effort for something that could be a feature. Of course, it would take time, maybe i'll look at making a code change proposition for this edge case. But thank you for your time.

@Telroshan Telroshan added the enhancement New feature or request label Jul 10, 2024
@trafflux
Copy link

I had this same issue and I do not like the solution proposed. sse_connect should be tied to an event to be consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants