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 connection fails.

See original GitHub issue

Hi,

I am trying to implement WebSocket subscriptions to Hasura and here is my client setup:

import ApolloClient from 'apollo-client';
import { WebSocketLink } from 'apollo-link-ws';
import { HttpLink } from 'apollo-link-http';
import { split } from 'apollo-link';
import { getMainDefinition } from 'apollo-utilities';
import { InMemoryCache } from 'apollo-cache-inmemory';

import { getToken } from 'services/auth0'
import { hasura } from 'config'

class HasuraDAO {
  constructor() {
    const wsLink = new WebSocketLink({
      uri: hasura.wsUrl,
      options: {
        reconnect: true,
        timeout: 30000
      }
    });
    const httpLink = new HttpLink({
      uri: hasura.httpUrl,
      headers: {
        'Authorization': `Bearer ${getToken()}`
      }
    })

    const link = split(
      ({ query }) => {
        const { kind, operation } = getMainDefinition(query);
        return kind === 'OperationDefinition' && operation === 'subscription';
      },
      wsLink,
      httpLink,
    )
    
    this.client = new ApolloClient({
      link,
      cache: new InMemoryCache()
    });
  }
}

export default new HasuraDAO()

This is the call of client.subscribe function:

import Hasura from 'dao/hasura/HasuraDAO';
import gql from 'graphql-tag';

const query = gql`subscription {
  users {
    id
  }
}`

const observable = Hasura.client.subscribe({ query });
observable.subscribe({
  next: ({ data }) => console.log(data)
});

I think I’m doing it all right, but still getting this error:

client.js:426 WebSocket connection to 'ws://localhost:8081/v1alpha1/graphql' failed: Data frame received after close

Can you please help me finding my mistake?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
coco98commented, Oct 6, 2018

Re-opening this issue so that we add it to our docs! Thanks @russc for bringing it back up.

5reactions
dedancommented, Apr 28, 2020

Even with the solution of @louisdvs I still get this error when using wss: instead of ws:.

Does anybody know how to fix this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebSocket connection failed: Error during ... - Stack Overflow
I just realized that websockets is working partially. I see a 101 Switching Protocols request in the Chrome developer console. However the only ......
Read more >
WebSocket connection failed. Error in browser console, since ...
Starting from version 8.0.4, a lot of this messages: WebSocket connection to 'ws://ip_host:port/api/live/ws' failed: centrifuge.js:544 , started ...
Read more >
WebSocket: error event - Web APIs | MDN
The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent...
Read more >
Troubleshooting connection issues | Socket.IO
You are trying to reach a plain WebSocket server; The server is not reachable; The client is not compatible with the version of...
Read more >
Error with WebSocket - Microsoft Q&A
The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent...
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 Hashnode Post

No results found