To enhance communication, it is recommended to have a data storage system in place for all subscribed clients on the server, stored in a file named endpoint.txt
. This allows for messages to be efficiently delivered to specific users.
For more information on this topic, refer to: developer.mozilla.org/en/docs/Web/API/Push_API
To customize the function that sends broadcast messages and select individual clients instead of broadcasting to all in a loop, you can modify the code found here: https://github.com/chrisdavidmills/push-api-demo/blob/gh-pages/server.js
if(obj.statusType === 'chatMsg') {
fs.readFile("endpoint.txt", function (err, buffer) {
var string = buffer.toString();
var array = string.split('\n');
for(i = 0; i < (array.length-1); i++) {
var subscriber = array[i].split(',');
webPush.sendNotification(subscriber[2], 200, obj.key, JSON.stringify({
action: 'chatMsg',
name: obj.name,
msg: obj.msg
}));
};
});