websocket in cdk typescript

0

Hey guys hope ya'll doing well i wanted help related to aws websocket if any of yall know how i can use it in cdk hit me up.

1개 답변
1

i can give you several lambda functions the Lambda function for handling new connections

const wsConnectLambda = new NodejsFunction(this, 'ws-connect-lambda', {
   entry: join(__dirname, '../src/connect/index.ts'),
   handler: 'handler',
   functionName: 'connect-lambda',
   runtime: Runtime.NODEJS_18_X,
});

Each Lambda function needs a handler.

export const handler = async (event: APIGatewayProxyEvent) => {
  const connectionId = event.requestContext.connectionId;
  console.log('connection created:', connectionId);
  return { statusCode: 200, body: 'Connected.' };
};

Use the WebSocketApi CDK construct to create the WebSocket API and integrate it with the Lambda functions

const webSocketApi = new apigw2.WebSocketApi(this, 'my-first-websocket-api', {
   connectRouteOptions: {
     integration: new WebSocketLambdaIntegration(
       'ws-connect-integration',
       wsConnectLambda
     ),
   },
   disconnectRouteOptions: {
     integration: new WebSocketLambdaIntegration(
       'ws-disconnect-integration',
       wsDisconnectLambda
     ),
   },
});

const apiStage = new apigw2.WebSocketStage(this, 'dev', {
   webSocketApi,
   stageName: 'dev',
   autoDeploy: true,
});

for more details you can refer the following application

https://github.com/aws-samples/websocket-chat-application

profile picture
전문가
답변함 4달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인