-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EADDRINUSE with WebSockets on file change #2903
Comments
This is because of HMR in that endpoint that re-creates the websocket server whenever you update it. Ideally, you'd want to close and restart the server whenever you make updates. Try adding a HMR handler at the end of your file: if (import.meta.hot) {
import.meta.hot.dispose(() => {
server.close() // or something similar
))
} Check the spec for more info. |
I updated the repo, but the server still crashes after a change in the file. |
My bad. You'd need to have Something like this should work: if (import.meta.hot) {
import.meta.hot.accept();
import.meta.hot.dispose(() => {
server.close();
});
} |
Unfortunately it still crashes when I reload the browser after a change. Repo updated again. |
Hmm. If so I might be out of ideas, but last I tried that code, it worked for me locally. |
The underlying issue is vitejs/vite#7887. It's possible to work around it, if you can hold your nose: if (globalThis.__server) {
globalThis.__server.close();
}
const server = new WebSocketServer({ port: 8080 });
globalThis.__server = server; Obviously you can wrap the relevant bits in |
Yeah the Vite devs are on crack. Do not |
Describe the bug
I'm trying to get WebSockets to work with SvelteKit. However, whenever I change the file the WebSocket server is created, the dev server crashes and says that the port is taken. So it appears like 2 servers are running at once at some point or the port remains open for some reason. I feel that this is not intended behaviour because if I restart the server the port is not taken anymore until I make a change in the file again.
Reproduction
Repository: https://github.com/Volper212/sveltekit-report
Steps:
git clone https://github.com/Volper212/sveltekit-report
cd sveltekit-report
npm i
npm run dev
src/routes/index.js
in any way that causes a refresh (for example add a semicolon on line 2)Logs
System Info
Severity
annoyance
Additional Information
No response
The text was updated successfully, but these errors were encountered: