Skip to content

Reminders Example

Code below is showing how to schedule 2 reminders that should be sent to user before some event in our app occurs. We'll also show how to remove or update these when user updates event. All this without storing any LambdaQueue related data in your app.

Architectural overview

We will use 2 separate queues and a common key, so we can further delay, remove or update notifications without need to persist any queue item IDs in our application. This way we we'll limit codebase changes to minimum.

Pushing event with same key into a queue will overwrite current items. We can take advantage of that to update items whenever something changes.

When notifications are no longer needed, we can just delete them by app specific key (event.id in this case)

Code example

ts
import lq from '@dayone-labs/lambda-queue-express'
import { subDays, subMinutes } from 'date-fns'
import './models'

const dayBeforeQueue = lq.queue('/reminder1d', async (event: AppEvent) => {
  await emailService.sendEventNotification(event)
})
const minutesBeforeQueue = lq.queue('/reminder15m', async (event: AppEvent) => {
  await emailService.sendEventNotification(event)
})

const onNewEvent = async (event: AppEvent) => {
  await Promise.all([
    dayBeforeQueue.push(event, {
      key: event.id,
      on: subDays(event.date, 1),
    }),
    minutesBeforeQueue.push(event, {
      key: event.id,
      on: subMinutes(event.date, 15),
    }),
  ])
}

const onEventUpdate = onNewEvent
const onEventDelete = async (event: AppEvent) => {
  await Promise.all([
    dayBeforeQueue.deleteByKey(event.id),
    minutesBeforeQueue.deleteByKey(event.id),
  ])
}

export const route = lq.compose(dayBeforeQueue, minutesBeforeQueue)
//Queue is an Express router, just mount it with app.use(queue)
//You can then call on* functions whenever your data changes
export default {
  route,
  onNewEvent,
  onEventUpdate,
  onEventDelete,
}

Remarks

TIP

🤔 A nice pattern can be to have an application global event bus that will notify whenever an event update happened. You could then just listen on these and modify queues as needed, without touching business logic.

All examples are licensed under MIT. You can use them for free in your projects.