Tauri 2.0: Ứng Dụng Desktop Rust Chạy Trên WebView Hệ Thống

Tauri 2.0: Ứng dụng Desktop Rust chạy trên WebView hệ thống

Tauri 2.0 là bản phát hành ổn định tháng 10/2024 của framework ứng dụng desktop đa nền tảng mã nguồn mở, cho phép xây dựng app bằng Rust (backend) và HTML/CSS/JS (frontend) chạy trên WebView gốc của hệ điều hành. Khác với Electron gói Chromium + Node.js (~100 MB), Tauri dùng WebKitGTK (Linux), WebKit (macOS/iOS), WebView2 (Windows) — kích thước binary chỉ ~3-5 MB.

Sơ đồ kiến trúc Tauri 2.0: Frontend (Web) ↔ IPC ↔ Backend (Rust) ↔ System APIs

Tại sao Tauri 2.0 quan trọng?

Electron thống trị desktop web tech từ 2013 (VS Code, Discord, Slack, Figma), nhưng chi phí là:

  • Dung lượng cài đặt lớn: Mỗi app mang theo bản Chromium riêng (~150 MB RAM baseline).
  • Bề mặt tấn công rộng: Engine JS + Node.js + Chromium C++ cùng update.
  • Hiệu năng: IPC qua JSON string serialization chậm; multi-process model tốn tài nguyên.
  • Phân phối: Cần code signing, notarization, auto-updater riêng cho từng OS.

Tauri 2.0 giải quyết bằng: zero-bundle runtime (dùng WebView OS), Rust type-safe IPC, single-process architecture (WebView embed trong process Rust), và hệ thống updater tích hợp dùng GitHub Releases + cryptographic verification.

Kiến trúc cốt lõi: Tao + Wry + IPC

Ba crate nền tảng:

  • tao (Tauri Application Object): Tạo cửa sổ, menu, tray, sự kiện vòng đời đa nền tảng (Linux/Windows/macOS/iOS/Android).
  • wry (Web Rendering in Rust): Wrapper WebView2/WebKitGTK/WebKit, xử lý navigation, script injection, custom protocol.
  • tauri (crate chính): Xây trên tao+wry, cung cấp API cao cấp (dialog, filesystem, shell, clipboard, notifications, updater).

IPC trong Tauri 2.0 dùng Invoke model: Frontend gọi invoke('command_name', {args}) → Rust handler nhận #[tauri::command] function → trả về Result tự serialize JSON. Không còn stringify/parse thủ công; type-safe end-to-end nhờ serde.

Tính năng mới quan trọng trong Tauri 2.0

  • Mobile support (iOS/Android): Cùng codebase desktop, build ra .ipa/.aab. WebView iOS dùng WKWebView, Android dùng Chrome WebView (System WebView từ Android 10+).
  • Isolation pattern: Mỗi webview context có capability riêng, ngăn chặn untrusted script truy cập backend API nhạy cảm (filesystem, shell).
  • Sidecar: Chạy binary phụ (Go, Python, C++) cùng process chính — hữu ích cho plugin, legacy code, hoặc ML inference local.
  • Strong CSP default: Content Security Policy chặt chẽ, chỉ cho phép script/hash explicit, chống XSS.
  • Updater v2: Delta updates (binary diff), background download, rollback tự động, hỗ trợ App Store / Play Store / Microsoft Store.
  • Plugin system: Hệ sinh thái plugin mở rộng (tauri-plugin-sql, tauri-plugin-fs, tauri-plugin-oauth2, v.v.).
So sánh kích thước bundle: Electron app (120 MB) vs Tauri app (4 MB)

So sánh Tauri 2.0 vs Electron vs Wails vs Neutralinojs

Tiêu chí Tauri 2.0 Electron 30+ Wails v2 Neutralinojs
Runtime bundle ~3-5 MB (OS WebView) ~120 MB (Chromium) ~8-12 MB (WebView2/WebKit) ~5-8 MB (OS WebView)
Backend language Rust Node.js Go Node.js / Go / Python
IPC model Invoke (type-safe) ipcMain/Renderer (string) Bind (Go struct ↔ JS) WebSocket / HTTP
Mobile support iOS + Android (stable) Không (đang thử nghiệm) iOS + Android (beta) iOS + Android (experimental)
Binary size (Hello World) ~4 MB ~120 MB ~8 MB ~6 MB
RAM baseline ~15-20 MB ~100-150 MB ~25-35 MB ~10-15 MB
Security model Capability + CSP Context isolation + sandbox Go sandbox + CSP CSP + permission system
App Store ready Có (notarization tự động) Có (electron-builder) Có (wails build) Hạn chế

Khi nào nên chọn Tauri 2.0?

Phù hợp:

  • App desktop cần hiệu năng cao, dung lượng nhỏ (utility, dev tool, editor, dashboard).
  • Team có kinh nghiệm Rust hoặc sẵn sàng học; muốn type-safety end-to-end.
  • Cần phân phối qua App Store / Microsoft Store / Snap / Flatpak.
  • Yêu cầu mobile support (iOS/Android) từ cùng codebase.
  • Bảo mật ưu tiên: isolation pattern, CSP strict, Rust memory safety.

