advanced
+500 XP

Production Deployment & Advanced Patterns

Deploy your messaging app to mainnet with multi-RPC failover, monitoring, and error recovery.

Lesson Syllabus

Mainnet Configuration
🚀

Switching from Testnet to Mainnet

Moving to mainnet requires careful configuration changes. Replace your testnet RPC URL with a mainnet endpoint, set environment variables for network selection, and ensure your package IDs point to mainnet-deployed contracts. Never hardcode RPC URLs -- use environment variables so you can switch networks without code changes. The @mysten/messaging SDK respects the SuiClient network configuration you provide.

🔧

Environment-Based Configuration

Create a configuration module that reads from environment variables and provides typed defaults. Use NEXT_PUBLIC_SUI_NETWORK for the network name and NEXT_PUBLIC_SUI_RPC_URL for the endpoint. This pattern lets you deploy the same code to staging (testnet) and production (mainnet) by changing environment variables. Always validate config at startup to catch misconfigurations early.

🔄

Multi-RPC Failover Pattern

Production apps need redundancy. If your primary RPC provider goes down, your app should automatically failover to a secondary endpoint. Create a custom SuiClient wrapper that tries multiple RPC URLs in order. Log failover events for monitoring. Popular Sui RPC providers include Mysten Labs, Shinami, BlockVision, and Triton. Use at least two providers for reliability.

Advanced Patterns
📦

Message Batching & Offline Queue

In production, users may send messages while briefly offline or during network hiccups. Implement an offline queue that stores unsent messages in memory (or IndexedDB for persistence) and automatically retries when the connection is restored. For high-throughput channels, batch multiple messages into a single Sui transaction using a Programmable Transaction Block to save gas costs.

🔌

Reconnection Logic

Network connections drop. Your app needs a reconnection strategy with exponential backoff and jitter. Track connection state to show users a 'Reconnecting...' banner. When reconnected, fetch missed messages by requesting all messages since the last known timestamp. Add a heartbeat ping every 30 seconds to detect stale connections early.

🚦

Rate Limiting & Throttling

Protect your app and RPC provider by implementing client-side rate limiting. Throttle message sends to a maximum rate (e.g., 1 message per second per user). Debounce polling requests to avoid hammering the RPC when rapidly switching channels. Use a token bucket algorithm for smooth rate control that allows short bursts while maintaining average limits.

Error Recovery & Monitoring
🛡

Error Boundaries & Graceful Degradation

Wrap your messaging components in React Error Boundaries to catch rendering errors without crashing the whole app. Create a MessagingErrorBoundary that shows a friendly 'Chat unavailable' fallback with a retry button. For transaction errors, categorize them: network errors should retry, insufficient gas errors should prompt the user, and contract errors should log and alert. Always show user-friendly messages, never raw error strings.

📊

Health Checks & Production Monitoring

Implement health checks that verify RPC connectivity, messaging contract accessibility, and wallet connection status. Create a monitoring dashboard that tracks message send latency, RPC error rates, and transaction success rates. Emit structured logs for every messaging operation. Use a health check endpoint that your uptime monitor can ping to detect outages before users report them.