Crate firebase_rtdb

Source
Expand description

§Firebase Realtime Database Client

A lightweight, async Rust client for interacting with Firebase Realtime Database. This crate provides a safe and ergonomic interface for performing CRUD operations on Firebase RTDB with JWT authentication support.

§Features

  • JWT-based authentication with automatic token renewal
  • Support for all CRUD operations (GET, PUT, POST, PATCH, DELETE)
  • Hierarchical node-based access to database paths
  • Connection timeout handling and error management
  • Serialization/deserialization support via serde
  • TLS encryption for all requests
  • Automatic token refresh before expiration

§Example

use firebase_rtdb::{FirebaseRTDB, Node, RtdbError};
use serde_json::json;

async fn example() -> Result<(), RtdbError> {
    // Initialize with JWT
    let mut db = FirebaseRTDB::new_from_jwt(
        "https://your-project.firebaseio.com",
        "your-jwt-token",
        "your-api-key"
    ).await?;

    // Access and modify data
    let mut root = db.root().await?;
    let mut users = root.child("users");
     
    // Read data
    let user_data = users.child("user1").get().await?;
     
    // Write data
    users.child("user2")
        .put(json!({
            "name": "John Doe",
            "age": 30,
            "email": "john@example.com"
        })).await?;
     
    // Update specific fields
    users.child("user2")
        .patch(json!({
            "age": 31,
            "last_login": "2023-01-01"
        })).await?;
     
    // Create new entry with generated key
    let new_user = users
        .post(json!({
            "name": "Jane Doe",
            "age": 25
        })).await?;
     
    // Delete data
    users.child("old_user").delete().await?;
     
    Ok(())
}

§Security Considerations

  • JWT tokens are automatically renewed before expiration
  • All HTTP requests use TLS encryption
  • Connection timeouts are enforced to prevent hanging
  • API keys and tokens should be kept secure and not hardcoded
  • Supports Firebase Security Rules for access control

§Error Handling

The crate uses a custom RtdbError type that wraps various error conditions:

  • Network errors (connection timeouts, DNS failures)
  • Authentication errors (invalid tokens, expired credentials)
  • Database errors (permission denied, invalid paths)
  • Serialization/deserialization errors

§Performance

  • Uses connection pooling via reqwest
  • TCP nodelay enabled for reduced latency
  • Efficient token renewal with expiration buffering
  • Reuses HTTP clients for better performance

Structs§

AuthResponsePayload
Authentication response from Firebase containing tokens and expiration.
FirebaseRTDB
Firebase Realtime Database client with JWT authentication support.
Node
Represents a node in the Firebase Realtime Database.
RtdbError
Custom error type for Firebase RTDB operations.

Constants§

DEFAULT_EXPIRE_BUFFER_SECS
Default buffer time before token expiration to trigger renewal