Hook up data to the server
In the previous step, you’ve set up the PartyKit server to handle messages. In this step, you will connect it to the UI. When the user submits the poll form, the Next.js app will send it to your PartyKit server.
Connect the poll form
Navigate to page.tsx
, in the app
directory, which generates the poll (see online in the starter or the finished code).
Take a look at the file. You’ll notice that we’ve already included the code to get the data from the form in the server action:
Now you need to send it to PartyKit with an HTTP request to the server:
In the example project, we’ve already defined the PARTYKIT_URL
variable and set it to the default PartyKit development server address. In the final step of this tutorial, you’ll deploy your project to PartyKit and set this variable’s value to your project’s address.
To test if the code you wrote works, try creating a new poll in the browser. Your form creates a poll but there’s a problem: the data is static. Let’s change that.
Connect the poll page
Navigate to the file which renders the poll page: page.tsx
in the app/[poll_id]
directory (see online in the starter or the finished code).
In the previous step of this tutorial, your onRequest
method returned the poll data. You can now use it to render the poll on the server:
Finally, read the response from the server and replace the mock data:
Time to validate that your poll page works. Go to the page and see if the data you entered in the form renders now on the new poll page.
Next steps
Congratulations! This page works so let’s make it better. Let’s make the votes update in real time.
🎈 If you’d like to check how your code compares to the finished app, check the finished code (app/page.tsx
and app/[poll_id]/page.tsx
) online 🎈