#✅ - Unable to create subscriptions on Nodejs server

16 messages · Page 1 of 1 (latest)

late mirage
#

I'm trying to create appsync subscriptions on nodejs server, but I am unable to create a connection.

I am unable to create a connection, and it just oscillates between "Connecting" and "ConnectionDistorted"

Code:

Hub.listen("api", (data: any) => {
  console.log("data is:", data);
  const { payload } = data;
  if (payload.event === CONNECTION_STATE_CHANGE) {
    connectionState = payload.data.connectionState as ConnectionState;
    console.log("Connection state updated to:", connectionState);
    if (connectionState === "Connected") {
      resolve();
    }
  }
});

subscription = client
  .graphql<GraphQLSubscription<UserResult>>({
    query: subscribeUser,
    variables: { id: user2UserId },
  })
  .subscribe({
    next: (value) => {
      console.log("Subscription value received:", value);
    },
    error: (error) => {
      console.error("Subscription error:", error);
    },
  });

Logs:

data is: {
      channel: 'api',
      payload: {
        event: 'ConnectionStateChange',
        data: {
          provider: [AWSAppSyncRealTimeProvider],
          connectionState: 'Connecting'
        },
        message: 'Connection state is Connecting'
      },
      source: 'PubSub',
      patternInfo: []
    }


  console.log
    Connection state updated to: Connecting

 
  console.log
    data is: {
      channel: 'api',
      payload: {
        event: 'ConnectionStateChange',
        data: {
          provider: [AWSAppSyncRealTimeProvider],
          connectionState: 'ConnectionDisrupted'
        },
        message: 'Connection state is ConnectionDisrupted'
      },
      source: 'PubSub',
      patternInfo: []
    }

  console.log
    Connection state updated to: ConnectionDisrupted
#

A few more pointers:

  • The exact same queries for subscription does works on iOS, and AWS Console
  • I am able to run Mutations and Queries on nodejs server, only subscription does not work here
  • I am using template literal for the query (the ones with the backticks)

I did some digging, and found that subscription has partial support on server side

Referring to this link: https://docs.amplify.aws/gen1/javascript/build-a-backend/graphqlapi/connect-from-server-runtime/

  • It says subscriptions is not supported on nextjs server side
  • It says subscription is supported on AWS Lambda, but it does not give any example for subscription

What am I missing here? And let me know subscriptions is really not supported on the server side or if it is, how should I do it?

stable garden
#

Hi @late mirage I don’t see where on that page it mentions that subscriptions are supported on AWS Lambda. Can you link to a highlight on the page or screenshot the exact phrase?

If it does indeed say that, it would be inaccurate or incomplete at best.

As far as I can tell, Node did not have native support for WebSockets until v22.0.0 which released on 2024-04-25.

Lambda currently only supports up to Node v20.

Also, even if that weren’t the case, there might be code in our library that disables real time in a server environment because of this lack of support in the past. I’m not sure if that’s the case for the graphql client, but I’m sure it is for DataStore because there’s a warning that is logged saying realtime is disabled when used in a non-browser environment.

#

The closest thing I could find on the page was this excerpt:


This API will have operations available for Query, Mutation, and Subscription. Let's take a look at how to perform both a query as well as a mutation from a Lambda function using Node.js.

I can see how this could be misleading because it mentions all three operations are “available” on the API, which they are. Then it proceeds to only give examples for query and mutations, which are the only current supported operations in a Lambda.

late mirage
stable garden
#

We should explicitly mention that subscriptions are not supported at the moment. Once Lambda supports Node v22, then I assume subscriptions should work

late mirage
#

Ohh awesome

Thanks so much for the detailed explanation and the details Chris!

stable garden
#

I’ve seen that there is a ws library that effectively implements web sockets in Node, maybe that’s a worth a try if you need a solution sooner?

https://www.npmjs.com/package/ws

late mirage
#

Great, will check this out.

Thanks, once again!

late mirage
#

Hey @stable garden just to share an update.

We switched to Node 22, and it worked!! (we are running it in a nodejs server)

So the library didn't really disable subscriptions for graphql

Thanks a lot for your help in getting it working!!

stable garden
#

Awesome! Thanks for the update, glad you're unblocked 😄

last kilnBOT
#

✅ - Unable to create subscriptions on Nodejs server

#

Answer selected!


If it does indeed say that, it would be inaccurate or incomplete at best. 

As far as I can tell, Node did not have native support for WebSockets until v22.0.0 which released on 2024-04-25.

Lambda currently only supports up to Node v20.

Also, even if that weren’t the case, there might be code in our library that disables real time in a server environment because of this lack of support in the past. I’m not sure if that’s the case for the graphql client, but I’m sure it is for DataStore because there’s a warning that is logged saying realtime is disabled when used in a non-browser environment.```
Kudos to @stable garden!
#1261637124084469760 message
late mirage
#

Hey @stable garden (not urgent, so please do take your time)

We got this working in test cases for jest

Problem:
But now, jest does not exit

If we comment out the subscriptions code, then jest does exit, which means that subscription is the root cause of the issie

Seems like subscription is keeping some processes on

Here is what we tried:

  • ensure that we complete the subscription i.e got the completed callback running
  • --detectOpenHandles returns nothing
  • ran unsubscribe() and verified it with subscription.closed returning as true
  • also set the subscription variable as null after unsubscribe() to ensure it is cleared from the memory

Found a TLSSocket running using process._getActiveHandles() (this is found whether I unsubscribe() or don't unsubscribe()

Analysis:

Seems like subscription is automatically starting some sub-processes or something else
The subscription has closed, since I got the completed callback from subscription

Could you please point me where I should look for this?
I did take a look at documentation, but wasn't able to find anything regarding this.

stable garden
# late mirage Hey <@153291416909512704> (not urgent, so please do take your time) We got this...

Hey @late mirage , I'm not sure about the processes that subscriptions start besides WebSockets. We have some tests around subscriptions in our library where we mock web sockets that may be helpful to go through for some insight on how to test subscriptions:

https://github.com/aws-amplify/amplify-js/blob/main/packages/api-graphql/__tests__/helpers.ts

GitHub

A declarative JavaScript library for application development using cloud services. - aws-amplify/amplify-js

late mirage