-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
31 lines (23 loc) · 820 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const AWS = require('aws-sdk');
module.exports.handler = async (event) => {
console.log(JSON.stringify(event, 2));
const { send } = getSocketContext(event);
await send(JSON.stringify({ message: 'This response was pushed from my Lambda.' }));
return {
isBase64Encoded: false,
statusCode: 200,
body: ""
};
};
function getSocketContext(event) {
const { domainName, stage, connectionId } = event.requestContext;
const endpoint = `${domainName}/${stage}`;
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint
});
const send = async (data) => {
await apigwManagementApi.postToConnection({ ConnectionId: connectionId, Data: data }).promise();
};
return { connectionId, endpoint, send };
}