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.

'WebSocket is not supported by this browser.' in a node environment

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
manueliglesiascommented, Nov 16, 2018

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');
1reaction
manueliglesiascommented, Nov 16, 2018

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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

websocket problem: cannot connect to node.js server
When I try to add socket.io.js into my client, I get an error that the client can't find "require". I looked at the...
Read more >
WebSocket - Wikipedia
WebSocket is a computer communications protocol, providing full-duplex communication ... Most browsers support the protocol, including Google Chrome, Firefox, ...
Read more >
How To Create a Real-Time App with Socket.IO, Angular, and ...
A modern web browser that supports WebSocket. This tutorial was originally written in an environment consisting of Node.js v8.11.4, ...
Read more >
NodeJS support for y-websocket? - Yjs Community
... take to make y-websocket support the nodejs runtime environment. ... in node, it fails with “ReferenceError: window is not defined”, ...
Read more >
Why the Hell Would I Use Node.js? A Case-by-case Tutorial
Node.js can solve I/O scaling, but was not created to compute scaling ... The applications would run on the open web stack backed...
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