Fork me on GitHub
#biff
<
2023-10-06
>
Martynas Maciulevičius11:10:29

I have a question about websockets. When I try to connect via Chrome then it seems to work. I only render one element of hx-ext:

document.querySelectorAll("[hx-ext]")
NodeList [ div ]
But if I try to use Firefox then I get three retries like this:

Martynas Maciulevičius11:10:36

It doesn't happen when I try to run eelchat example app (only one socket). And it doesn't happen when I add the websocket to the parent div of the document too. It also happens 10 times if I add hx-ext to 10 items in my page. But if I add it at the top level where it's sent to rum then it opens three sockets and I'm not sure which ones are alive.

Martynas Maciulevičius11:10:40

I think I found the problem. It's the problem with oob swap that I do. I replace the whole HTML and force the websocket to reopen. But it's weird that it doesn't reopen the WS on chromium.

Martynas Maciulevičius14:10:43

It's weird that it doesn't reuse the connection. I "fixed" this bug (or at least located it for now) but the larger issue remains: How do I keep the websocket connection while the user is navigating through the page? It kind of solves the issue with release of the resources when the user goes into another part of the webpage. But then isn't it wasteful to create a lot of requests just because the user is clicking buttons? It works fine if the only part that should be responsive is the chat window. But what if I want to make every piece of data responsive? Then what? :thinking_face:

Jacob O'Bryant14:10:29

hm, that is interesting it works differently on chrome my initial thought is to use a single websocket connection per page, and then just let it reconnect when the user navigates to a new page. you'll already be doing one request to get the new page, and I'm not sure doing another websocket connection request would be that much more overhead :thinking_face: though it does seem like there should be a way to make the connection persistent across page navigation...

Martynas Maciulevičius15:10:20

I was thinking to save a client's ID in session but what if user has multiple tabs? This means I can't do it. Then I was thinking to generate a random header value somewhere in the body element but then this header isn't included in the websocket request. It is included into form requests. This may be a bug as they claim that hx-headers should be included everywhere. They're not included here. Now I think about simply using a path parameter and generating a nonce per-page. I don't want to open a connection per subscribable element on a page. This is silly. And I guess I'll have to live with reopening of websockets on page navigation (chromium also does reopen the websockets upon navigation. So behavior is not that different). P.S. I also am not sure already if behavior is really different on Chrome. It may have been my testing error. But in any way when the navigation reopens the WS then it seems like a waste :thinking_face: And the fact that I can't include headers is sad. I'll take a break from this.