0

I am making a program where (to make it easier for myself) I am using different ports for different pages. I already have codespaces set up and I do not want to switch to using something else, but I still want to be able to run it however I want, like if I'm programming at home when I have access to VS Code desktop. Is there a way I can have it connect to the different ports correctly using codespaces and other things?

I have tried the following code:

HTML

let socket = io(":3001");

(that file is being hosted on port 3000)

The code worked perfectly before I started using the same server to host the clients but different servers for the server-side sockets.

I have also seen documentation on namespaces and rooms. If I need to use those, how do I use them securely and which one should I use?

1 Answer 1

0

it sounds like you have different pages served over different ports, but the issue arises when you try to have a socket connection between the client and server on different ports (e.g., the client is served on port 3000, but the WebSocket is on port 3001).

When your HTML file (client-side code) is served from one port (3000), and your WebSocket connection is on another (3001), you are essentially dealing with cross-origin requests. Most modern browsers block this by default unless properly configured.

in your socket.io file you can ocnfigure it like this

const io = require('socket.io')(server, {
  cors: {
    origin: "http://localhost:3000",
    methods: ["GET", "POST"]
  }
});

Adjust "http://localhost:3000" to the correct origin if needed

Sign up to request clarification or add additional context in comments.

1 Comment

How do I make it work with GitHub codespaces too, where there's no localhost?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.