-
I would like to tell my clients to go to a different page by sending websocket messages. Is there a way to do this? I was thinking of sending a div that pulls a url that has the HX-Redirect header, but hopefully there is a cleaner way to do this |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey, you could probably do it with a simple event listener, using either wsSource.addEventListener("htmx:wsAfterMessage", function (event) {
if (event.detail.message === "redirect") {
document.location.href = "/my/url"
}
}) Or you could for example define a custom syntax like a Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hey, you could probably do it with a simple event listener, using either
htmx:wsAfterMessage
orhtmx:wsBeforeMessage
, and the nativelocation.href
orlocation.replace
depending on your usecase.Example:
Or you could for example define a custom syntax like a
redirect:
prefix then the target URL, for your JS to parse, or send a JSON with the target URL... Likely many ways to implement it!Hope this helps!