Home > Net >  How to setup a socket connection when you don't know the URL in advance?
How to setup a socket connection when you don't know the URL in advance?

Time:10-05

I'm trying to setup a Docker container with NGINX and NuxtJS that will be hosted in on AWS EBS. I might setup DNS for it later, but as of right now I won't know the URL in advance. Is there anyway to tell NGINX/socket.io to use address of the machine where the container is running?

I've been using 'localhost' as a placeholder value, which works fine in development on my local machine. But when hosted on EBS the app is still trying to connect to my machine on the socket's port.

CURRENT SETTINGS

nginx config:

server {
    listen 80;
    server_name example.com;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;

      proxy_pass http://nuxt:3000;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
}

socket.io (this is from a module but you can see the url is set to localhost)

io: {
        sockets: [{     
            default: true,
            name: 'mainSocket',
            url: 'http://localhost:3000'
        }],
    },

Docker compose file

version: "3.8"

services:
  nuxt:
    build: ./app
    restart: always
    ports:
      - "3000:3000"
    command:
      "npm run start"

  nginx:
    image: nginx:1.19
    ports:
      - "80:80"
    volumes:
      - ./nginx:/etc/nginx/conf.d
    depends_on:
      - nuxt

Any suggestions?

CodePudding user response:

As per user253751's comments the window.location contains data about the URL for the application. In the module I was using this value was easily accessible. That might not always be the case.

  • Related