0/24
21. Cron Jobs

Lesson 21: Cron Jobs

live.cron() schedules a function to run automatically on a repeating schedule using standard cron expressions.

How it works

export const job = live.cron('* * * * *', (ctx) => {
  // runs every minute
  ctx.publish('topic', 'event', data);
});

The function receives ctx with publish(), just like a regular RPC. Cron uses standard 5-field expressions (minute, hour, day, month, weekday):

  • * * * * * - every minute
  • */5 * * * * - every 5 minutes
  • 0 * * * * - every hour

Try it

The starter has a message feed. Your task:

  1. Add a MAX_AGE_MS constant (60000 = 60 seconds)
  2. Export a cleanup cron with live.cron('* * * * *', ...)
  3. Inside it, filter messages older than MAX_AGE_MS, splice them from the array, and publish 'deleted' for each

Post some messages from both users and wait. The cron fires at the top of each minute (:00). A message must be 60+ seconds old when the cron ticks, so cleanup happens between 60 and 120 seconds after posting.

WebSocket
0
No messages yet
User A
User B