Skip to content

Commit

Permalink
server-side push recurring inverval notification
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Dec 22, 2024
1 parent 6a9ad06 commit 6fea6bf
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)

0 comments on commit 6fea6bf

Please sign in to comment.