Problem with establishing socket.io connection

I'm new to using Traefik and for the past days, I've been struggling with connecting my frontend application with my backend (NestJS) application despite having enabled sticky session, here is my compose file:

services:
  reverse-proxy:
    image: traefik:v3.1
    command: --api.insecure=true --providers.docker
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  nest-server:
    build:
      context: .
      dockerfile: Dockerfile.dev
    volumes:
      - .:/app
    env_file:
      - .env
    environment:
      - NODE_ENV=development
      - TIMESCALE_HOST=timescale-db
      - RABBITMQ_HOST=message-broker
    command: npm run start:dev
    depends_on:
      - timescale-db
      - message-broker
    deploy:
      replicas: 3
    labels:
      - "traefik.http.routers.nest-server.rule=Host(`nest-server.localhost`)"
      - "traefik.http.services.nest-server.loadBalancer.sticky.cookie.name=server_id"
      - "traefik.http.services.nest-server.loadBalancer.sticky.cookie.httpOnly=true"
  
  timescale-db:
    image: timescale/timescaledb-ha:pg16
    container_name: ec-statistics-database
    volumes:
      - ./timescale-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_DB=${POSTGRES_DATABASE}

  message-broker:
    image: rabbitmq:4.0.2
    container_name: rabbitmq
    environment:
      - RABBITMQ_DEFAULT_USER=${RABBITMQ_USERNAME}
      - RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD}

In the network tab, I mostly get 400 BAD REQUEST errors with the response body: {"code":1,"message":"Session ID unknown"}

This is an application error, not from Traefik.

Verify the cookie in browser developer tools.

Enable Traefik access log in JSON format (doc) to get more details about the request, for example which target service instance is used.

Alternatively you can use docker compose logs -f to see all your container logs at the same time.

Furthermore share your full Traefik static and dynamic config.

My personal recommendation is to use explicit Docker networks, especially if you scale in the future.

Thank you so much for you reply! It turns out to be exactly what you said (I'm facepalming so hard right now)

The problem was I forgot to set withCredentials to true on my front-end application and once I set it to true, everything is running perfectly.

io("http://nest-server.localhost", { withCredentials: true }).on(
      "connect",
      () => {
        console.log("Connected to server");
      }
    );

Once again, thank you. I will also look into docker networks and see what it does per your recommendation.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.