fastify-socket.io
fastify-socket.io copied to clipboard
Not working with fastify 4.x
Tried the attached example, but have errors
Route GET:/socket.io/socket.io.js not found
Even when I trying to get access to /socket.io/ I got the same 404 response, but response must be like
{"code": 0, "message": "Transport unknown"}
With fastify 3 everything is ok.
Same problem
I found a temporary solution: request to 127.0.0.1 gives correct response, but to localhost gives 404. Looks like its fastify problem or misconfig.
I have same issue with node 16x and when I upgrade to node 18x, it is working. you can try
In my case, all socket.io connection request responded with code 404 following by the CORS error in browser console.
I downgrade fastify to 3.* and added transports: ['websocket'] to client and resolved the issue.
Server package.json
"dependencies": {
"@fastify/autoload": "^5.0.0",
"@fastify/cors": "^7.0.0",
"@fastify/env": "^3.0.0",
"@fastify/formbody": "^6.0.1",
"@fastify/sensible": "^4.1.0",
"fastify": "^3.29.1",
"fastify-cli": "^4.2.0",
"fastify-plugin": "^3.0.0",
"socket.io": "^4.5.1"
},
Client Code
import { io } from "socket.io-client"
const socket = io(import.meta.env.VITE_APP_API_DEFAULT, {
transports: ['websocket'],
})
Can this issue be closed?
I don't why, but:
- request to 127.0.0.1 works with node 16
- request to localhost works with node 18
I think this issue can be closed as this may not be related to the plugin.
I think fastify doesn't listen on 0.0.0.0 on default since version 4 anymore. Try adding that and it should work again.
server.listen({ port: PORT as number, host: '0.0.0.0' }) Fixed the issue