Scheduling tasks with Alarms
PartyKit allows you to schedule an alarm some time in the future, and will wake up your room at the scheduled time. This is especially helpful for tasks like:
- Recurring tasks (cron jobs)
- Cleaning up unused storage
Setting an alarm
You can schedule an alarm by calling Party.storage.setAlarm
.
For example, the following methods sets an alarm 5 minutes from now:
Reacting to alarms
When the alarm âringsâ, PartyKit will call the onAlarm
callback in your Party server.
Waking up rooms
If the room is currently âawakeâ (meaning that someone is connected to it, or, in Hibernation mode, it has recently processed messages), the onAlarm
callback is executed in the current room process.
If the room is currently âasleepâ (itâs not actively handling connections or messages), the PartyKit runtime will load the room into memory, as if it just received a connection or a message. This means that PartyKit will first run the roomâs constructor and the onStart
method, and only then call onAlarm
.
Limitations
One active alarm per room
Each PartyKit room can have only one active alarm at a time. If you call setAlarm
while another alarm is already active, the previous alarm will be canceled in favor of the new one.
You can use Party.storage.getAlarm
to check if an alarm is already set. In this example, we only set the new alarm if it is happening sooner than the alarm which was set previously:
If you need multiple concurrent alarms, you can implement this yourself by storing the alarm times in room storage, and when the first alarm goes off, scheduling the next soonest alarm in the onAlarm
callback.
Accessing Party.id
Because we cannot expect the room to be currently holding connections when the alarm rings, we arenât currently able to automatically deduce the roomâs id
. Because of that, the following will throw an error:
You can work around this limitation by storing the room id
in room storage, and reading it from there:
This is a temporary limitation, and will be lifted in a future version of PartyKit.
Accessing other parties
Normally, you can communicate between different parties and rooms inside the PartyKit runtime as follows:
However, Party.context.parties
is not available inside onAlarm
, so the above code snippet would throw and error.
As a workaround, you can fetch
to the other parties using their public address:
Note that using fetch
is not fully equivalent to the otherParty.fetch
call, as the former is routed via the Internet, and the latter happens inside PartyKitâs runtime.
This is a temporary limitation, and will be lifted in a future version of PartyKit.
Common tasks
Expiring unused room storage
Sometimes, you may want to persist room storage for a limited period, and clean it up when it is no longer being used.
A handy pattern is to keep updating an alarm every time you save data to storage. If the alarm ever rings, you know that the room has not been accessed for that time, and you can safely remove unused storage:
The above example deletes the data after it has not been changed for 30 days. If you wanted to keep the data around 30 days since it was last read, you can also update the same alarm for example when a user connects to the room: