ws-response
Post message to WebSocket connection.
Install
To install this middleware you can use NPM:
- npm
- Yarn
npm install --save @middy/ws-responder
yarn add @middy/ws-responder
Options
AwsClient
(object) (defaultAWS.ApiGatewayManagementApi
): AWS.ApiGatewayManagementApi class constructor (e.g. that has been instrumented with AWS XRay). Must be fromaws-sdk
v2.awsClientOptions
(object) (defaultundefined
): Options to pass to AWS.ApiGatewayManagementApi class constructor.awsClientAssumeRole
(string) (defaultundefined
): Internal key where secrets are stored. See @middy/sts on to set this.awsClientCapture
(function) (defaultundefined
): Enable XRay by passingcaptureAWSClient
fromaws-xray-sdk
in.disablePrefetch
(boolean) (defaultfalse
): On cold start requests will trigger early if they can. SettingawsClientAssumeRole
disables prefetch.
NOTES:
- Lambda is required to have IAM permission for
execute-api:ManageConnections
- If
awsClientOptions.endpoint
is 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 wsResonse from '@middy/ws-responder'
export const handler = middy((event, context) => {
return 'message'
})
handler
.use(wsResonse())
General
import middy from '@middy/core'
import wsResonse from '@middy/ws-responder'
export const handler = middy((event, context) => {
return {
ConnectionId: '...',
Data: 'message'
}
})
handler
.use(wsResonse({
awsClientOptions: {
endpoint: '...'
}
}))
Bundling
To exclude aws-sdk
add aws-sdk/clients/apigatewaymanagementapi.js
to the exclude list.