Skip to content

Commit d0a025c

Browse files
committed
Tweaking timeouts and adding try/catch
1 parent e638174 commit d0a025c

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

api/sockets/connectHandler.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,35 @@ const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({}));
1818

1919
export const handler = async (event: WebSocketConnectEvent) => {
2020
console.log("Connect event:", JSON.stringify(event));
21-
const { connectionId, authorizer } = event.requestContext;
22-
23-
if (!authorizer?.userId) {
24-
console.error("Missing userId in authorizer context");
25-
return { statusCode: 401 };
21+
try {
22+
const { connectionId, authorizer } = event.requestContext;
23+
24+
if (!authorizer?.userId) {
25+
console.error("Missing userId in authorizer context");
26+
return { statusCode: 401 };
27+
}
28+
29+
const userId = authorizer.userId;
30+
31+
await ddb.send(
32+
new PutCommand({
33+
TableName: process.env.ABSTRACT_PLAY_TABLE!,
34+
Item: {
35+
pk: "wsConnections",
36+
sk: connectionId,
37+
38+
connectionId,
39+
userId,
40+
41+
// Optional TTL for auto-cleanup
42+
ttl: Math.floor(Date.now() / 1000) + 3600,
43+
},
44+
})
45+
);
46+
} catch (ex) {
47+
console.log("Connect error:", JSON.stringify(ex));
48+
return { statusCode: 500 };
2649
}
2750

28-
const userId = authorizer.userId;
29-
30-
await ddb.send(
31-
new PutCommand({
32-
TableName: process.env.ABSTRACT_PLAY_TABLE!,
33-
Item: {
34-
pk: "wsConnections",
35-
sk: connectionId,
36-
37-
connectionId,
38-
userId,
39-
40-
// Optional TTL for auto-cleanup
41-
ttl: Math.floor(Date.now() / 1000) + 3600,
42-
},
43-
})
44-
);
45-
4651
return { statusCode: 200 };
4752
};

api/sockets/disconnectHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const translateConfig = { marshallOptions, unmarshallOptions };
3030
const ddbDocClient = DynamoDBDocumentClient.from(clnt, translateConfig);
3131

3232
export const handler = async (event: WebSocketDisconnectEvent) => {
33+
console.log("Disconnect event:", JSON.stringify(event));
3334
const { connectionId } = event.requestContext;
3435

3536
try {

serverless.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ functions:
314314
connect:
315315
handler: api/sockets/connectHandler.handler
316316
logRetentionInDays: 7
317+
timeout: 30
317318
events:
318319
- websocket:
319320
route: $connect
@@ -323,6 +324,7 @@ functions:
323324

324325
disconnect:
325326
handler: api/sockets/disconnectHandler.handler
327+
timeout: 30
326328
logRetentionInDays: 7
327329
events:
328330
- websocket:
@@ -331,6 +333,7 @@ functions:
331333
messageHandler:
332334
handler: api/sockets/messageHandler.handler
333335
logRetentionInDays: 7
336+
timeout: 30
334337
environment:
335338
DOMAIN_NAME: ${self:custom.websocketDomain}
336339
STAGE: ${self:provider.stage}

0 commit comments

Comments
 (0)