The hx-ws allows you to work with Web Sockets directly from HTML. The value of the attribute can be one or more of the following, separated by commas:

Here is an example:

  <div hx-ws="connect:/chatroom">
    <div id="chat_room">
    <form hx-ws="send">
        <input name="chat_message">

This example establishes a WebSocket to the chatroom end point. Content that is sent down from the websocket will be parsed as HTML and swapped in by the id property, using the same logic as Out of Band Swaps.

The form uses the send syntax to indicate that when it is submitted, the form values should be serialized as JSON and send to the nearest enclosing WebSocket.

The serialized values will include a field, HEADERS, that includes the headers normally submitted with an htmx request.

After an unexpected connection loss due to Abnormal Closure, Service Restart or Try Again Later, reconnecting is tried until successful. The default reconnection interval is implemented with the full-jitter exponential-backoff algorithm. Own implementations can be provided by setting htmx.config.wsReconnectDelay to a function with retryCount as its only parameter.