Building a Telegram Game: From Unity to Mini App
Introduction
At Ferne::Labs, we built Vikare Battle - a multiplayer battle game that runs entirely inside Telegram. Here's how we architected the system and the challenges we overcame.
The Stack
- Game Engine: Unity WebGL for the gameplay
- Frontend: React for the Telegram Mini App shell
- Backend: Firebase serverless functions
- Blockchain: On-chain game logic and NFT integration
Challenge 1: Unity in Telegram
Telegram Mini Apps are essentially web pages running inside the Telegram client. To serve a Unity game, we compile it to WebGL and embed it in a React wrapper that communicates with the Telegram SDK.
The key challenge is performance. Mobile devices have limited resources, and running a Unity WebGL game inside a webview inside Telegram means we need to be extremely careful about memory usage and rendering.
// Telegram Mini App initialization
const tg = window.Telegram.WebApp;
tg.ready();
tg.expand();
// Bridge between React shell and Unity instance
const unityInstance = await createUnityInstance(canvas, {
dataUrl: "/Build/game.data",
frameworkUrl: "/Build/game.framework.js",
codeUrl: "/Build/game.wasm",
});
Challenge 2: Real-Time State Synchronization
For multiplayer features like leaderboards and battles, we needed real-time state sync. We used PostgreSQL for persistent state and Redis for real-time data like active sessions and leaderboard rankings.
Challenge 3: Wallet Integration
Integrating cryptocurrency wallets into a Telegram game requires careful UX design. Most Telegram users aren't crypto-native, so we implemented a seamless flow where wallet creation happens in the background.
Architecture Decisions
- Serverless backend: Firebase functions scale automatically with player count
- Event-driven updates: Game events trigger backend processes asynchronously
- Caching aggressively: Leaderboard data is cached with TTL-based invalidation
- Progressive loading: Game assets load incrementally to reduce initial load time
Results
The game attracted thousands of active players, with daily tasks, weekly leaderboards, and an upgrade system that kept players engaged. The serverless architecture handled traffic spikes during launch without any manual intervention.