You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the following code, I cannot establish websocket connections:
importFastifyfrom"fastify";importwsPluginfrom"fastify-websocket";exportconstserver=Fastify();server.register(wsPlugin);server.get("/",{websocket: true},(ws,req)=>{console.log(req);ws.socket.on("message",(msg)=>{console.log(msg.toString());ws.socket.send("hi from server");});});if(process.env.NODE_ENV==="production"){server.listen(3000);}
if(process.env.NODE_ENV==='production'){constserver=app.listen(3000,()=>console.log(`server run on: http://localhost:3000`))if(process.env.WS==='true'){// a check I added to make sure there is WS handler we want to add in.addWebSocketHandler(server)}}
As for ViteDevServer,
I think this is a good way to add it only need to add support in ViteNodeConfig for that.
like adding new key called wsPath like the appPath key in the config.
That we can call the function from the file provided in the config and add it to vite dev server although I fairly new to vite so I do not know how to accomplish that.
The WS library also doesn't work. For now I've been using the below work around which uses nodemon to build and run the app on file change. This is obviously not ideal because I lose out on HMR, and have to run the app in production mode locally, but its better than having to manually restart Vite everytime I make a change.
Here is the work around, replace your npm run dev with the following command: nodemon --exec \"vite build && node dist/main.js\" -e ts --ignore dist/
Just like the viteNodeApp export key, there can be added another export default key that receives the web server built by vite. Then the user code can be do anything with it.
Of course, the function should be called every time the HMR runs.
I have the same issue. In order for websockets to work the server needs to handle upgrade request, and since vite uses it for live reload, our own ws implementation does not work. I think vite recieves the upgrade event and it does not propograte to our code. It may be very hard to solve this issue, as rewriting vite's behavior is a no go.
Vite is an absolute blessing upon js and ts community, and bringing it to the server is the next step. Typescript project setup is always a pain, and this project solves all the issues.
I had a similar problem when using vite-plugin-node, koa and socket-io and kinda made it work. My solution looks like this (I use the express adapter because it expects a plain http server unlike the koa adapter apparently):
// src/main.tsimportKoafrom'koa';import{createServer}from'http';import*asconstantsfrom'@/constants';constapplication=newKoa();constserver=createServer(application.callback());constio=newSocketIoServer(server,{/* options */});io.on('connection',(socket)=>{// do somethin});// Workaround to allow websockets on development using vite-plugin-nodeexportconstserverListener=server.listeners('request')[0];if(constants.common.ENVIRONMENT==='production'){server.listen(3000);console.log('Running application on http://localhost:3000');}
I haven't been able to establish connections to ws://localhost:3000 yet, but I can now use the polling transport of socket-io on the dev server, which I previously could not do at all, so I suspect my issue is a different one. Hope someone finds a use for this!
// Add Web Socket Configuration
await app.register(require('@fastify/websocket'), {
options: {
maxPayload: 1048576,
server: app.server
},
});
it is working well in local. once I deployed and configured SSL then it is not working
Activity
axe-me commentedon Oct 21, 2021
hi @chanced thank you for reporting this. I have to figure out a way to support websocket
chanced commentedon Oct 21, 2021
@axe-me no problem. Thanks for publishing the package.
I'm just going to roll with esbuild for that package. It makes more sense anyway. I figured I'd let you know, regardless.
dhkatz commentedon Nov 13, 2021
Websockets don't seem to work with Express as well
shaif-dorIT commentedon Feb 17, 2022
I think it work in production at least for me, I used express.js and socket.io for example (I took the base code from YT called The net ninja repo link: https://github.com/iamshaunjp/websockets-playlist).
what I did is:
As for ViteDevServer,
I think this is a good way to add it only need to add support in ViteNodeConfig for that.
like adding new key called wsPath like the appPath key in the config.
That we can call the function from the file provided in the config and add it to vite dev server although I fairly new to vite so I do not know how to accomplish that.
jordanranson commentedon Feb 26, 2022
The WS library also doesn't work. For now I've been using the below work around which uses nodemon to build and run the app on file change. This is obviously not ideal because I lose out on HMR, and have to run the app in production mode locally, but its better than having to manually restart Vite everytime I make a change.
Here is the work around, replace your
npm run devwith the following command:nodemon --exec \"vite build && node dist/main.js\" -e ts --ignore dist/datyin commentedon Mar 28, 2022
Any news on this issue?
francipvb commentedon Jul 3, 2022
Hllo,
I think this is not too difficult to work on.
Just like the
viteNodeAppexport key, there can be added another export default key that receives the web server built by vite. Then the user code can be do anything with it.Of course, the function should be called every time the HMR runs.
francipvb commentedon Jul 3, 2022
Not sure if it can be done, but here is my suggestion.
[-]Websockets don't seem to work, at least with Fastify[/-][+]Websockets do not work[/+]romanzy313 commentedon Jul 17, 2022
I have the same issue. In order for websockets to work the server needs to handle upgrade request, and since vite uses it for live reload, our own ws implementation does not work. I think vite recieves the upgrade event and it does not propograte to our code. It may be very hard to solve this issue, as rewriting vite's behavior is a no go.
Vite is an absolute blessing upon js and ts community, and bringing it to the server is the next step. Typescript project setup is always a pain, and this project solves all the issues.
In the meantime use nodemon.
Hope this can be resolved someday.
Shyam-Chen commentedon Oct 7, 2022
For a workaround:
nodemon+vite-nodepackage.json:vite.config.ts:My template is here.
svicalifornia commentedon Jan 17, 2023
@Shyam-Chen What's the benefit of running
vite-nodeif you're just going to restart it vianodemonwhenever any of the source files changes?Shyam-Chen commentedon Jan 18, 2023
@svicalifornia No benefit of
vite-nodeinnodemon. I just want to support WebSockets in development.Shyam-Chen commentedon Feb 3, 2023
Ref: vitest-dev/vitest#2334
Use
vite-node -w src/server.tsReload is faster than
nodemon.srmagura commentedon May 3, 2023
Probably not necessary, but I created another minimal repro of this problem before I found this issue.
Feel free to use it for testing: https://github.com/srmagura/ws-test-vite
daleal commentedon May 15, 2023
I had a similar problem when using
vite-plugin-node,koaandsocket-ioand kinda made it work. My solution looks like this (I use theexpressadapter because it expects a plainhttpserver unlike thekoaadapter apparently):I haven't been able to establish connections to
ws://localhost:3000yet, but I can now use thepollingtransport ofsocket-ioon the dev server, which I previously could not do at all, so I suspect my issue is a different one. Hope someone finds a use for this!tamilv commentedon Feb 25, 2024
anyone please help me