davehowson / chat-app

MERN Stack Based Chat Application
350 stars 123 forks source link

fix: React doesn't connect to socketIO #31

Open michalismichailidis1999 opened 2 years ago

michalismichailidis1999 commented 2 years ago

Your React Application can't connect to socketIO

Tips to fix this

This is how you can connect now

const http = require("http")
const {Server} = require("socket.io");

const app = require("express")();

const server = http.createServer(app)

const io = new Server(server, {cors: {origin: 'http://localhost:3000'}});

io.on("connection", socket => {
  console.log("connected");
})

const port = process.env.PORT || 5000;

server.listen(port, () => console.log(`Listening on port ${port}`))