'WebSocket connection to 'ws://localhost:8080/socket.io/?EIO=4&transport=websocket' failed. Apply socket.io in ApolloServer nodejs/React is not working
I change from "const socketio = require("socket.io-client");" to "const socketio = require("socket.io");" but it is not working.
the third pic is Back-end and fourth is Front-end. it doesn't say the exact error is but "this.ws =" and "websocket.js:50 WebSocket connection to 'ws://localhost:8080/socket.io/?EIO=4&transport=websocket' failed: " is all.





Solution 1:[1]
you can check in https://socket.io/docs/v4/server-initialization/ i think it's:
io.on("connection",()=>{}) // on server side
if that doesn't work you can try to move all socket code before server.listen it would be something like this:
const http = require('http')
const { Server } = require('socket.io')
const app = require('./app')
const server = http.createServer(app)
const io = new Server(server, {
cors: {
origin: 'http://localhost:8080',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
},
})
io.on('connection', (socket) => {
console.log("socket",socket);
}
server.listen(process.env.PORT || 5000, () => {
console.log(`Listening on port ${process.env.PORT || 5000}`)
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
