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 minutes0 * * * *- every hour
Try it
The starter has a message feed. Your task:
- Add a
MAX_AGE_MSconstant (60000 = 60 seconds) - Export a
cleanupcron withlive.cron('* * * * *', ...) - 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