Skip to content

Commit 348fa72

Browse files
committed
Save endpoint with the wsConnections record instead of environment variables
1 parent ca36a7f commit 348fa72

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

api/sockets/authHandler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export const handler = async (event: WebSocketEvent) => {
5252
return { statusCode: 400, body: "Missing token" };
5353
}
5454

55+
const domain = event.requestContext.domainName;
56+
const stage = event.requestContext.stage;
57+
5558
try {
5659
// console.log(`Verifying JWT: ${token}`);
5760
const payload = await verifier.verify(token);
@@ -72,6 +75,7 @@ export const handler = async (event: WebSocketEvent) => {
7275
connectionId,
7376
userId,
7477
invisible,
78+
endpoint: `https://${domain}/${stage}`,
7579

7680
// Optional TTL for auto-cleanup
7781
ttl: Math.floor(Date.now() / 1000) + 3600,

api/sockets/messageHandler.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,13 @@ async function processRecord(record: SQSRecord) {
5353
return;
5454
}
5555

56-
const { verb, payload, exclude, domainName, stage } = body;
57-
58-
if (!domainName || !stage) {
59-
console.error("Missing domainName or stage in SQS message");
60-
return;
61-
}
56+
const { verb, payload, exclude } = body;
6257

6358
if (!["chat", "game", "test", "connections"].includes(verb)) {
6459
console.warn("Unsupported verb:", verb);
6560
return;
6661
}
6762

68-
const apigw = new ApiGatewayManagementApiClient({
69-
endpoint: `https://${domainName}/${stage}`,
70-
});
71-
7263
// Query all active connections
7364
const result = await ddbDocClient.send(
7465
new QueryCommand({
@@ -83,7 +74,10 @@ async function processRecord(record: SQSRecord) {
8374
const now = Math.floor(Date.now() / 1000);
8475

8576
for (const conn of result.Items ?? []) {
86-
console.log(`Processing connection: ${JSON.stringify(conn)}`);
77+
const endpoint = conn.endpoint.S!;
78+
const apigw = new ApiGatewayManagementApiClient({
79+
endpoint,
80+
});
8781
const connectionId = conn.sk.S!;
8882
const userId = conn.userId.S!;
8983
const ttl = conn.ttl?.N ? parseInt(conn.ttl.N) : null;

lib/wsBroadcast.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const REGION = "us-east-1";
44
const sqsClient = new SQSClient({ region: REGION });
55

66
type WsMsgBody = {
7-
domainName: string;
8-
stage: string;
97
verb: string;
108
payload?: any;
119
exclude?: string[];
@@ -14,8 +12,6 @@ type WsMsgBody = {
1412
export async function wsBroadcast (verb: string, payload: any, exclude?: string[]): Promise<SendMessageCommandOutput> {
1513
// construct message
1614
const body: WsMsgBody = {
17-
domainName: process.env.WEBSOCKET_DOMAIN!,
18-
stage: process.env.WEBSOCKET_STAGE!,
1915
verb,
2016
payload,
2117
exclude,

0 commit comments

Comments
 (0)