Chưa phù hợp:

  • Team chỉ biết JS/TS, không muốn học Rust (curve dốc ~2-3 tháng).
  • Cần Node.js native modules (native-addons) — Tauri không chạy Node.js runtime.
  • App phụ thuộc nặng Chromium APIs (extension API, DevTools protocol, Chrome-specific CSS).
  • Deadline gấp, prototype nhanh — Electron + Vite + React setup nhanh hơn.

Migration từ Tauri 1.x sang 2.0

Breaking changes chính:

  1. Config format: tauri.conf.json → tauri.conf.json5 (hỗ trợ comments, trailing comma).
  2. API namespace: window.__TAURI__.tauri → window.__TAURI__.core + plugin APIs riêng biệt.
  3. Updater: Cần migrate manifest format mới, signature algorithm Ed25519 thay vì RSA.
  4. Mobile: Thêm tauri::mobile::init() trong main.rs, config android/ios trong tauri.conf.json5.
  5. Permissions: Hệ thống capability mới, cần khai báo permissions cho từng plugin.

Công cụ migration tự động: cargo install tauri-migrator && tauri-migrator — xử lý ~80% thay đổi cơ cấu.

Minh họa quy trình migration Tauri 1.x sang 2.0 với tauri-migrator

Hệ sinh thái plugin và công cụ

Top plugin chính thức (maintained by Tauri team):

  • tauri-plugin-sql: SQLite / libSQL embedded, type-safe queries qua sea-orm hoặc sqlx.
  • tauri-plugin-fs: Filesystem scope-based access (read/write/copy/mkdir) với permission granular.
  • tauri-plugin-shell: Spawn process, pipe stdin/stdout, open URL/external.
  • tauri-plugin-oauth2: OAuth2 PKCE flow cho Google, GitHub, Microsoft, Apple.
  • tauri-plugin-notification: System notification cross-platform.
  • tauri-plugin-clipboard-manager: Read/write clipboard text, image, files.
  • tauri-plugin-autostart: Launch at login (Linux systemd, macOS LaunchAgent, Windows Registry).

Công cụ phát triển: tauri dev (hot reload frontend + backend), tauri build (cross-compile via GitHub Actions matrix), tauri icon (generate app icons từ SVG source).

Case study thực tế

  • RustDesk (remote desktop open-source): Migrate từ Flutter → Tauri 2.0, binary giảm từ 45 MB → 8 MB, RAM idle 120 MB → 18 MB.
  • Copilot for Xcode (Apple internal): Tauri 2.0 iOS app cho internal tooling, sharing 90% code với macOS version.
  • WarTerminal (terminal emulator Rust): Sidecar chạy PTY process, WebView chỉ render UI, latency typing <2ms.
  • Tabby (SSH/Serial client): Plugin architecture, cross-platform từ single codebase.

Tương lai: Tauri 3.0 và webview standardization

Đường dẫn phát triển:

  • WebView standardization: W3C WebView Community Group đang chuẩn hóa API (WebView API Level 1). Tauri đóng vai trò contributor chính.
  • WebGPU support: WebView2 (Windows 11 24H2+) và WebKit (Safari 18+) hỗ trợ WebGPU; Tauri sẽ expose qua wry.
  • WASM Components: Backend Rust compile sang WASM Component Model, chạy trong Wasmtime nhúng — cho phép plugin hot-swap mà không restart app.
  • AI local inference: Sidecar + llama.cpp / ONNX Runtime, chạy LLM 7B local trên CPU/NPU, IPC type-safe qua Tauri commands.

Tóm lại: Tauri 2.0 không chỉ “Electron nhẹ hơn” — là sự chuyển dịch nguyên lý: từ “bundle runtime” sang “dùng platform capabilities”, từ “dynamic language IPC” sang “type-safe Rust contracts”. Nếu bạn xây dựng app desktop năm 2025+, Tauri xứng đáng là lựa chọn đầu tiên để đánh giá.

Nguồn: Wikipedia – Tauri (software framework), Tauri Official Website, Tauri GitHub Repository

Tôi là một lập trình viên IOS. Code chính là IOS nhưng thỉnnh thoảng vẫn đá sang Android hoặc web. Mặc dù không quá thông thạo nhưng tôi sẽ chia sẻ những kiến thức mà mình đã tìm hiểu, áp dụng qua.

Bài viết liên quan

Feather Robotics: Nền tảng robot mô-đun giá 30k USD cho developer

Feather Robotics, khởi nghiệp humanoid do cựu kỹ sư Tesla và founder trước của 1X thành lập, đang định vị mình là “Android of robotics” — cung cấp nền tảng…

Xem thêm

Swift 6 Async/Await & Structured Concurrency: Hướng Dẫn Từ Cơ Bản Đến Production

Swift 6 Async/Await & Structured Concurrency: Hướng Dẫn Từ Cơ Bản Đến Production-Ready Swift 6 (ra mắt WWDC 2024) và Swift 6.2 (WWDC 2025) đưa Structured Concurrency trở thành default…

Xem thêm

Axum: Web Framework Rust Async Hiệu Suất Cao Cho Backend

Axum là một web framework Rust hiện đại, được xây dựng trên nền tảng Tokio và Tower, tập trung vào ergonomics (khả năng sử dụng dễ dàng) và modularity (tính…

Xem thêm
0 0 đánh giá
Article Rating
Theo dõi
Thông báo của
guest
0 Comments
Cũ nhất
Mới nhất Được bỏ phiếu nhiều nhất