Graphql subscription

I have followed GraphQL Subscriptions tutorials, Here is a code sandbox version of it (Ignore value of JWT_TOKEN and WORKSPACE_ID)

I get error Invalid WebSocket implementation provided and I have tried with different version of dependencies. When I remove webSocketImpl I can establish websocket connection but when I send subscription request I have Error: Socket closed with event 1011 Internal Error error

One of my teammate has a successful experience with subscriptions-transport-ws as it is no longer actively maintained I don’t like to use it for our production

According to the apollo client documentation The first popular JavaScript library to implement subscriptions over WebSocket is called subscriptions-transport-ws. This library is no longer actively maintained. Its successor is a library called graphql-ws. These two libraries do not use the same WebSocket subprotocol, so you need to use the same subprotocol that your GraphQL endpoint uses.

I am wondering if Bluescape support graphql-ws ?

When I change ws import to import { WebSocket } from 'ws' I don’t have Invalid WebSocket implementation provided error anymore instead I have Socket closed with event 1011 Internal Error

Try to change webSocketImpl: ws to webSocketImpl: ws.WebSocket and open console in codesandbox to see the error

Hi @Amerehei,

Sorry you have run into errors. I updated the graphQL subscription example NodeJS code with what is needed in the package.json and improved imports, which should solve the problems for you.

The updated example should run if you follow the following steps:

  1. Create a directory and add package.json to the directory with the following:
    "name": "ws-client-demo",
    "version": "1.0.0",
    "type": "module",
    "license": "MIT",
    "dependencies": {
      "graphql": "^15.5.0",
      "graphql-ws": "^4.2.1",
      "ws": "^7.4.4"
  1. install dependencies in package.json with npm install
  2. copy the example code to subscriptionTest.js file in the directory
  3. update the sample code with your your JWT_TOKEN and WORKSPACE_ID in subscriptionTest.js
  4. run subscriptionTest.js

This should work with graphql-ws, which is much better than subscriptions-transport-ws.

Can you try the steps above to see if this solves the errors you are having?

Thank you @kkoechley for your time and nice responses

I have done the instruction in this code sandbox you can see Invalid WebSocket implementation provided error and again if we change webSocketImpl: ws to webSocketImpl: ws.Websocket we have error 1011

The other problem is, I use "graphql": "^16.7.1" in my project and cannot downgrade it to "graphql": "^15.5.0"

About subscriptions-transport-ws : It seems it dosen’t work anymore and in the recent update of Bluescape something is updated or broken

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

@kkoechley When I update JWT_TOKEN and WORKSPACE_ID and change webSocketImpl: ws to webSocketImpl: ws.Websocket, I can subscribe to events

@kkoechley I can verify that it works well on latest version of graphql and graphql-ws

"graphql": "^16.7.1",
"graphql-ws": "^5.14.0",

I don’t need to install ws and pass webSocketImpl, the default value is compatible with Bluescape.

@kkoechley Thank you for your help

hi @Amerehei,

I’m glad to hear that things are working for you now! That’s great news.

As always, please let me know if you need anything else.

1 Like

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