This guide explains how to set up and run the distributed multiplayer system for myMCP.
The multiplayer system consists of:
- Multiple Engine Instances: 1 primary and 3 worker engines (ports 3000-3003)
- Redis Pub/Sub: For cross-engine communication
- Socket.IO: For real-time client connections
- Shared Game State: Each engine maintains its own state but broadcasts updates
- Node.js 18+
- Docker (for Redis and multi-engine setup)
- Redis (can be run via Docker)
- Build and start all services:
npm run dev:multiplayer:build
npm run dev:multiplayer- Stop all services:
npm run dev:multiplayer:down- Start Redis:
npm run redis:start- Start the primary engine:
npm run dev:engine:primary- Start worker engines (in separate terminals):
npm run dev:engine:worker1
npm run dev:engine:worker2
npm run dev:engine:worker3- Stop Redis when done:
npm run redis:stop- Open
examples/multiplayer-demo.htmlin multiple browser windows - Select different engines from the dropdown
- Try sending messages in the global chat
- Watch as player presence updates across all windows
Connect different CLI instances to different engines:
# Terminal 1 - Connect to primary engine
cd packages/cli
ENGINE_URL=http://localhost:3000 node dist/index.js chat -i
# Terminal 2 - Connect to worker 1
cd packages/cli
ENGINE_URL=http://localhost:3001 node dist/index.js chat -iThe MCP server connects to the primary engine by default but is aware of the multiplayer setup:
ENGINE_URL=http://localhost:3000 MULTIPLAYER_MODE=true npm run dev:mcpserverEach engine exposes a multiplayer status endpoint:
# Check multiplayer status
curl http://localhost:3000/api/multiplayer/status- Players connected to different engines can see each other
- Global chat messages are synchronized across all engines
- Quest progress is shared in real-time
- Online/offline status tracked across engines
- Player location updates broadcast to relevant players
- Session management with automatic cleanup
- Messages sent to one engine appear on all engines
- System announcements can be broadcast globally
- Chat history maintained per session
- Each engine maintains its own persistent state
- Updates are broadcast via Redis pub/sub
- Automatic synchronization of player actions
# Engine configuration
PORT=3000 # Engine port
ENGINE_ID=engine-primary # Unique engine identifier
IS_PRIMARY=true # Primary engine flag
REDIS_URL=redis://localhost:6379
# Multiplayer features
MULTIPLAYER_MODE=true # Enable multiplayer featuresThe engines are configured to accept connections from:
- http://localhost:3001-3003 (other engines)
- http://localhost:5173 (Vite dev server)
- Any local file:// URLs (for HTML demos)
- Ensure Redis is running:
docker ps | grep redis - Check Redis connectivity:
redis-cli ping
- Verify CORS settings in engine configuration
- Check browser console for connection errors
- Ensure the selected engine is running
- Verify all engines are connected to the same Redis instance
- Check multiplayer status endpoint on each engine
- Ensure player identification is sent after connection
- Update
docker-compose.multiplayer.ymlto add more workers - Update the
peerEnginesarray in MultiplayerService - Add new npm scripts for additional workers
Add new event types in MultiplayerService.ts:
// Subscribe to custom events
this.subClient.subscribe('game:custom:event');
// Publish custom events
this.publishEvent('game:custom:event', {
type: 'CUSTOM_EVENT',
data: { /* your data */ }
});The architecture supports integration with:
- Slack (via Slack Bolt SDK)
- Discord (via discord.js)
- Custom web applications
- Mobile apps via Socket.IO clients
- Each engine handles its own subset of players
- Redis pub/sub is used for lightweight message passing
- Game state is persisted locally on each engine
- Consider using Redis Cluster for production deployments