-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathactions.js
58 lines (50 loc) · 1.91 KB
/
actions.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
import AWS from 'aws-sdk';
let NAMES_DB = {};
/*
IMPORTANT: remove 'https://' and '@connections' from the Connection URL that you copy over
example:
Connection URL https://xxxxxxxxxxx/execute-api.us-east-1.amazonaws.com/production/@connections
turns to:
ENDPOINT = 'xxxxxxxxxxx/execute-api.us-east-1.amazonaws.com/production/'
see minute 7:00 https://youtu.be/BcWD-M2PJ-8?t=420
*/
const ENDPOINT = '>>>ENTER_YOUR_ENDPOINT_HERE<<<';
const client = new AWS.ApiGatewayManagementApi({ endpoint: ENDPOINT });
const sendToOne = async (id, body) => {
try {
await client.postToConnection({
'ConnectionId': id,
'Data': Buffer.from(JSON.stringify(body)),
}).promise();
} catch (err) {
console.error(err);
}
};
const sendToAll = async (ids, body) => {
const all = ids.map(i => sendToOne(i, body));
return Promise.all(all);
};
export const $connect = async () => {
return {};
};
export const setName = async (payload, meta) => {
NAMES_DB[meta.connectionId] = payload.name;
await sendToAll(Object.keys(NAMES_DB), { members: Object.values(NAMES_DB) });
await sendToAll(Object.keys(NAMES_DB), { systemMessage: `${NAMES_DB[meta.connectionId]} has joined the chat` })
return {};
};
export const sendPublic = async (payload, meta) => {
await sendToAll(Object.keys(NAMES_DB), { publicMessage: `${NAMES_DB[meta.connectionId]}: ${payload.message}` })
return {};
};
export const sendPrivate = async (payload, meta) => {
const to = Object.keys(NAMES_DB).find(key => NAMES_DB[key] === payload.to);
await sendToOne(to, { privateMessage: `${NAMES_DB[meta.connectionId]}: ${payload.message}` });
return {};
};
export const $disconnect = async (payload, meta) => {
await sendToAll(Object.keys(NAMES_DB), { systemMessage: `${NAMES_DB[meta.connectionId]} has left the chat` })
delete NAMES_DB[meta.connectionId];
await sendToAll(Object.keys(NAMES_DB), { members: Object.values(NAMES_DB) })
return {};
};