question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

not finding websocket with subscriptions

See original GitHub issue

Writing a simple chat application using apollo and express. Prior to this I was using subscriptions-transport-ws and graphql-server-express but there was an issue with graphiql not being able to call subscriptions due to a client error with graphiql. Because of this issue, I swapped over to apollo.

Currently, when I try to access a subscription through graphiql the client comes back and says: "Could not connect to websocket endpoint ws://localhost:6001/graphql. Please check if the endpoint url is correct." If I try to use a simple websocket client to find the websocket endpoint, it doesn’t seem to exist.

I stripped out most of the stuff from my server/index.js file to see if I could solve this problem, heres what it looks like:

import { createServer } from 'http'
import express from 'express'

import { typeDefs } from './types'
import resolvers from './resolvers'
import { ApolloServer } from 'apollo-server-express'

const PORT = process.env.PORT || 6001

const app = express()

const apollo = new ApolloServer({
  typeDefs,
  resolvers
})

apollo.applyMiddleware({ app })

const httpServer = createServer(app)

apollo.installSubscriptionHandlers(httpServer)

app.listen({ port: PORT }, () => {
  console.log(`server ready at http://localhost:${PORT}${apollo.graphqlPath}`)
  console.log(`Subscriptions ready at ws://localhost:${PORT}${apollo.subscriptionsPath}`)
})

My imports in package.json look like this:

"dependencies": {
    "apollo-server": "^2.1.0",
    "apollo-server-express": "^2.1.0",
    "bluebird": "^3.5.2",
    "body-parser": "^1.18.3",
    "compression": "^1.7.3",
    "cors": "^2.8.4",
    "express": "^4.16.4",
    "graphql": "^14.0.2",
    "graphql-playground-middleware-express": "^1.7.6",
    "graphql-subscriptions": "^1.0.0",
    "graphql-tools": "^4.0.2",
    "mongoose": "^5.3.4"
  },

Not really sure why the subscription endpoint isn’t properly being deployed through the apollo.installSubscriptionHandlers(httpServer) command.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:24

github_iconTop GitHub Comments

132reactions
tensor-programmingcommented, Oct 22, 2018

Never mind, figured it out right after I posted this.

59reactions
Hardronoxcommented, Sep 15, 2019

Had the same issue, console was showing 400 error. Adding highlighted line from this article solved problem for me. https://www.apollographql.com/docs/apollo-server/data/subscriptions/#subscriptions-with-additional-middleware

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebSocket connection to 'ws://localhost:4000/graphql' failed
Hi, I am running into an issue on my front-end (Vue 2) when trying to connect to the web socket. I've tried several...
Read more >
Using WebSocket subscriptions without queues
In this post, you can learn about the WebSocket Protocol and how to use WebSocket subscriptions without queues.
Read more >
ActionCable: Unable to find subscription with identifier
It looks like every new subscription with new 'client' and 'page_id' overwrites previous one, but I can't figure out how and why. The...
Read more >
Subscriptions and Streaming - Nethereum Documentation
IMPORTANT A subscription is tied to a web socket connection. When that socket connection is closed or broken, the subscription will no longer...
Read more >
webSocket - RxJS
When WebSocketSubject is subscribed, it attempts to make a socket connection, ... WebSocketSubject has an additional operator, not found in other Subjects.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found