apteryxxyz / next-ws

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

Unable to connect to WebSocket server using provided example #7

Closed Alex-Mastin closed 8 months ago

Alex-Mastin commented 9 months ago

I'm using Next.js 13.4.10. I created a project with create-t3-app and set up the project to follow the example provided in the README. Unfortunately I'm unable to get it working.

This warning is in the console on page load:

WebSocket connection to 'ws://localhost:3000/api/ws' failed: WebSocket is closed before the connection is established.

If I try pushing the button to send a message to the server, I get this error:

WebSocket connection to 'ws://localhost:3000/api/ws' failed: 
Alex-Mastin commented 9 months ago

The warning can be solved by making a small change in client/context.tsx

useEffect(() => {
  if (instance?.readyState === 1) {
    return () => instance?.close();
  }
}, []);
apteryxxyz commented 8 months ago

I've just published next-ws@0.2.7, which fixes this issue.