I have this in server.js
//socket io config
const server = require('http').createServer(app)
const io = require('socket.io')(server)
io.on('connection', function (socket) {
socket.on('SOCKET_COMMENT_RECEIVED', ({ notification }) => {
io.emit(`SOCKET_COMMENT_RECEIVED`, notification)
})
//and many more
})
In my client (react)
import io from 'socket.io-client'
const socket = io('localhost:3001') // working in localhost
in my prod I do this checking
let socket = io('localhost:3001')
if(process.env.NODE_ENV === 'production') { socket =
io('https://api.example.com:3001')
}
Why is it so? I don't think it's cors issue because I already did
app.use(cors({
origin: true,
credentials: true
}))
my package.json deps
"socket.io": "^2.1.1",
"socket.io-client": "^2.1.1",