Skip to content

Commit 1031e17

Browse files
committed
Add domain and stage to environment
1 parent 580a92b commit 1031e17

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

api/sockets/messageHandler.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ApiGatewayManagementApiClient,
99
PostToConnectionCommand,
1010
} from "@aws-sdk/client-apigatewaymanagementapi";
11+
import { SQSEvent, SQSRecord } from "aws-lambda";
1112

1213
const REGION = "us-east-1";
1314
const clnt = new DynamoDBClient({ region: REGION });
@@ -26,7 +27,7 @@ const unmarshallOptions = {
2627
const translateConfig = { marshallOptions, unmarshallOptions };
2728
const ddbDocClient = DynamoDBDocumentClient.from(clnt, translateConfig);
2829

29-
export const handler = async (event: any) => {
30+
export const handler = async (event: SQSEvent) => {
3031
// SQS may batch multiple messages
3132
for (const record of event.Records) {
3233
await processRecord(record);
@@ -35,7 +36,7 @@ export const handler = async (event: any) => {
3536
return { statusCode: 200 };
3637
};
3738

38-
async function processRecord(record: any) {
39+
async function processRecord(record: SQSRecord) {
3940
let body;
4041
try {
4142
body = JSON.parse(record.body);
@@ -75,11 +76,13 @@ async function processRecord(record: any) {
7576
const now = Math.floor(Date.now() / 1000);
7677

7778
for (const item of result.Items ?? []) {
79+
console.log(`Processing item: ${JSON.stringify(item)}`);
7880
const connectionId = item.sk.S!;
7981
const ttl = item.ttl?.N ? parseInt(item.ttl.N) : null;
8082

8183
// Delete expired TTL entries
8284
if (ttl && ttl < now) {
85+
console.log(`Deleting expired connection: ${connectionId}`);
8386
await deleteConnection(connectionId);
8487
continue;
8588
}

serverless.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ provider:
8686
userpoolClient: ${self:custom.stageConfig.${self:provider.stage}.userpoolClient}
8787
SQS_URL: ${self:custom.stageConfig.${self:provider.stage}.sqsurl}
8888
AIAI_USERID: SkQfHAjeDxs8eeEnScuYA
89+
WEBSOCKET_DOMAIN: ${cf:${self:service}-${self:provider.stage}.WebSocketApiDomainName}
90+
WEBSOCKET_STAGE: ${self:provider.stage}
8991
TOTP_KEY: ${env:TOTP_KEY}
9092
VAPID_PRIVATE_KEY: ${env:VAPID_PRIVATE_KEY}
9193
VAPID_PUBLIC_KEY: ${env:VAPID_PUBLIC_KEY}
@@ -218,6 +220,14 @@ resources:
218220
Value: !Ref WsMessageQueue
219221
Export:
220222
Name: WsMessageQueueUrl-${self:provider.stage}
223+
WebSocketApiDomainName:
224+
Value:
225+
Fn::Join:
226+
- ""
227+
- - Ref: WebsocketsApi
228+
- ".execute-api."
229+
- Ref: AWS::Region
230+
- ".amazonaws.com"
221231
WebsocketApiUrl:
222232
Value:
223233
Fn::Join:

0 commit comments

Comments
 (0)