Next.js Discord

Discord Forum

CORS error with socket.io

Unanswered
Naeemgg posted this in #help-forum
Open in Discord
I have a Tauri + next app that uses socket.io, and in dev, it's like the web apps so my CORS origin array is like this
cors:{
        origin:["http://localhost:3000","http://127.0.0.1:3000"],
        methods:['GET', 'POST']
    }

but when I build the app it gives CORS errors in the console. So I just want to know if there is a way to find out from where the connection is being requested in socket.io so I'll just update the array with that URL

20 Replies

so do I need to send and receive some headers?
like this
const io = require("socket.io")(httpServer, {
  cors: {
    origin: "https://example.com",
    methods: ["GET", "POST"],
    allowedHeaders: ["my-custom-header"],
    credentials: true
  }
});
// server-side
const io = require("socket.io")(httpServer, {
  cors: {
    origin: "https://example.com",
    methods: ["GET", "POST"],
    allowedHeaders: ["my-custom-header"],
    credentials: true
  }
});

// client-side
const io = require("socket.io-client");
const socket = io("https://api.example.com", {
  withCredentials: true,
  extraHeaders: {
    "my-custom-header": "abcd"
  }
});
Ok let me try that
as of now I'm just setting the origin and methods
not allowedHeaders
hey one confusion is still there
what should I add...
Original message was deleted
yeah thats the answer
Ohk
Original message was deleted
AND IT WORKED
:all_the_things:
so I'll do something like this
process.env.NODE_ENV==="production"?'tauri...':'http://localhost...'
that should work right
Original message was deleted
 cors:{
        origin: ["https://tauri.localhost","http://localhost:3000","http://127.0.0.1:3000"],
    methods: ["GET", "POST"],
    credentials: true
    }
its working perfectly fine with both dev and prod builds
Original message was deleted
"if it works just don't touch it"
~sensei senseless
Original message was deleted
😆