-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice-worker.js
39 lines (31 loc) · 1.15 KB
/
service-worker.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
//Get all the connected clients and forward the message along.
self.addEventListener('message', function(event) {
var promise = self.clients.matchAll()
.then(function(clientList) {
//event.source.id contains the ID of the sender of the message.
var senderID = event.source.id;
clientList.forEach(function(client) {
//Skip sending the message to the client that sent it.
if (client.id === senderID) {
return;
}
client.postMessage({
client: senderID,
message: event.data
});
});
});
//If event.waitUntil is defined, use it to extend the lifetime of the Service Worker.
if (event.waitUntil) {
event.waitUntil(promise);
}
});
//Immediately claim any new clients. This is not needed to send messages, but makes for a better experience since the user does not need to refresh.
self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim());
});
//someday we'll be able to get the local clinet's ID to add on as a feature?
// self.addEventListener('fetch', function(event) {
// // console.log(event.clientId);
// event.respondWith(event.clientId)
// });