Answer the question
In order to leave comments, you need to log in
How do I properly map host ports and set up CORS in Docker using socket.io?
Good afternoon.
There is such a question - if the server and client are on different ports in Docker , is it really possible to forward the socket between them? There are 2 Services in
docker-compose - Nuxt frontend and Nodejs / Express / SocketIo backend . External ports are mapped to internal.
d8bface3deb4 app_frontend "npm run docker-dev" 16 minutes ago Up 16 minutes 0.0.0.0:3010->3000/tcp app_frontend
cf11cf802afc app_server "node" 16 minutes ago Up 16 minutes 0.0.0.0:4006->4000/tcp app_server Docker- composite :
version: '3.7'
services:
frontend:
container_name: app_frontend
build:
context: .
dockerfile: Dockerfile.frontend.dev
volumes:
- ./app/frontend:/var/www
working_dir: /var/www
ports:
- 3010:3000
environment:
HOST: 0.0.0.0
restart: always
tty: true
server:
container_name: app_server
build:
context: .
dockerfile: Dockerfile.server.dev
volumes:
- ./app/server:/var/www
working_dir: /var/www
ports:
- 4006:4000
environment:
PORT: 4000
export default function({ store }) {
Vue.use(
new VueSocketIO({
debug: false,
connection: 'http://localhost:4006',
vuex: {
store,
actionPrefix: 'SOCKET_',
mutationPrefix: 'SOCKET_'
}
})
)
}
const express = require('express');
const app = express();
const server = require('http').Server(app);
Access to XMLHttpRequest at 'http://localhost:4006/socket.io/?EIO=3&transport=polling&t=N5XdK-N' from origin 'http://localhost:3010' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:3000' that is not equal to the supplied origin.
// const allowFrontPort = 3000;
// const allowFrontPortCORS = 3000;
GET http://localhost:4006/socket.io/?EIO=3&transport=polling&t=N5Xdgm1 404 (Not Found)
// const allowFrontPort = 3010;
// const allowFrontPortCORS = 3010;
Access to XMLHttpRequest at 'http://localhost:4006/socket.io/?EIO=3&transport=polling&t=N5XdtEk' from origin 'http://localhost:3010' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:3000' that is not equal to the supplied origin.
// const allowFrontPort = 3010;
// const allowFrontPortCORS = 3000;
GET http://localhost:4006/socket.io/?EIO=3&transport=polling&t=N5Xe1ov 404 (Not Found)
const allowFrontPort = 3000;
const allowFrontPortCORS = 3010;
const io = require('socket.io')(server, {origins: `http://localhost:${allowFrontPort}`});
const cors = require('cors');
const Users = require('./Users')();
const port = 4000;
app.use(cors({
origin: `http://localhost:${allowFrontPortCORS}`,
credentials: true
}));
io.on('connection', socket => {
...
});
socket.on('createMessage', (data, cb) => {
...
});
socket.on('userLeft', (id, cb) => {
...
});
socket.on('disconnect', () => {
...
})
});
app.get('/', function (req, res) {
res.send('We are happy to see you!');
});
app.listen(port, () => console.log(`Server running... (port=${port})`));
const io = require('socket.io')(server, {origins: '*:*'});
app.use(cors());
Answer the question
In order to leave comments, you need to log in
This solution turned out to be working:
https://medium.com/@msanathkumar/building-a-simple...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question