Build Real-Time Multiplayer Apps with PartyKit: A Step-by-Step Guide
PartyKit is a serverless real-time infrastructure that lets you build multiplayer apps without managing WebSocket servers. It handles state, rooms, and connections so you can focus on your game or collaboration tool. This tutorial walks you through creating a basic multiplayer app.
First, install the PartyKit CLI globally: npm install -g partykit. Then scaffold your project with partykit init my-multiplayer-app and navigate into the folder. You’ll see a server.ts file – this is where your room logic lives.

Setting Up a PartyKit Server
Edit server.ts to define how rooms behave. Export a class that extends Room:
onConnect(connection, ctx)– runs when a client joins. Usectx.room.setUserStateto store user data.onMessage(message, sender)– processes incoming messages. Broadcast state changes to all clients withthis.broadcast.
Managing Shared State
Use this.state to persist data across connections. For example, store player positions in a JSON object. In onMessage, update the state and broadcast it:
this.state = { ...this.state, [sender.id]: newPos }this.broadcast('state', this.state)
Connecting from the Client
Install partysocket on the frontend: npm install partysocket. Initialize a connection:
const socket = new PartySocket({ host: 'localhost:1999', room: 'game-room' })- Listen for
socket.onmessageto receive state updates and render them. - Send movement commands with
socket.send(JSON.stringify({ x, y })).
Running and Deploying
Start locally with npx partykit dev. For production, deploy with npx partykit deploy – PartyKit provides a public URL.
PartyKit removes WebSocket boilerplate, letting you build collaborative apps in minutes. Experiment with rooms, persistence, and real-time events to create your multiplayer experience.