apteryxxyz / next-ws

Add support for WebSockets in Next.js app directory.
https://npmjs.com/next-ws
131 stars 9 forks source link

socket unexpectedly closed with code 1005 #29

Open FlynnHillier opened 3 months ago

FlynnHillier commented 3 months ago

I am using next v14.1.1 and next-ws v1.0.1

I am using next-ws's built in WebSocketProvider component and useWebSocket hook, in order to establish and reference a websocket connection. I have come to find that when inspecting dev tools i can see that 2 websocket connections are established when the WebSocketProvider component is accessed. This is understandable as I am in development and as such react strictmode is enabled - however by inspecting chrome dev tools' network > messages tab I can see that both connections remain open as they continue to receive data from my server. image image\ This is not in itself breaking for me, as my server can handle a user having multiple open socket connections, although I do wonder if it is intentional and perhaps not the case that only one connection should remain open and instead some useEffect clean-up callback is missing somewhere in next-ws?

The breaking issue that I am facing is that seemingly only when a certain client component of mine is loaded, does one of these two socket connections unexpectedly close with code 1005 very shortly after opening.

This is what I typically see when in my server log's when I load the page (and WebSocketProvider establishes websocket connections) image

However, (seemingly) if and only if I then include the afermentioned client component do I sometimes receive this instead. image

As is shown, very shortly after the socket connects to the server, it unexpectedly closes, with code 1005.

Now, unsuprisingly the socket stops receiving further communication from my server, and as such my application cannot function as intended as it is missing the required socket connection. (The socket that remains active has its event listeners cleaned up by my useEffect hook).

image image

I should also note that when the page is refreshed (and the sockets close as I believe they should), the server logs them as closing with code 1001 like so: image

I have no idea as to why this socket is closing, I am not super familiar with websockets so the error is likely a fault of my own and not the packages, however the fact that it does not happen consistently makes me even more bewildered as to what the cause might be. Any help would be appreciated, even if they are just suspicions, I am happy to try anything.

I will link the files in my project that I believe to be of note: