ws-response
Post message to WebSocket connection.
Install
To install this middleware you can use NPM:
- npm
- Yarn
- pnpm
- Bun
npm install --save @middy/ws-response
npm install --save-dev @aws-sdk/client-apigatewaymanagementapi
yarn add @middy/ws-response
yarn add --dev @aws-sdk/client-apigatewaymanagementapi
pnpm add @middy/ws-response
pnpm add --save-dev @aws-sdk/client-apigatewaymanagementapi
bun add @middy/ws-response
bun add --dev @aws-sdk/client-apigatewaymanagementapi
Options
AwsClient(object) (defaultApiGatewayManagementApiClient): ApiGatewayManagementApi class constructor (i.e. that has been instrumented with AWS XRay). Must be from@aws-sdk/client-apigatewaymanagementapi.awsClientOptions(object) (defaultundefined): Options to pass to ApiGatewayManagementApiClient class constructor.awsClientAssumeRole(string) (defaultundefined): Internal key where secrets are stored. See @middy/sts on to set this.awsClientCapture(function) (defaultundefined): Enable XRay by passingcaptureAWSv3Clientfromaws-xray-sdkin.disablePrefetch(boolean) (defaultfalse): On cold start requests will trigger early if they can. SettingawsClientAssumeRoledisables prefetch.
NOTES:
- Lambda is required to have IAM permission for
execute-api:ManageConnections - If
awsClientOptions.endpointis not set it will be set usingevent.requestContext.{domainName,stage} - If response does not contain
ConnectId, it will be set fromevent.requestContext.connectionId
Sample usage
API Gateway
import middy from '@middy/core'
import wsResponse from '@middy/ws-response'
export const handler = middy((event, context) => {
return 'message'
})
handler.use(wsResponse())
General
import middy from '@middy/core'
import wsResponse from '@middy/ws-response'
const lambdaHandler = (event, context) => {
return {
ConnectionId: '...',
Data: 'message'
}
}
export const handler = middy()
.use(
wsResponse({
awsClientOptions: {
endpoint: '...'
}
})
)
.handler(lambdaHandler)
Bundling
To exclude @aws-sdk add @aws-sdk/client-apigatewaymanagementapi to the exclude list.