K
K
Kneepy2021-09-11 10:49:30
Node.js
Kneepy, 2021-09-11 10:49:30

Not connecting to sockets?

Customer:

import { io } from "socket.io-client";
io('ws://127.0.0.1:5000/')

Server:
const app = express()
const server = require('http').createServer(app)
const socketIO = require('socket.io')
const io = new socketIO.Server(server)

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

The API is located at 127.0.0.1:5000 and an error occurs when trying to connect to a socket at this address
GET http://127.0.0.1:5000/socket.io/?EIO=4&transport=polling&t=NlJrC6b net::ERR_CONNECTION_REFUSED

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kneepy, 2021-09-11
@Kneepy

the error was related to CORS and some parameters needed to be specified:

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question