Claude Code for PartyKit: Real-Time Multiplayer Backends — Claude Skills 360 Blog
Blog / AI / Claude Code for PartyKit: Real-Time Multiplayer Backends
AI

Claude Code for PartyKit: Real-Time Multiplayer Backends

Published: July 14, 2027
Read time: 6 min read
By: Claude Skills 360

PartyKit makes real-time multiplayer apps with a room-based edge server model — a PartyServer class with onConnect(conn, room), onMessage(message, sender, room), and onClose(conn, room) lifecycle methods handles server logic. party.broadcast(message) sends to all connections in a room. party.broadcast(message, [exclude]) sends to all except a connection. room.storage.get("key") and room.storage.put("key", value) persist state per room using Durable Objects storage. The client: new PartySocket({ host, room }) is a WebSocket with automatic reconnection. React: usePartySocket({ host, room, onMessage }) from partysocket/react. Presence pattern: on connect, broadcast a join message; on close broadcast leave; track a connections: Map<id, data> on the server. conn.id is the unique connection identifier. conn.setState(data) places arbitrary data on the connection. room.connections iterates all connected clients. File: partykit.json at project root sets name and main. Deploy: npx partykit deploy. Y.js integration: import { YPartyKitProvider } from "y-partykit/provider"new YPartyKitProvider(host, room, ydoc) syncs a Yjs document across all peers via PartyKit. Claude Code generates PartyKit multiplayer chat, collaborative editors, cursor sharing, and presence systems.

CLAUDE.md for PartyKit

## PartyKit Stack
- Version: partykit >= 0.0.98, partysocket >= 1.x
- Config: partykit.json — { "name": "my-app", "main": "party/index.ts" }
- Server: export default class MyServer implements Party.Server { onConnect(conn, room) {} onMessage(msg, sender, room) {} onClose(conn, room) {} }
- Broadcast: party.broadcast(JSON.stringify(event)) — sends to all, party.broadcast(msg, [sender.id]) excludes sender
- Storage: await room.storage.get<T>("key"); await room.storage.put("key", value)
- Client: const socket = new PartySocket({ host: PARTYKIT_HOST, room: roomId })
- React hook: const ws = usePartySocket({ host, room, onMessage: (e) => handleMsg(e.data) })
- Connection ID: conn.id — unique per connected client

PartyKit Server

// party/index.ts — PartyKit server with presence and chat
import type * as Party from "partykit/server"

export type ChatMessage = {
  type: "chat"
  id: string
  userId: string
  name: string
  text: string
  timestamp: number
}

export type PresenceEvent =
  | { type: "join";  connectionId: string; userId: string; name: string; color: string }
  | { type: "leave"; connectionId: string }
  | { type: "presence"; connections: PresenceUser[] }

export type PresenceUser = {
  connectionId: string
  userId: string
  name: string
  color: string
  cursor?: { x: number; y: number }
}

export type ClientMessage =
  | { type: "identify"; userId: string; name: string; color: string }
  | { type: "chat"; text: string }
  | { type: "cursor"; x: number; y: number }

export default class ChatRoom implements Party.Server {
  // Track in-memory presence (conn.id → user data)
  private presence = new Map<string, PresenceUser>()

  constructor(readonly room: Party.Room) {}

  async onConnect(conn: Party.Connection, ctx: Party.ConnectionContext) {
    // Send existing presence to the new connection
    const existing = [...this.presence.values()]
    conn.send(JSON.stringify({ type: "presence", connections: existing } satisfies PresenceEvent))

    // Load and send recent chat history from storage
    const history = await this.room.storage.get<ChatMessage[]>("history") ?? []
    conn.send(JSON.stringify({ type: "history", messages: history }))
  }

  async onMessage(raw: string | ArrayBuffer, sender: Party.Connection) {
    const message = JSON.parse(raw.toString()) as ClientMessage

    if (message.type === "identify") {
      // Register user info for this connection
      const user: PresenceUser = {
        connectionId: sender.id,
        userId: message.userId,
        name: message.name,
        color: message.color,
      }
      this.presence.set(sender.id, user)

      // Tell everyone about the new joiner
      this.room.broadcast(
        JSON.stringify({ type: "join", ...user } satisfies PresenceEvent),
        [sender.id],
      )
      return
    }

    if (message.type === "chat") {
      const user = this.presence.get(sender.id)
      if (!user) return // Not identified yet

      const chatMsg: ChatMessage = {
        type: "chat",
        id: `${Date.now()}-${Math.random().toString(36).slice(2)}`,
        userId: user.userId,
        name: user.name,
        text: message.text.slice(0, 2000), // cap length
        timestamp: Date.now(),
      }

      // Persist to room history (keep last 50 messages)
      const history = await this.room.storage.get<ChatMessage[]>("history") ?? []
      history.push(chatMsg)
      if (history.length > 50) history.splice(0, history.length - 50)
      await this.room.storage.put("history", history)

      // Broadcast to all including sender
      this.room.broadcast(JSON.stringify(chatMsg))
      return
    }

    if (message.type === "cursor") {
      const user = this.presence.get(sender.id)
      if (!user) return
      user.cursor = { x: message.x, y: message.y }
      // Broadcast cursor update to others only
      this.room.broadcast(
        JSON.stringify({ type: "cursor", connectionId: sender.id, x: message.x, y: message.y }),
        [sender.id],
      )
      return
    }
  }

  onClose(conn: Party.Connection) {
    this.presence.delete(conn.id)
    this.room.broadcast(
      JSON.stringify({ type: "leave", connectionId: conn.id } satisfies PresenceEvent),
    )
  }
}

React Multiplayer Chat

// components/chat/MultiplayerChat.tsx — real-time chat with presence
"use client"
import { useState, useEffect, useRef, useCallback } from "react"
import usePartySocket from "partysocket/react"
import type { ChatMessage, PresenceUser, ClientMessage, PresenceEvent } from "@/party/index"

const COLORS = ["#ef4444","#f97316","#eab308","#22c55e","#3b82f6","#8b5cf6","#ec4899"]

function randomColor() { return COLORS[Math.floor(Math.random() * COLORS.length)] }

type Props = { roomId: string; userId: string; userName: string }

export default function MultiplayerChat({ roomId, userId, userName }: Props) {
  const [messages, setMessages] = useState<ChatMessage[]>([])
  const [presence, setPresence] = useState<PresenceUser[]>([])
  const [input, setInput] = useState("")
  const bottomRef = useRef<HTMLDivElement>(null)
  const userColor = useRef(randomColor())

  const ws = usePartySocket({
    host: process.env.NEXT_PUBLIC_PARTYKIT_HOST!,
    room: roomId,

    onOpen() {
      // Identify ourselves once connected
      ws.send(JSON.stringify({
        type: "identify",
        userId,
        name: userName,
        color: userColor.current,
      } satisfies ClientMessage))
    },

    onMessage(event) {
      const data = JSON.parse(event.data)

      if (data.type === "history") {
        setMessages(data.messages)
        return
      }
      if (data.type === "chat") {
        setMessages((prev) => [...prev, data as ChatMessage])
        return
      }
      if (data.type === "presence") {
        setPresence((data as PresenceEvent & { type: "presence" }).connections)
        return
      }
      if (data.type === "join") {
        const { type: _, ...user } = data as PresenceEvent & { type: "join" }
        setPresence((prev) => [...prev.filter((u) => u.connectionId !== user.connectionId), user])
        return
      }
      if (data.type === "leave") {
        const { connectionId } = data as PresenceEvent & { type: "leave" }
        setPresence((prev) => prev.filter((u) => u.connectionId !== connectionId))
        return
      }
    },
  })

  // Auto-scroll to bottom on new messages
  useEffect(() => {
    bottomRef.current?.scrollIntoView({ behavior: "smooth" })
  }, [messages])

  const sendMessage = useCallback(() => {
    const text = input.trim()
    if (!text) return
    ws.send(JSON.stringify({ type: "chat", text } satisfies ClientMessage))
    setInput("")
  }, [input, ws])

  return (
    <div className="flex h-[600px] rounded-xl border overflow-hidden bg-white">
      {/* Sidebar: presence */}
      <aside className="w-48 border-r bg-gray-50 p-3 flex flex-col gap-2">
        <h3 className="text-xs font-semibold uppercase tracking-wide text-gray-400 mb-1">
          Online ({presence.length})
        </h3>
        {presence.map((u) => (
          <div key={u.connectionId} className="flex items-center gap-2">
            <span className="w-2 h-2 rounded-full" style={{ background: u.color }} />
            <span className="text-sm text-gray-700 truncate">{u.name}</span>
            {u.userId === userId && (
              <span className="text-xs text-gray-400 ml-auto">(you)</span>
            )}
          </div>
        ))}
      </aside>

      {/* Chat area */}
      <div className="flex flex-col flex-1">
        <div className="flex-1 overflow-y-auto p-4 space-y-3">
          {messages.map((msg) => {
            const isOwn = msg.userId === userId
            return (
              <div key={msg.id} className={`flex ${isOwn ? "justify-end" : "justify-start"}`}>
                <div className={`max-w-xs lg:max-w-sm ${isOwn ? "items-end" : "items-start"} flex flex-col gap-1`}>
                  {!isOwn && (
                    <span className="text-xs text-gray-400 ml-1">{msg.name}</span>
                  )}
                  <div
                    className={`px-4 py-2 rounded-2xl text-sm ${isOwn ? "bg-indigo-500 text-white" : "bg-gray-100 text-gray-800"}`}
                  >
                    {msg.text}
                  </div>
                </div>
              </div>
            )
          })}
          <div ref={bottomRef} />
        </div>

        {/* Input */}
        <div className="p-3 border-t flex gap-2">
          <input
            className="flex-1 rounded-xl border border-gray-200 px-4 py-2 text-sm focus:ring-2 focus:ring-indigo-400 focus:border-transparent"
            placeholder="Message…"
            value={input}
            onChange={(e) => setInput(e.target.value)}
            onKeyDown={(e) => e.key === "Enter" && !e.shiftKey && sendMessage()}
          />
          <button
            onClick={sendMessage}
            disabled={!input.trim()}
            className="px-4 py-2 rounded-xl bg-indigo-500 text-white text-sm font-medium disabled:opacity-40 hover:bg-indigo-600 transition-colors"
          >
            Send
          </button>
        </div>
      </div>
    </div>
  )
}

