Vibe Coding with Watchtower

Building multiplayer games with AI assistance? This page is for you.

Whether you're using Cursor, Claude, Copilot, or any other AI coding assistant, Watchtower's simple API makes it easy to add multiplayer to any project. One import, one function call, done.

Quick Reference Download

Download our single-page markdown reference to include in your AI context:

Add this file to your project root, @docs folder, or paste it directly into your AI context.

The Essentials

Here's what you need to know:

1. Install

npm install @watchtower-sdk/core

2. Connect

import { connect } from '@watchtower-sdk/core'

const room = await connect('room-name')

3. Send Messages

// To everyone
room.broadcast({ x: 100, y: 200 })

// To one player
room.send('player123', { private: 'message' })

4. Receive Messages

room.on('message', (from, data, meta) => {
  // Handle incoming data
})

5. Track Players

room.on('join', (player) => { /* new player */ })
room.on('leave', (player) => { /* player left */ })

room.players     // current player list
room.isHost      // am I the host?

Common Patterns

Cursor Sharing

document.onmousemove = (e) => {
  room.broadcast({ x: e.clientX, y: e.clientY })
}

room.on('message', (from, pos) => {
  cursors[from] = pos
})

Turn-Based

// Host manages turn order
if (room.isHost && currentTurn === room.playerId) {
  room.broadcast({ type: 'move', data: moveData })
}

Host Authority

// Only host processes game state
room.on('message', (from, data) => {
  if (room.isHost) {
    const result = processAction(data)
    room.broadcast({ type: 'state', result })
  }
})

Debugging

Enable debug mode to see all WebSocket activity in the console:

const room = await connect('my-room', { debug: true })

// Console shows:
// [Watchtower] Connecting to my-room
// [Watchtower] Connected! playerId: p_abc123 isHost: true
// [Watchtower] ← join {...}
// [Watchtower] → broadcast {...}

Key Properties

PropertyDescription
room.codeRoom code to share
room.playerIdYour player ID
room.isHostAre you the host?
room.playersAll players in room
room.historyRecent events
room.connectedWebSocket status

Tips for AI Sessions

  • Keep it simple — One connect(), broadcast what you need, done
  • Use debug mode — Add { debug: true } to see what's happening
  • Host authority — Use room.isHost for authoritative game logic
  • No persistence — Rooms are ephemeral; use your own storage if needed
  • Auto-reconnect — Built in, just works (unless kicked)

Need More?