graphql subscription Could not connect to websocket endpoint at elastic beanstalk

Viewed 1918

I have graphql-yoga(GraphQL server) application running on Elastic Beanstalk with Application Load Balancer. I am able to do Query and Mutation. But when I tried Subscription using GraphQL playground it does not connect with the below error. { "error": "Could not connect to WebSocket endpoint wss://domainname.com/subscriptions. Please check if the endpoint URL is correct." }

I checked the app is working fine local and also in Heroku dyno. I checked the AWS CloudWatch logs. The request is logged with [0mGET /subscriptions [33m404 [0m3.860 ms - 152[0m

2 Answers

I actually was having this same issue and found it was related to how my sever port was starting and how I had my Apollo Provider configured on my FE. Note "ws" did not work and it required "wss" to work for me as well. I was not directing anything to a specific url like you are with "/subscriptions".instead im just using whatever URI Heroku/localhost provides me

Server GraphQLServer code:

const server = new GraphQLServer({
  typeDefs,
  resolvers,
  context: { pubsub, endpoint: "/" },
});

Server start code:

server.start(({ port }) => {
  console.log(`Server listening on ${port} for incoming requests`);
});

FE Apollo Provider Config

const httpLink = new HttpLink({
  uri: "https://domainName.herokuapp.com,
});

const wsLink = new WebSocketLink({
  uri: "wss://domainName.herokuapp.com/",
  options: {
    reconnect: true,
  },
});

const splitLink = split(
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === "OperationDefinition" &&
      definition.operation === "subscription"
    );
  },
  wsLink,
  httpLink
);

const client = new ApolloClient({
  link: splitLink,
  uri: "https://domainName.herokuapp.com",
  cache: new InMemoryCache({
    addTypename: false,
  }),
});

Hopefully this helps you out and gets you moving forward. took a half a day to figure out myself. I assume it may be the same situation for you just a little different for your situation. Happy coding!!!

I have figured out that the EC2 instance attached with Elastic Beanstalk has a default Nginx configuration. I have manually changed nginx.conf with

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

and it worked.More Info

Related