Collaborative Y.js Document with PartyKit

// components/collab/CollabEditor.tsx — Y.js synced rich text editor
"use client"
import { useEffect, useRef } from "react"
import * as Y from "yjs"
import { YPartyKitProvider } from "y-partykit/provider"

export default function CollabEditor({ roomId, userId }: { roomId: string; userId: string }) {
  const containerRef = useRef<HTMLDivElement>(null)
  const editorRef = useRef<{ destroy: () => void } | null>(null)

  useEffect(() => {
    const ydoc = new Y.Doc()
    const provider = new YPartyKitProvider(
      process.env.NEXT_PUBLIC_PARTYKIT_HOST!,
      `doc-${roomId}`,
      ydoc,
      { connect: true },
    )

    // Awareness (cursor/user info)
    provider.awareness.setLocalStateField("user", {
      id: userId,
      color: "#" + Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, "0"),
    })

    // Mount a textarea wired to a Y.Text CRDT — swap for Quill/TipTap as needed
    const yText = ydoc.getText("content")
    const textarea = document.createElement("textarea")
    textarea.className = "w-full h-full p-4 resize-none focus:outline-none text-sm font-mono"
    textarea.value = yText.toString()
    containerRef.current?.appendChild(textarea)

    const updateTextarea = () => { textarea.value = yText.toString() }
    yText.observe(updateTextarea)

    textarea.addEventListener("input", () => {
      const delta = textarea.value
      if (delta !== yText.toString()) {
        ydoc.transact(() => {
          yText.delete(0, yText.length)
          yText.insert(0, delta)
        })
      }
    })

    editorRef.current = {
      destroy: () => {
        yText.unobserve(updateTextarea)
        provider.destroy()
        ydoc.destroy()
        textarea.remove()
      },
    }

    return () => editorRef.current?.destroy()
  }, [roomId, userId])

  return (
    <div
      ref={containerRef}
      className="w-full h-96 rounded-xl border overflow-hidden bg-white"
    />
  )
}

For the Liveblocks alternative when needing a hosted presence and conflict-free collaborative editing service with built-in room management, conflict resolution, and a generous free tier — Liveblocks abstracts more infrastructure while PartyKit gives you full server code control as a Cloudflare Durable Object, allowing arbitrary server logic like AI agents running in the room itself, see the Liveblocks guide. For the Ably alternative when needing a battle-tested pub/sub service with global message delivery guarantees, message history replay, presence at scale, and enterprise SLAs — Ably is a managed channels product while PartyKit is a developer-first framework for writing stateful edge servers with co-located storage and WebSocket logic, see the Ably guide. The Claude Skills 360 bundle includes PartyKit skill sets covering multiplayer rooms, presence, and collaborative Y.js editors. Start with the free tier to try real-time multiplayer generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free