0/12
12. Cron Jobs

Lesson 12: 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. When the cron fires (every minute), any messages older than 60 seconds will vanish from both previews automatically.

WebSocket
0
No messages yet
User A
User B