2

I can't implement any subscriptions because it suddenly disconnects from it when I try to listen to some endpoint with GraphQL Playground:

{"error": "Could not connect to websocket endpoint wss://localhost:4000/graphql. Please check if the endpoint url is correct."}

I'm using ApolloServer alone, no express or anything else. It is containerized with Docker using node14 image, the port is properly fowarded, queries and mutations works properly.

This is the configuration snippet:

const server = new ApolloServer({
    typeDefs: mergedTypeDefs,
    resolvers: mergedResolvers,
    playground: {
        subscriptionEndpoint: 'ws://localhost:4000/graphql'
    },
    subscriptions: {
        keepAlive: 9000,
        onConnect: (connParams, webSocket, context) => {
            console.log('CLIENT CONNECTED');
        },
        onDisconnect: (webSocket, context) => {
            console.log('CLIENT DISCONNECTED')
        }
    },
    context: {
        models
    }
});

I tried everything, from using 'wss' instead of 'ws' to change the path. I checked for typos and didn't find one. What bothers me is that the paths are the same so It should at least try to notify me by the onConnect or onDisconnect but it doesn't.

This is the message through Chrome's dev tools:

WebSocket connection to 'wss://localhost:4000/graphql' failed: WebSocket is closed before the connection is established.

I tested subscriptions with a 'tutorial' project outside the container and it works fine.

Sometimes, the only function of subscriptions that works is onDisconnect but after 2-5 seconds after receiving the error message on PlayGround, and Still it doesn't gives me any insight on the problem.

Any help or suggestion is appreciated.

xadm
  • 8,219
  • 3
  • 14
  • 25
Masterhand83
  • 51
  • 1
  • 5

2 Answers2

0

wss will definitely not work locally without certificate, so use ws as a protocol.

If it works without Docker, then everything should be fine code-wise.

You should also make sure that you've mapped ports correctly, i.e. exposed port 4000 https://docs.docker.com/engine/reference/commandline/run/#publish-or-expose-port--p---expose

hp10
  • 602
  • 6
  • 11
  • Outside the container it stays for 30 seconds (max timeout?) then closes the connection, but it recieves the socket message and works as expected. Subscriptions works inside the container but it is a really weird case because the connections instantly closes before I'm even able to do something. I checked the port and it should be fowarded. I mean, queries and mutations work inside the container, playground and subscriptions are the weir part – Masterhand83 Jun 16 '21 at 04:58
  • Could you post your Dockerfile and running command? Have you tried to check docker logs? Also, try to put 0.0.0.0 instead of localhost as an address. – hp10 Jun 16 '21 at 09:27
  • I'm using a remote container generated by vscode, I checked docker logs and it doesn't show anything besides "docker initialized". I tried putting 0.0.0.0 as 'subscriptionEndpoint" for playground, it seems that sometimes it works after one fail and other it just doesn't work. I'll add my docker config in post. – Masterhand83 Jun 21 '21 at 04:28
0

Did you configure and start a websocket server ?

Like here https://github.com/MichalLytek/type-graphql/blob/master/examples/simple-subscriptions/index.ts

Oreste Viron
  • 3,592
  • 3
  • 22
  • 34