WebSocket connection failed

Hi guys, someone can help me with this problem?
when i push my project into balenaOS, i receive this errror.
i try put the ip defined in compose-yml but i receve the same error, then i try with the id from balena scan.
WebSocket connection to ‘ws://34880ba.local:4000/graphql’ failed: WebSocket is closed before the connection is established

this is my code

import { ApolloClient, InMemoryCache } from ‘@apollo/client’;

import { WebSocketLink } from ‘@apollo/client/link/ws’;

import { split, HttpLink } from ‘@apollo/client’;

import { getMainDefinition } from ‘@apollo/client/utilities’;

const http = new HttpLink({

uri: 'http://172.25.0.88:4000/graphql'

});

// window.location.protocol === ‘https:’ ? ‘wss’ : ‘ws’

const protocol = window.location.protocol === ‘https:’ ? ‘wss://’ : ‘ws://’;

const wsLink = new WebSocketLink({

uri: `${protocol}34880ba.local:4000/graphql`,

options: {

    reconnect: true,

}

});

const splitLink = split(

({ query }) => {

    const definition = getMainDefinition(query);

    return (

        definition.kind === 'OperationDefinition' &&

        definition.operation === 'subscription'

    );

},

wsLink,

http,

);

const client = new ApolloClient({

link: splitLink,

cache: new InMemoryCache()

});

export default client;

Hi there, this look like an application error. Can you share your docker-compose file? As a general rule, if you are trying to do inter-service comms you can use the service names as the hostname. So say you have service1 and service2 you should be able to reach service2 on port 4000 from service1 by using http://service2:4000. This is provided you use the default network mode and not network_mode: host

this is my compose file.

version: “2.1”

services:

client-write-aws:

container_name: client-write-aws

restart: always

build:

  context: client-write-aws

  dockerfile: Dockerfile.template

volumes:

  - "client-aws-cfg:/usr/src/app/client-write-aws/cfg"

  - "client-aws-log:/usr/src/app/client-write-aws/log"

  - "client-aws-sta:/usr/src/app/client-write-aws/sta"

  - "client-aws-crt:/usr/src/app/client-write-aws/certificates"

depends_on:

  - serverclp

networks:

  default:

    ipv4_address: 172.25.0.26

serverclp:

container_name: serverclp

restart: always

build:

  context: serverclp

  dockerfile: Dockerfile.template

volumes:

  - "clp-log:/usr/src/app/serverclp/log"

  - "clp-cfg:/usr/src/app/serverclp/cfg"

  - "clp-sta:/usr/src/app/serverclp/sta"

networks:

  default:

    ipv4_address: 172.25.0.22

ports:

  - 9091:9091

depends_on:

  - mosquittobroker

mosquittobroker:

container_name: broker-auth

restart: always

build:

  context: broker-auth

  dockerfile: Dockerfile

volumes:

  - "db-mosquitto:/mosquitto/data"

  - "auth-plugin:/mosquitto/config"

ports:

  - "1883:1883"

  - "9883:9883"

environment:

  - DOMAIN=example.local

  - PORT=8080

labels:

  - "local.example.description=Mosquitto v1.6.9 MQTT server"

networks:

  default:

    ipv4_address: 172.25.0.20

depends_on:

  - mongodb

mongodb:

container_name: mongodb

restart: always

image: arm64v8/mongo:4

networks:

  default:

    ipv4_address: 172.25.0.45

volumes:

  - "mongo-data:/data/db"

  - "mongo-config:/etc/mongod.conf"

ulimits:

  nproc: 990000

  nofile:

    soft: 990000

    hard: 990000

ports:

  - "27017:27017"

lactechfront:

container_name: lactechfront

restart: always

build:

  context: lactech_front

  dockerfile: Dockerfile

ports:

  - "80:80"

stdin_open: true

networks:

  default:

    ipv4_address: 172.25.0.89

depends_on:

  - mongodb

lactechback:

container_name: lactechback

restart: always

build:

  context: lactech_backend

  dockerfile: Dockerfile

ports:

  - "4000:4000"

stdin_open: true

networks:

  default:

    ipv4_address: 172.25.0.88

depends_on:

  - mongodb    

client-read-aws:

container_name: client-read-aws

restart: on-failure

build:

  context: client-read-aws

  dockerfile: Dockerfile.template

volumes:

  - "clientread-aws-cfg:/usr/src/app/client-read-aws/cfg"

  - "clientread-aws-log:/usr/src/app/client-read-aws/log"

  - "clientread-aws-sta:/usr/src/app/client-read-aws/sta"

  - "clientread-aws-crt:/usr/src/app/client-read-aws/certificates"

networks:

  default:

    ipv4_address: 172.25.0.3

depends_on:

  - "serverclp"

volumes:

client-aws-cfg:

client-aws-log:

client-aws-sta:

client-aws-crt:

clientread-aws-cfg:

clientread-aws-log:

clientread-aws-sta:

clientread-aws-crt:

db-mosquitto:

auth-plugin:

mongo-config:

mongo-data:

clp-log:

clp-cfg:

clp-sta:

networks:

default:

ipam:

  driver: default

  config:

    - subnet: 172.25.0.0/24

i think i understandwhat you tell me!
i just need to use the service at the locale of ip, correctly?

hi again, my problem is this error.

client.ts:557 Mixed Content: The page at ‘COVID’ was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint ‘ws://172.25.0.88:4000/graphql’. This request has been blocked; this endpoint must be available over WSS.

Hello @Morais is this already working? How did you solve your issue?

Hello @mpous no, i don’t resolve this problem yet.

Can you help me?

Morais,

I assume from the error message you’re seeing, that your lactechfront service is now managing to speak to your lactechback service - which is good. You were correct that Tomas was saying to use the service name in place of the IP address.

What I think is happening now, is that you are calling the frontend service using HTTPS, but the backend is only using WS. This is a downgrade in security, which the client browser isn’t going to let happen. If you call the lactechback service with WSS, it should work.

From a balena-standpoint I think everything is now working OK. Good luck with the rest of your project. :slight_smile:

Phil