Why is ECONNREFUSED when calling AWS ApiGatewayV2 from NodeJs locally with established connectionId?

Viewed 31

Diagram of Flow

I have created a flow of ApiGateway that stores connectionId of all currently connected users in a DynamoDB database. I want to test by using ApiGatewayManagement ( @aws-sdk/client-apigatewaymanagementapi ) locally with PostToConnection in a local NodeJS file.

I have established a connection, and my connectionId has been stored and is active. I have written a script to see if I can now send a message to this connection:

const { ApiGatewayManagementApi } = require("@aws-sdk/client-apigatewaymanagementapi");
const api = new ApiGatewayManagementApi({ apiVersion: "2018-11-29", endpoint: "wss://REDACTED.execute-api.us-east-1.amazonaws.com/prod" });

const params = {
    ConnectionId: "REDACTED",
    Data: Buffer.from(JSON.stringify({ Item: { q: "hello world" } }))
};

const run = async () => {
    return api.postToConnection(params);
};

run();

However, I am receiving an ECONNREFUSED error and I am unsure if I am leaving something out locally to authenticate to my API Gateway. I am able to do this all on AWS console, but using above test.js file locally fails. My question is: Can I use connectionId to talk directly to this connection that is actively established?

0 Answers
Related