Tapaculo
A lightweight Rust server that handles real-time and turn-based communication for any multiplayer client interactions.
Features
- ** Pluggable PubSub**: Swap between in-memory and Redis backends without code changes
- ** JWT Authentication**: Secure token-based auth with session tracking and reconnection support
- ** Room Management**: Capacity limits, player tracking, and lifecycle events
- ** Real-time WebSocket**: Bidirectional communication with automatic cleanup
- ** Rate Limiting**: Built-in spam prevention and abuse protection
- ** Message History**: Optional replay for new joiners
- ** User Metadata**: Associate custom data with players
- ** Typed Messages**: Type-safe message handling with serde
- ** Production Ready**: Comprehensive error handling, logging, and reconnection logic
Quick Start
[]
= "0.2"
Basic Server
use *;
async
Use Cases
Chess Server (2 players, turn-based)
use *;
use ;
async
;
Chat Server (group messaging)
new
.with_auth
.with_pubsub
.with_room_settings
.with_limits
.on_message
.listen
.await
Core Concepts
1. Room Management
Rooms are isolated multiplayer sessions with configurable limits:
let room_settings = RoomSettings ;
new
.with_room_settings
// ...
2. Broadcast Filtering
Send messages to specific players:
// To all players in room
ctx.broadcast.await?;
// To all OTHER players (exclude sender)
ctx.broadcast_to_others.await?;
// Custom filter
ctx.broadcast_filtered.await?;
// Direct message to one player
ctx.send_to.await?;
3. Room Lifecycle Events
React to room state changes:
;
new
.with_event_handler
// ...
4. Room State Queries
Access room information:
// Get all members in room
let members = ctx.get_room_members.await;
// Check if specific user is in room
if ctx.has_member.await
// Get room info
if let Some = ctx.get_room_info.await
// Get message history
let history = ctx.get_message_history.await;
5. Rate Limiting
Prevent spam and abuse:
let limits = MessageLimits ;
new
.with_limits
// ...
6. Message Validation
Validate messages before processing:
new
.on_message_validate
// ...
7. Typed Message Handlers
Type-safe message processing:
new
.
// ...
8. User Metadata
Store custom player data:
// Set metadata
let metadata = PlayerMetadata ;
ctx.set_user_metadata.await?;
// Get metadata
if let Some = ctx.get_user_metadata.await
9. Reconnection Support
Sessions persist across connections:
// Generate token with session ID
let auth = new;
let token = auth.sign_access?;
// Later, reconnect with same session ID
let new_token = auth.sign_access?;
// Access session ID in handler
ctx.session_id; // "session-abc"
PubSub Backends
In-Memory (Development)
let pubsub = new;
// or with custom buffer size
let pubsub = with_buffer;
Redis (Production)
[]
= { = "0.2", = ["redis-backend"] }
let pubsub = new?;
// With custom retry configuration
let config = BackoffConfig ;
let pubsub = with_config?;
Authentication
Creating Tokens
let auth = new;
// Access token
let access_token = auth.sign_access?;
// Refresh token
let refresh_token = auth.sign_refresh?;
// Refresh access token
let new_access = auth.refresh_access?;
Client Connection
const token = "eyJ0eXAiOiJKV1QiLCJhbGc...";
const ws = ;
ws ;
// Send message
ws.;
Examples
See the examples/ directory for complete implementations:
chess_server.rs: 2-player chess with move validationchat_server.rs: Group chat with history and typing indicators
Run examples:
Architecture
┌──────────────────────────────────────────────┐
│ WebSocket Connections │
│ (JWT Auth + Session Tracking) │
└──────────┬───────────────────────────────────┘
│
┌──────────▼──────────────────────────────────┐
│ Server (Room Management) │
│ • Rate Limiting │
│ • Message Validation │
│ • Event Handlers │
│ • User Metadata │
└──────────┬──────────────────────────────────┘
│
┌──────────▼──────────────────────────────────┐
│ PubSub Backend │
│ ┌────────────────┬──────────────────────┐ │
│ │ InMemoryPubSub│ RedisPubSub │ │
│ │ (Single Node) │ (Distributed) │ │
│ └────────────────┴──────────────────────┘ │
└─────────────────────────────────────────────┘
Production Checklist
- Use Redis backend for multi-server deployments
- Enable rate limiting to prevent spam
- Set appropriate room size limits
- Configure empty room timeouts
- Use strong JWT secrets (env variables)
- Add message validation for your use case
- Implement custom event handlers for game logic
- Enable message history if needed
- Set up structured logging (tracing)
- Monitor rate limit bans
- Handle reconnections gracefully
License
MIT
Contributing
Contributions welcome! Please open an issue or PR.