Add storage
PartyKit rooms come with a key-value storage, which make it easy to persist data without the need for an external database. It is also a good way to ensure that your data will persist in case of, for example, server restart or app redeployment. In this step, you will enable your server to store the votes.
Loading data on start
Navigate to the server file (party/index.ts
) and add a new method called onStart
. This method will be triggered when the first connection is made to the PartyKit room or after the server restarts:
Persisting data
Next, give your app possibility to also add data to storage. Create a helper method:
And then invoke it in onRequest
(to save the poll when itβs created) by adding just one line of code:
and invoke it again in onMessage
to save the poll when user votes:
And - thatβs it! You are now persisting data on the PartyKit server.
Next steps
Congratulations! Your polling app is working! Now itβs time to deploy it π₯³
π If youβd like to check how your code compares to the finished app, check the finished code online π