fastify-socket.io icon indicating copy to clipboard operation
fastify-socket.io copied to clipboard

Not working with fastify 4.x

Open ivd opened this issue 3 years ago • 8 comments

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.

ivd avatar Jul 02 '22 12:07 ivd

Same problem

vadolasi avatar Jul 03 '22 00:07 vadolasi

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.

ivd avatar Jul 03 '22 11:07 ivd

I have same issue with node 16x and when I upgrade to node 18x, it is working. you can try

lemanh2006 avatar Jul 05 '22 03:07 lemanh2006

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'],
})

Chuiantw1212 avatar Jul 05 '22 09:07 Chuiantw1212

Can this issue be closed?

alemagio avatar Jul 13 '22 17:07 alemagio

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.

botura avatar Jul 19 '22 03:07 botura

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.

cm-cm-cm-cm avatar Aug 12 '22 12:08 cm-cm-cm-cm

server.listen({ port: PORT as number, host: '0.0.0.0' }) Fixed the issue

abhishek-butola avatar Sep 16 '22 08:09 abhishek-butola