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/core2. 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
| Property | Description |
|---|---|
room.code | Room code to share |
room.playerId | Your player ID |
room.isHost | Are you the host? |
room.players | All players in room |
room.history | Recent events |
room.connected | WebSocket 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.isHostfor authoritative game logic - No persistence — Rooms are ephemeral; use your own storage if needed
- Auto-reconnect — Built in, just works (unless kicked)
Need More?
- Quick Start Guide — Step-by-step tutorial
- SDK Reference — Complete API documentation
- Game Patterns — Common implementation patterns