Skip to content

First Application

Building Your First Nerve Application

This guide walks you through creating a complete Nerve Framework application from scratch.

Project Setup

Create a new Rust project:

cargo new my-nerve-app
cd my-nerve-app

Add dependencies to Cargo.toml:

[package]
name = "my-nerve-app"
version = "0.1.0"
edition = "2021"

[dependencies]
nerve = "0.1.0"
tokio = { version = "1.0", features = ["full"] }

Application Structure

Create a simple sensor monitoring application:

// src/main.rs
use nerve::prelude::*;
use nerve::communication::pubsub::{Publisher, Subscriber};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🚀 Starting Nerve Framework Application...");

    // Create publisher and subscriber
    let mut publisher = Publisher::new();
    let mut subscriber = Subscriber::new();

    // Subscribe to sensor topics
    subscriber.subscribe("sensor.temperature").await?;
    subscriber.subscribe("sensor.humidity").await?;

    // Start publishing sensor data
    tokio::spawn(async move {
        let mut counter = 0;
        loop {
            let temperature = 20.0 + (counter as f32 * 0.1);
            let humidity = 50.0 + (counter as f32 * 0.2);

            publisher.publish("sensor.temperature", &format!("{}°C", temperature)).await.unwrap();
            publisher.publish("sensor.humidity", &format!("{}%", humidity)).await.unwrap();

            counter += 1;
            tokio::time::sleep(Duration::from_secs(1)).await;
        }
    });

    // Listen for messages
    loop {
        if let Some(message) = subscriber.receive().await {
            println!("📡 Received: {} = {}", message.topic, message.payload);
        }
        tokio::time::sleep(Duration::from_millis(100)).await;
    }
}

Running the Application

cargo run

Expected output:

🚀 Starting Nerve Framework Application...
📡 Received: sensor.temperature = 20.0°C
📡 Received: sensor.humidity = 50.0%
📡 Received: sensor.temperature = 20.1°C
📡 Received: sensor.humidity = 50.2%
...

Key Concepts Demonstrated

  1. Publisher/Subscriber Pattern - Core communication model
  2. Async/Await - Non-blocking operations
  3. Message Routing - Topic-based message delivery
  4. Error Handling - Proper error propagation

Next Steps


This is a placeholder file. Full content coming soon.