Aws-mobile-appsync-sdk-js: 'WebSocket is not supported by this browser.' in a node environment

Created on 16 Nov 2018  路  7Comments  路  Source: awslabs/aws-mobile-appsync-sdk-js

Do you want to request a feature or report a bug?
Report a bug

What is the current behavior?

(node:30502) UnhandledPromiseRejectionWarning: Error: AMQJS0010E WebSocket is not supported by this browser.
    at new ClientImpl (/home/brandon/src/matrix-runner/node_modules/aws-appsync/lib/vendor/paho-mqtt.js:771:11)
    at new Client (/home/brandon/src/matrix-runner/node_modules/aws-appsync/lib/vendor/paho-mqtt.js:1782:17)
    at SubscriptionHandshakeLink.<anonymous> (/home/brandon/src/matrix-runner/node_modules/aws-appsync/lib/link/subscription-handshake-link.js:170:34)
    at step (/home/brandon/src/matrix-runner/node_modules/aws-appsync/lib/link/subscription-handshake-link.js:56:23)
    at Object.next (/home/brandon/src/matrix-runner/node_modules/aws-appsync/lib/link/subscription-handshake-link.js:37:53)
    at /home/brandon/src/matrix-runner/node_modules/aws-appsync/lib/link/subscription-handshake-link.js:31:71
    at new Promise (<anonymous>)
    at __awaiter (/home/brandon/src/matrix-runner/node_modules/aws-appsync/lib/link/subscription-handshake-link.js:27:12)
    at SubscriptionHandshakeLink.connectNewClient (/home/brandon/src/matrix-runner/node_modules/aws-appsync/lib/link/subscription-handshake-link.js:163:16)
    at /home/brandon/src/matrix-runner/node_modules/aws-appsync/lib/link/subscription-handshake-link.js:159:75
(node:30502) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:30502) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.

function graphqlSubscribe(accessToken) {
  // Set up Apollo client
  const client = new AWSAppSyncClient({
    url: url,
    region: region,
    auth: {
      type: type,
      jwtToken: accessToken,
    }
  });

  const gql = require('graphql-tag');
  // Set up a subscription query
  const subquery = gql(`subscription OnCreateBuild {
    onCreateBuild {
        id
        buildNumber
    }
  }`);

  client.hydrated().then(function (client) {
      //Now run a query
  //    client.query({ query: query })
  //    //client.query({ query: query, fetchPolicy: 'network-only' })   //Uncomment for AWS Lambda
  //        .then(function logData(data) {
  //            console.log('results of query: ', data);
  //        })
  //        .catch(console.error);

      //Now subscribe to results
      const observable = client.subscribe({ query: subquery });

      const realtimeResults = function realtimeResults(data) {
          console.log('realtime data: ', data);
      };

      observable.subscribe({
          next: realtimeResults,
          complete: console.log,
          error: console.log,
      });
  });
}

What is the expected behavior?
No errors

Which versions and which environment (browser, react-native, nodejs) / OS are affected by this issue? Did this work in previous versions?
nodejs v8.12.0, unknown if it works in another version

question documentation

Most helpful comment

Hi @blbradley

Node environments don't have a Websocket implementation, one thing you can do is:

npm i ws@^3.3.1

and then in your file do a

global.WebSocket = require('ws');

All 7 comments

Hi @blbradley

Node environments don't have a Websocket implementation, one thing you can do is:

npm i ws@^3.3.1

and then in your file do a

global.WebSocket = require('ws');

Is it possible to have this transparently handled by this library?

Or, I guess the docs could be updated. Just the Building a client app in Node.js bit.

The docs recommend to install the ws library but nothing about setting global.WebSocket.

Hi @blbradley

Is it possible to have this transparently handled by this library?

It could be possible but I personally don't think it would be a good idea, since consuming code might already be providing a different polyfill.

Or, I guess the docs could be updated. Just the Building a client app in Node.js bit.

Totally agree, I've tagged this issue as documentation to take care of that

The docs recommend to install the ws library but nothing about setting global.WebSocket.

Yeah, the docs could be clearer about this, though they have these lines:

"use strict";
/**
* This shows how to use standard Apollo client on Node.js
*/

global.WebSocket = require('ws');

NOTE
Please note that you need ws versions 3.x or 4.x (NOT 5.x since it has some issues connecting to the mqtt/ws endpoints AppSync uses)

Oh! This is a copy/paste and reading error on my part, then. But it was good to document in case anyone else forgets.

Thanks!

Hi @blbradley

Node environments don't have a Websocket implementation, one thing you can do is:

npm i ws@^3.3.1

and then in your file do a

global.WebSocket = require('ws');

It now works with newer versions of ws ie 7.4.5! Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yhenni1989 picture yhenni1989  路  3Comments

yarax picture yarax  路  3Comments

ArronHsiao picture ArronHsiao  路  3Comments

wzup picture wzup  路  3Comments

manueliglesias picture manueliglesias  路  3Comments