Next.js Discord

Discord Forum

Getting CORS error when using socket.io, but only when the user disconnects and connects again.

Unanswered
Brown bear posted this in #help-forum
Open in Discord
Brown bearOP
I am not getting any errors when the frontend is already running and then i start the server. The socket connection is formed without any issues.
However, If I disconnect the frontend and start again, it gives me CORS error:
The error looks like this:
Access to XMLHttpRequest at 'http://localhost:3001/socket.io/?EIO=4&transport=polling&t=pypdmqlh' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.


This is the socket cors:
export const registerSocketServer = (server: HttpServer) => {
  const io = new Server<
    ClientToServerEvents,
    ServerToClientEvents,
    InterServerEvents,
    SocketData
  >(server, {
    cors: {
      //   origin: true,
      origin: [`${process.env.FRONTEND_URL}`, "*"],

      methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
      credentials: true,
      //   allowedHeaders: "*",
      allowedHeaders: [
        "Content-Type",
        "AccessKey",
        "st-auth-mode",
        "rid",
        "x-file-name",
        "x-start-byte",
        "x-end-byte",
        "x-total-size",
        "content-type",
        "Access-Control-Max-Age",
        "Access-Control-Allow-Origin",
        "Access-Control-Allow-Methods",
        "Access-Control-Allow-Headers",
        ...getAllCORSHeaders(),
      ],
    },

    // allow max 10mb file
    maxHttpBufferSize: 1e7,
  });

the cors config for the server is in the image

0 Replies