1 Answer
- Newest
- Most votes
- Most comments
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
Relevant content
- asked 8 months ago
- Accepted Answerasked 2 years ago
- asked 5 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago