DEV Community

Alex Spinov
Alex Spinov

Posted on

PartyKit Has a Free API: Build Real-Time Multiplayer Apps With Cloudflare Durable Objects

WebSockets are easy to start, impossible to scale. PartyKit makes multiplayer real-time apps deployable with one command.

What Is PartyKit?

PartyKit is a platform for building real-time, multiplayer, collaborative applications. It runs on Cloudflare's network using Durable Objects for stateful server logic.

// party/main.ts — server logic
import type { Party, Connection } from "partykit/server"

export default class ChatRoom implements Party.Server {
  messages: string[] = []

  onConnect(conn: Connection) {
    // Send message history to new connections
    conn.send(JSON.stringify({ type: "history", messages: this.messages }))
  }

  onMessage(message: string, sender: Connection) {
    this.messages.push(message)
    // Broadcast to ALL connections in this room
    this.room.broadcast(JSON.stringify({ type: "message", text: message }))
  }
}
Enter fullscreen mode Exit fullscreen mode
npx partykit dev   # Local dev
npx partykit deploy  # Deploy globally
Enter fullscreen mode Exit fullscreen mode

Client

import PartySocket from "partysocket"

const ws = new PartySocket({
  host: "my-app.username.partykit.dev",
  room: "chat-room-1",
})

ws.addEventListener("message", (e) => {
  const data = JSON.parse(e.data)
  console.log(data)
})

ws.send("Hello everyone!")
Enter fullscreen mode Exit fullscreen mode

Use Cases

  • Multiplayer games — shared game state across players
  • Collaborative editing — Google Docs-like real-time editing
  • Live dashboards — real-time metrics visible to all viewers
  • Chat — rooms, presence, typing indicators
  • Voting/polling — instant results across all clients

Why PartyKit

  • Rooms — each room is an isolated Durable Object with its own state
  • Global — runs on Cloudflare's 300+ edge locations
  • Stateful server — not just pub/sub, actual server-side logic per room
  • Hibernation — rooms sleep when empty, wake on first connection ($0 when idle)
  • Yjs integration — CRDT-based collaborative editing built in

Free Tier

  • 20 concurrent connections, unlimited rooms, unlimited projects.

Building real-time apps? Check out my developer tools or email spinov001@gmail.com.

Top comments (0)