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.

Unable to get subscriptions working with Urql

See original GitHub issue

Hi I’m having an issue getting subscriptions to function with Urql, I am using a class created using examples in docs

class WebSocketLink {
  private client: Client;
  constructor(config: ClientOptions) {
    this.client = createClient(config);
  }
  public request(op: SubscriptionOperation): Observable<ExecutionResult> {
    return new Observable((sink) => {
      return this.client.subscribe<ExecutionResult>(
        { query: op.query, variables: op.variables as Record<string, unknown> | null | undefined },
        {
          ...sink,
          error: (err) => {
            console.log('Error: ', err);
            if (err instanceof Error) {
              sink.error(err);
            } else if (err instanceof CloseEvent) {
              sink.error(new Error(`Socket closed with even ${err.code}` + err.reason ? `: ${err.reason}` : ''));
            } else {
              sink.error(new Error(err.map(({ message }) => message).join(',')));
            }
          },
        },
      );
    });
  }
}

Below is what Urql passes to client.subscribe of which I extract query and variables and pass them on.

export interface SubscriptionOperation {
    query: string;
    variables?: object;
    key: string;
    context: OperationContext;
}

When calling a subscription via Urql I get an error with code: 1002 which appears to be a low level Websocket protocol violation from what I can find. Comparing what is sent to the server from subscriptions-transport-ws and graphql-transport-ws there doesn’t appear to be a difference. I am authenticating connections via connectionParams.

I use Express/Postgraphile on the server which uses subscriptions-transport-ws, I am aware you have a pull request to replace subscriptions-transport-ws with your implementation, so unsure if this is a compatibility issue, an error on my part or something else.

Many thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MikeT117commented, Sep 29, 2020

Thanks @enisdenjo! I had thought that may be the problem but wasn’t 100% sure, I will try the your fork and see how it goes.

0reactions
enisdenjocommented, Feb 2, 2022

@isuhendro because that branch has been merged to PostGraphile a while ago. It ships with graphql-ws built in since v4.11.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[@urql/svelte] Svelte subscription example from docs doesn't ...
Unable to find native implementation, or alternative implementation for WebSocket! React app (with subscriptions) worked with subscriptions- ...
Read more >
Web socket problem when adding subscriptions in URQL client
And I encountered the error below (Server Error Error: Unable to find native implementation, or alternative implementation for WebSocket!).
Read more >
Subscriptions | urql Documentation
We will concatenate the incoming messages so that we can display all messages that have come in over the subscription across events. import...
Read more >
Errors | urql Documentation
Invalid GraphQL document : All GraphQL documents must contain an OperationDefinition node for a query, subscription or mutation.
Read more >
Error handling - urql Documentation
This is because in GraphQL a query can have partially failed but still contain some data. In that case CombinedError will be passed...
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