-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
116 lines (97 loc) · 2.72 KB
/
handler.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"use strict";
const AWS = require("aws-sdk");
const dynamo = new AWS.DynamoDB.DocumentClient();
const CONNECTION_DB_TABLE = process.env.CONNECTION_DB_TABLE;
const successfullResponse = {
statusCode: 200,
body: "Success",
};
const failedResponse = (statusCode, error) => ({
statusCode,
body: error,
});
module.exports.connectHandler = (event, context, callback) => {
addConnection(event.requestContext.connectionId)
.then(() => {
callback(null, successfullResponse);
})
.catch((err) => {
callback(failedResponse(500, JSON.stringify(err)));
});
};
module.exports.disconnectHandler = (event, context, callback) => {
deleteConnection(event.requestContext.connectionId)
.then(() => {
callback(null, successfullResponse);
})
.catch((err) => {
console.log(err);
callback(failedResponse(500, JSON.stringify(err)));
});
};
module.exports.defaultHandler = (event, context, callback) => {
callback(null, failedResponse(404, "No event found"));
};
module.exports.broadcastHandler = (event, context, callback) => {
sendMessageToAllConnected(event)
.then(() => {
callback(null, successfullResponse);
})
.catch((err) => {
callback(failedResponse(500, JSON.stringify(err)));
});
};
const sendMessageToAllConnected = (event) => {
return getAllConnections().then((connectionData) => {
return connectionData.Items.map((connectionId) => {
return send(event, connectionId.connectionId);
});
});
};
const getAllConnections = () => {
const params = {
TableName: CONNECTION_DB_TABLE,
ProjectionExpression: "connectionId",
};
return dynamo.scan(params).promise();
};
const send = (event, connectionId) => {
const body = JSON.parse(event.body);
let postData = body.data;
console.log("Sending.....");
if (typeof postData === Object) {
console.log("It was an object");
postData = JSON.stringify(postData);
} else if (typeof postData === String) {
console.log("It was a string");
}
const endpoint =
event.requestContext.domainName + "/" + event.requestContext.stage;
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: "2018-11-29",
endpoint: endpoint,
});
const params = {
ConnectionId: connectionId,
Data: postData,
};
return apigwManagementApi.postToConnection(params).promise();
};
const addConnection = (connectionId) => {
const params = {
TableName: CONNECTION_DB_TABLE,
Item: {
connectionId: connectionId,
},
};
return dynamo.put(params).promise();
};
const deleteConnection = (connectionId) => {
const params = {
TableName: CONNECTION_DB_TABLE,
Key: {
connectionId: connectionId,
},
};
return dynamo.delete(params).promise();
};