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