Skip to main content

ws-response

Post message to WebSocket connection.

Install

To install this middleware you can use NPM:

npm install --save @middy/ws-responder

Options

  • AwsClient (object) (default AWS.ApiGatewayManagementApi): AWS.ApiGatewayManagementApi class constructor (e.g. that has been instrumented with AWS XRay). Must be from aws-sdk v2.
  • awsClientOptions (object) (default undefined): Options to pass to AWS.ApiGatewayManagementApi class constructor.
  • awsClientAssumeRole (string) (default undefined): Internal key where secrets are stored. See @middy/sts on to set this.
  • awsClientCapture (function) (default undefined): Enable XRay by passing captureAWSClient from aws-xray-sdk in.
  • disablePrefetch (boolean) (default false): On cold start requests will trigger early if they can. Setting awsClientAssumeRole disables prefetch.

NOTES:

  • Lambda is required to have IAM permission for execute-api:ManageConnections
  • If awsClientOptions.endpoint is not set it will be set using event.requestContext.{domainName,stage}
  • If response does not contain ConnectId, it will be set from event.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.