htmx icon indicating copy to clipboard operation
htmx copied to clipboard

ws-connect retries multiple times

Open anuj9196 opened this issue 2 years ago • 3 comments

Using websocket like

<div hx-ext="ws" ws-connect="/ws/attempt/{{ id }}/" hx-trigger="load"></div>

However, it retries n number of times when the connection is failing.

Accordingly to my use case, I do not want the connection to reconnect or retry only once. Also in the development server, I want to completely disable retrying.

How can I do it?

anuj9196 avatar Oct 25 '23 09:10 anuj9196

By default it uses an exponential backoff algorithm, in the docs it also says you can use your own https://htmx.org/extensions/web-sockets/ Under the "Automatic Reconnection" header. If you make the delay function something like

htmx.config.wsReconnectDelay = function (retryCount) { 
return 1000000000 // retry in 1 million seconds
}

Hope this solves your problem

itepastra avatar Dec 06 '23 09:12 itepastra

@itepastra Thanks for the suggestion. However, it will still retry after 1 million seconds. I understand that this is a large value and no session can be alive for this long time. Isn't there a way to completely disable retrying or can add this feature?

anuj9196 avatar Dec 06 '23 12:12 anuj9196

Removing the following should keep htmx from retrying. If you download the ws.js file and host the modified version locally you can test it. https://github.com/bigskysoftware/htmx/blob/master/src%2Fext%2Fws.js#L261-L267

itepastra avatar Dec 07 '23 09:12 itepastra