From 6fea6bf011b8bada2cf408aa266b8d58dccdb903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Iv=C3=A1n=20Vieitez=20Parra?= <3857362+corrideat@users.noreply.github.com> Date: Sun, 22 Dec 2024 16:58:09 +0000 Subject: [PATCH] server-side push recurring inverval notification --- backend/server.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/server.js b/backend/server.js index 2fa115608..2df58353b 100644 --- a/backend/server.js +++ b/backend/server.js @@ -17,7 +17,7 @@ import { createPushErrorResponse, createServer } from './pubsub.js' -import { addChannelToSubscription, deleteChannelFromSubscription, pushServerActionhandlers, subscriptionInfoWrapper } from './push.js' +import { addChannelToSubscription, deleteChannelFromSubscription, postEvent, pushServerActionhandlers, subscriptionInfoWrapper } from './push.js' import { GIMessage } from '~/shared/domains/chelonia/GIMessage.js' import type { SubMessage, UnsubMessage } from '~/shared/pubsub.js' @@ -378,3 +378,18 @@ sbp('okTurtles.data/set', PUBSUB_INSTANCE, createServer(hapi.listener, { console.info('Backend server running at:', hapi.info.uri) sbp('okTurtles.events/emit', SERVER_RUNNING, hapi) })() + +// Recurring task to send messages to push clients (for periodic notifications) +setInterval(() => { + const pubsub = sbp('okTurtles.data/get', PUBSUB_INSTANCE) + // Notification text + const notification = JSON.stringify({ type: 'recurring' }) + // Find push subscriptions that do _not_ have a WS open. This means clients + // that are 'asleep' and that might be woken up by the push event + Object.values(pubsub.pushSubscriptions || {}).filter((pushSubscription) => pushSubscription.sockets.size === 0).forEach((pushSubscription) => { + postEvent(pushSubscription, notification).catch((e) => { + console.warn(e, 'Error sending recurring message to web push client', pushSubscription.id) + }) + }) +// Repeat every 12 hours +}, 12 * 60 * 60 * 1000)