Build a Hello World (Rust)

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:

mkdir hello-world-ml
cd hello-world-ml

Next, create the packages inside the workspace:

cargo new subscriber --vcs none
cargo new publisher --vcs none

Finally, manually create a Cargo.toml file in the root directory for the workspace with the following content:

[workspace]
members = ["subscriber", "publisher"]

[workspace.dependencies]
nostr-sdk = "0.38.0"  
nostr = "0.38.0"
tokio = { version = "1.38.0", features = ["full"] }

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:

🎉At this point, the basic publish and subscribe functionality is complete, and the command line has output the results we expected!

Next, we will enhance this by integrating with the Solana blockchain and the Messaging Layer.Specifically, when publishing a greeting like "Hello World," the publisher will request a Solana airdrop.


Step 5: Update the Workspace Dependencies


Step 6: Update the Publisher

Replace the contents of publisher/src/main.rs with this code to publish a message with a Solana public key:


Step 7: Update the Subscriber

Edit subscriber/Cargo.toml to use workspace dependencies:

Replace the contents of subscriber/src/main.rs with this code to subscribe to events and request a Solana airdrop:


Step 8: Compile and Run

  • Run the Subscriber (in one terminal):

  • Run the Publisher (in a new terminal):

  • Expected Output:

    • Subscriber terminal:

    • Publisher terminal:

    • Subscriber terminal:


Next Steps

This example demonstrates a basic integration between Messaging Layer and Solana. You could extend this further by:

  • Deploying a Solana smart contract (program) to handle more complex interactions

  • Using Nostr events to trigger contract calls

  • Implementing payment verification or other blockchain-based logic

  • Adding error handling and retry mechanisms for airdrop requests

This simple integration opens the door to building decentralized applications that combine messaging capabilities with Solana's high-performance blockchain.

Last updated