Registry / messaging / aws-post-to-connection

aws-post-to-connection

JSON →
library0.1.21jsnpmunverified

A utility library for posting messages to a connected WebSocket client via AWS API Gateway. Version 0.1.21 is the current stable release; the package is infrequently updated. It simplifies sending messages to WebSocket connections from AWS Lambda functions by wrapping the AWS SDK's ApiGatewayManagementApi. Key differentiators include support for the same gateway, another gateway, and local WebSocket servers, making it useful for development and testing.

npm install aws-post-to-connection
INSTALL
IMPORT
SIG · AWS-POST-TO-CONNEC
A
aws-post-to-connection
messagingjavascriptv0.1.21
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const PostToConnection = require('aws-post-to-connection')
import PostToConnection from 'aws-post-to-connection'
The package uses CommonJS; ESM import may fail without wrapper.
PostToConnection
const PostToConnection = require('aws-post-to-connection')
import { PostToConnection } from 'aws-post-to-connection'
Default export is a function; named import will be undefined.
postToConnection (function result)
const postToConnection = PostToConnection(options); await postToConnection(data, connectionId)
await PostToConnection(options)(data, connectionId)
Works but less readable; no significant difference.

Creates a post function from event context and sends a message to the current WebSocket connection.

const PostToConnection = require('aws-post-to-connection') // Use event from API Gateway to post to same gateway const postToConnection = PostToConnection({ stage: process.env.STAGE ?? '', domainName: process.env.DOMAIN_NAME ?? '', }) async function handler(event) { const connectionId = event.requestContext.connectionId await postToConnection({ message: 'Hello!' }, connectionId) return { statusCode: 200 } }
Debug
Known issues
gotchaMissing stage or domainName in options will cause failure when posting to another gateway.
fix
Ensure options for PostToConnection include stage and domainName if not using event from same gateway.
affects: >=0.0.0
gotchaThe function returned by PostToConnection is asynchronous and must be awaited.
fix
Always use await or handle the promise to avoid unhandled rejections.
affects: >=0.0.0
gotchaWhen using a local WebSocket server, secure must be set to false explicitly.
fix
Set secure: false in options when posting to localhost without HTTPS.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot destructure property 'stage' of 'undefined' as it is undefined.
Calling PostToConnection() without any options.
fix
Provide options object with at least stage and domainName or pass the event object.
Error: Missing required key 'ConnectionId' in params
Called postToConnection without a connectionId string.
fix
Ensure second argument to the returned function is a valid connection ID string.
TypeError: postToConnection is not a function
Tried to use named import or mistook the export.
fix
Use const PostToConnection = require('aws-post-to-connection') and then call PostToConnection(options) which returns a function.
Upgrade
Version history
0.1.21latest on npm
Audit
Dependencies
aws-sdkrequiredUsed to create ApiGatewayManagementApi client for posting messages to WebSocket connections.
Agent activity
22 hits · last 30 days
node
18
Amazon
1
OpenAI (training)
1
Resources
aws-post-to-connection — npm install aws-post-to-connection · libregistry