Skip to content

Can I issue a redirect via htmx websockets? #3058

Answered by Telroshan
kwint asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, you could probably do it with a simple event listener, using either htmx:wsAfterMessage or htmx:wsBeforeMessage, and the native location.href or location.replace depending on your usecase.
Example:

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 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!

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kwint
Comment options

Answer selected by kwint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants