-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (51 loc) · 1.8 KB
/
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
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
'use client';
import 'dotenv/config'
import WebSocket from 'ws';
let region = process.env.REGION;
let appsyncID = process.env.APPSYNC_ENDPOINT_ID;
let apiKey = process.env.APPSYNC_KEY_ID;
let url = `wss://${appsyncID}.appsync-realtime-api.${region}.amazonaws.com/graphql`
console.log(appsyncID, apiKey);
const api_header = {
host: `${appsyncID}.appsync-api.${region}.amazonaws.com`,
'x-api-key': apiKey,
};
let ws = new WebSocket(url, ['graphql-ws']);
// payload should be an empty JSON object
const payload = {};
const base64_api_header = btoa(JSON.stringify(api_header));
const base64_payload = btoa(JSON.stringify(payload));
const appsync_url = url + '?header=' + base64_api_header + '&payload=' + base64_payload;
ws = new WebSocket(appsync_url, ['graphql-ws']);
const listenChannel = (channelName) => {
let listen = JSON.stringify({
"id": "1", // change this to UUID
"type": "start",
"payload": {
"variables": {
},
"extensions": {
"authorization": api_header
},
"operationName": null,
"data": JSON.stringify({ "query": `subscription {\n subscribe(name: \"${channelName}\") {\n data \n }\n}\n` })
}
});
console.log(`Starting listener for ${channelName}`);
ws.send(listen)
}
ws.onopen = (e) => {
console.log('Socket opened');
ws.send(JSON.stringify({ type: "connection_init" }));
listenChannel( process.env.CHANNEL || "robots");
};
ws.onmessage = (e) => {
let data = JSON.parse(e.data);
if (data.payload) {
console.log('Msg received', data.payload.data);
} else {
console.log('Msg received', e.data);
}
};
ws.onclose = (e) => {console.log('Socket closed', e);};
ws.onerror = (e) => {console.log('Socket error', e);};