Home > OS >  connecting react frontend and express backend using socket.io
connecting react frontend and express backend using socket.io

Time:01-16

I have one frontend made in React, running on port 3000 and one express/node server running on port 5000, both of them on the same computer (no network used).

I try to make them communicate using socket.io for the backend and socket.io-client for the frontend.

The connexion works somehow, but I have some error message in the frontend console, namely :

Access to XMLHttpRequest at 'http://localhost:5000/socket.io/?EIO=4&transport=polling&t=OMmsufE' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
polling.js:334          

GET http://localhost:5000/socket.io/?EIO=4&transport=polling&t=OMmsufE net::ERR_FAILED 200

here's the front end code, App.js :

import './App.css';

function App() {
  const socket = require('socket.io-client')('http://localhost:5000/socket.io/');

  socket.on('connect', () => {
    console.log('Connected to server');

    socket.on('message', (message) => {
      console.log('Received message:', message);
    });

    socket.emit('message', 'Hello, server!');
  });

socket.on('disconnect', () => {
  console.log('Disconnected from server');
});

  return (
    <div className="App">App 
    </div>
  );
}

and the backend index.js :

const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

const cors = require("cors")
app.use(cors({ origin: 'http://localhost:3000' }));
io.on('connection', (socket) => {
  console.log('User connected');

  socket.on('disconnect', () => {
    console.log('User disconnected');
  });

  socket.on('message', (message) => {
    console.log('Received message:', message);
    io.emit('message', message);
  });
});
app.use("/socket.io/", (req, res, next) => {
  next()
})

app.get("/", (req, res) => console.log("hello"))

app.use(cors())

server.listen(5000, () => {
  console.log('Server listening on port 5000');
});

Anyone having an idea ?

CodePudding user response:

I'm pretty sure that your socket.io endpoint is blocking the true endpoint

Client:

// remove `socket.io` here
const socket = require('socket.io-client')('http://localhost:5000/');

Server:

const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

const cors = require("cors")
app.use(cors({ origin: 'http://localhost:3000' }));
io.on('connection', (socket) => {
  console.log('User connected');

  socket.on('disconnect', () => {
    console.log('User disconnected');
  });

  socket.on('message', (message) => {
    console.log('Received message:', message);
    io.emit('message', message);
  });
});

app.get("/", (req, res) => console.log("hello"))

app.use(cors())

server.listen(5000, () => {
  console.log('Server listening on port 5000');
});

CodePudding user response:

so, indeed, as Konrad said, the problem came from the frontend, and especially this line :

const socket = require('socket.io-client')('http://localhost:5000/socket.io');

which was necessary to turn into, as Konrad said :

const socket = require('socket.io-client')('http://localhost:5000/);

but also, it was necessary to add :

const socket = require('socket.io-client')('http://localhost:5000/', {transports: ['websocket'], upgrade: false});

and now it works

  • Related