
Tauri 2.0: Framework desktop app gọn nhẹ thay thế Electron cho developer
Khi nhắc đến ứng dụng desktop viết bằng web technology, Electron đã thống trị hơn một thập kỷ với các ứng dụng như VS Code, Discord, Slack, Figma. Nhưng Electron đi kèm một cái giá đắt: một ứng dụng “Hello World” cơ bản đã tốn ~150MB RAM và 50MB disk size, do gói gọn cả Chromium và Node.js trong mỗi app. Tauri ra đời như một giải pháp thay thế, sử dụng WebView của hệ điều hành (WebView2 trên Windows, WebKit trên Linux/macOS) thay vì bundle Chromium, giúp kích thước app giảm xuống chỉ còn 2-5MB và RAM tiêu thụ thấp hơn 10 lần.
Dự án Tauri được phát triển bởi cộng đồng mã nguồn mở (GitHub) và hiện đang thu hút ~4.900 Pull Requests cùng ~1000 discussions.
Kiến trúc Tauri: Rust backend + Web frontend
Khác với Electron dùng Node.js cho cả main process và renderer, Tauri tách biệt rõ ràng:
- Frontend: Bất kỳ framework nào bạn thích – React, Vue, Svelte, Solid, vanilla JS/TS. Build ra static assets (HTML/CSS/JS)
- Backend: Rust, được compile thành native binary. Xử lý file system, network, system APIs, window management
- IPC: Command/event-based communication giữa frontend và backend thông qua TypeScript bindings tự động generate từ Rust code
Lợi ích: Rust cho memory safety, concurrency, performance cực cao. Không có garbage collector, không có memory leak điển hình của Node.js trong long-running apps.
So sánh Tauri 2.0 vs Electron 30+
| Đặc điểm | Electron 30+ | Tauri 2.0 |
|---|---|---|
| Kích thước app tối thiểu | ~150MB | ~2-5MB |
| RAM cơ bản (idle) | ~120MB | ~10-20MB |
| Startup time | ~1-2s | ~200-400ms |
| Security sandbox | Cơ bản | Mặc định bật, capability-based |
| Auto-update | Cần electron-updater | Built-in (Tauri Updater) |
| Sidecar binary | Khó khăn | Hỗ trợ native (Python, Go, etc.) |
| Mobile support | Không có | Tauri Mobile (iOS/Android) – beta |
Tính năng mới trong Tauri 2.0
Phiên bản 2.0 ổn định (ra mắt tháng 5/2024) mang đến nhiều cải tiến quan trọng:
- Sidecar binaries: Chạy Python script, Go binary, hay bất kỳ executable nào bên cạnh app chính. Hữu ích cho ML inference local, database engine, CLI tool
- Plugin system: Hệ thống plugin chính thức cho filesystem, shell, dialog, notification, clipboard, store, SQL, websocket…
- Tauri Mobile: Hỗ trợ iOS (WKWebView) và Android (WebView) – dùng chung codebase Rust + Web
- Strong CSP mặc định: Content Security Policy chặt chẽ, giảm thiểu XSS risk
- Rust 1.77+ requirement: Tận dụng các tính năng Rust mới
- Vite integration: Build frontend bằng Vite, hot-reload cực nhanh
Tạo dự án Tauri đầu tiên
Cài đặt prerequisites:
# Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
# System deps (Ubuntu/Debian)
sudo apt update && sudo apt install libwebkit2gtk-4.1-dev
libayatana-appindicator3-dev librsvg2-dev
# Tạo project
npm create tauri-app@latest my-tauri-app
cd my-tauri-app
npm install
npm run tauri dev
Gọi Rust function từ JavaScript
Định nghĩa command trong Rust (src-tauri/src/lib.rs):
use tauri::command;
#[command]
fn greet(name: &str) -> String {
format!("Xin chào, {}!", name)
}
#[command]
async fn read_config() -> Result {
let content = std::fs::read_to_string("config.json")
.map_err(|e| e.to_string())?;
serde_json::from_str(&content).map_err(|e| e.to_string())
}
Đăng ký command và generate bindings:
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet, read_config])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Gọi từ frontend (TypeScript):
import { invoke } from '@tauri-apps/api/core';
async function sayHello() {
const name = document.getElementById('name')?.value;
const greeting = await invoke('greet', { name });
document.getElementById('greeting').textContent = greeting;
}
Bảo mật: Capability-based permission model
Tauri 2.0 giới thiệu capability – định nghĩa quyền truy cập tài nguyên theo nguyên tắc least privilege. Mỗi capability là một file JSON trong src-tauri/capabilities/:
{
"identifier": "main-capability",
"description": "Core capabilities for the app",
"windows": ["main"],
"permissions": [
"core:default",
"core:window:allow-close",
"core:window:allow-minimize",
"core:event:allow-listen",
"core:event:allow-emit",
"fs:allow-read-text-file",
"fs:allow-write-text-file",
"shell:allow-execute",
"dialog:allow-open",
"dialog:allow-save"
]
}
Điều này ngăn chặn việc app truy cập file system, network, hay shell command mà không được cấp phép tường minh – một bước nhảy vọt so với Electron.
Triển khai và phân phối
Tauri tích hợp sẵn Tauri Updater cho auto-update cross-platform:
- Windows: MSI installer + NSIS updater
- macOS: DMG + Sparkle framework (App Store ready)
- Linux: AppImage, .deb, .rpm, Flatpak, Snap
Cấu hình updater trong tauri.conf.json:
"updater": {
"active": true,
"endpoints": ["https://updates.myapp.com/{{target}}/{{current_version}}"],
"dialog": true,
"pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6..."
}
Ứng dụng thực tế đang dùng Tauri
- Inferno: AI code assistant desktop client
- Rocket.Chat Desktop: Migrated từ Electron, giảm 80% RAM
- Hypothesis: PDF annotation tool
- Artichoke: Ruby interpreter frontend
- Countless dev tools: Database clients, API testers, terminal apps
Khi nào chọn Tauri thay vì Electron?
Chọn Tauri khi:
- App cần nhẹ, nhanh, tiết kiệm tài nguyên (quan trọng cho laptop pin)
- Team có sẵn kinh nghiệm Rust hoặc sẵn sàng học
- Cần bảo mật cao (enterprise, fintech, healthcare)
- Muốn chạy sidecar binary (Python ML model, Go service)
- Target mobile trong tương lai
Chọn Electron khi:
- Cần compatibility tối đa với npm ecosystem phức tạp
- Team chỉ biết Node.js, không có bandwidth học Rust
- Cần Node.js native modules chưa có binding Rust
- App phụ thuộc nặng vào Chrome Extension APIs
Kết luận
Tauri 2.0 đánh dấu sự trưởng thành của framework desktop Rust + Web. Với kích thước app nhỏ, RAM thấp, bảo mật mặc định, và hệ sinh thái plugin đang phát triển nhanh, Tauri đang trở thành lựa chọn số 1 cho các dự án desktop mới. Nếu bạn là web developer muốn xây dựng desktop app mà không muốn “mang theo” 150MB Chromium, hãy thử Tauri ngay hôm nay – bạn sẽ bất ngờ về sự nhẹ nhàng và tốc độ của nó.


