-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
86 lines (78 loc) · 2.73 KB
/
main.js
File metadata and controls
86 lines (78 loc) · 2.73 KB
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
const { register, listen } = require('push-receiver');
const { WebSocketServer } = require('ws');
const FCM_PATH = process.env.FCM_PATH || "./fcm_cred.json";
const SENDER_ID = process.env.SENDER_ID
const WEBSOCKET_PORT = process.env.WS_PORT || 8082
const fs = require('fs');
const wss = new WebSocketServer({
port: WEBSOCKET_PORT
});
const getFCMCredentials = async () => {
if (!fs.existsSync(FCM_PATH)) {
const credentials = await register(SENDER_ID);
fs.writeFileSync(FCM_PATH, JSON.stringify(credentials));
wss.clients.forEach(client => {
client.send(JSON.stringify({credentials}));
})
return credentials;
} else {
return JSON.parse(fs.readFileSync(FCM_PATH).toString('utf-8'));
}
}
let persistentIds = []
let listenerStarted = false;
let notificationQueue = [];
const handlePersistentIdReceive = async (receivedIds) => {
persistentIds = receivedIds
console.log("Received Persistent IDs!");
if (!listenerStarted) {
await startFCMListener()
} else if (notificationQueue.length > 0) {
console.log("Clearing queue");
notificationQueue.forEach(notification => {
wss.clients.forEach(client => {
client.send(JSON.stringify(notification));
})
})
notificationQueue = [];
console.log("Queue cleared");
}
}
const startFCMListener = async () => {
const credentials = await getFCMCredentials()
await listen({ ...credentials, persistentIds}, onNotification);
console.log("Listening for FCM messages...");
listenerStarted = true;
function onNotification({ notification, persistentId }) {
console.log(notification, persistentId);
persistentIds.push(persistentId)
// Broadcast new FCM message
if (wss.clients.size > 0) {
wss.clients.forEach(client => {
client.send(JSON.stringify({notification, persistentId}));
})
} else {
notificationQueue.push({notification, persistentId});
}
}
}
wss.on('connection', (ws) => {
ws.on('message', (data) => {
try {
let jsonData = JSON.parse(data.toString('utf-8'))
if (jsonData.request) {
if (jsonData.success) {
switch (jsonData.request) {
case 'get-persistent-ids':
handlePersistentIdReceive(jsonData.response);
}
} else {
console.error("Client responded with error", jsonData)
}
}
} catch (e) {
console.error("Error while processing message:", e)
}
})
ws.send(JSON.stringify({request: 'get-persistent-ids'}));
});