htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Trigger refresh/redirect after SSE message

Open andybarilla opened this issue 1 year ago • 3 comments

I'm trying to convert my current basic javascript websockets implementation to HTMX's SSE. Is there a way to trigger a redirect or page refresh after SSE messages? There's the occasional event for which the standard swapping of content would not work for in my use case.

andybarilla avatar Jan 07 '24 23:01 andybarilla

You can probably add a JS event listener for the htmx:sseMessage event and trigger a page refresh based on that.

neelrr1 avatar Jan 10 '24 08:01 neelrr1

Thanks!

For those searching:

<div hx-ext="sse" sse-connect="/events" sse-swap="refresh"
    _="on htmx:sseMessage location.replace(location.href)" />

Note: This does result in the body of the refresh message displaying in the div before the page refreshes.

andybarilla avatar Jan 11 '24 04:01 andybarilla

I have another problem with the redirect.

After a JWT token is expired a server is sending the 307 Redirect response. It turned out, that an sse-handler can't interpret normal responses and keep trying to reestablish connection while receieving 307 again and again.

It would be great to handle the case and trigger redirectin with a location in a header.

4l1fe avatar Jan 31 '24 01:01 4l1fe

I have another problem with the redirect.

After a JWT token is expired a server is sending the 307 Redirect response. It turned out, that an sse-handler can't interpret normal responses and keep trying to reestablish connection while receieving 307 again and again.

It would be great to handle the case and trigger redirectin with a location in a header.

Did you work out a way to resolve this? I was wondering whether to try and send through a HX-REDIRECT header in my response too. I'm getting the same issue when JWT expires.

ImTheDeveloper avatar Jul 04 '24 12:07 ImTheDeveloper