Data Pub-Sub
DePHY DePIN Infra: Data pub-sub.
The Messaging Layer is built around a publish-subscribe (Pub-sub) model for data distribution, facilitating the exchange of device states and control commands across distributed nodes. This approach is particularly suited for scenarios requiring real-time responses, such as device management or user-initiated requests.
Event-Driven Communication: The system defines specific event types (e.g., device state changes or user requests) to propagate messages between nodes. Nodes subscribe to relevant event streams and update their local state or trigger actions based on received events, ensuring low-latency and high-throughput messaging.
Decentralized Subscriptions: By leveraging the Nostr protocol, the Messaging Layer allows multiple nodes (e.g., device controllers and servers) to subscribe to the same event source without relying on a centralized server. Subscribers can filter events by criteria like timestamps or device identifiers, processing only pertinent data.
Historical Data Synchronization: The system supports retrieving events from a specified time window, enabling newly joined nodes to quickly sync historical states. This feature is critical for fault tolerance and state recovery during system startups, such as restoring a device’s latest status after a reboot.
Flexible Event Tagging: Custom tags (e.g., session IDs and device public keys) enable precise message routing to target devices or processing logic. This flexibility supports multi-tenant environments or complex event distribution needs.
In practice, this mechanism tracks device availability or working status and broadcasts updates to all relevant parties, maintaining system-wide consistency.
Hello World!
Prerequisites
Before you begin, ensure you have the following installed:
Step 1: Create a Rust Workspace
First, create a new directory for the project and navigate into it:
Next, create the packages inside the workspace:
Finally, manually create a Cargo.toml
file in the root directory for the workspace with the following content:
This ensures that both subscriber and publisher share the same dependency versions and avoids issues from creating the workspace Cargo.toml
before the packages.
Step 2: Write the Subscriber Code
Edit subscriber/Cargo.toml
to use workspace dependencies:
Replace the contents of subscriber/src/main.rs
with this code to subscribe to "Hello World" events:
Step 3: Write the Publisher Code
Edit publisher/Cargo.toml
to use workspace dependencies:
Edit publisher/src/main.rs
:
Step 4: Compile and Run:
Run the
Subscriber
:This starts the subscriber, which will wait for events.
Run the
Publisher
(in a new terminal):Expected Output:
Subscriber
terminal:
Publisher
terminal:
Subscriber
terminal:
Last updated