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:

Copy

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

Next, create the packages inside the workspace:

Copy

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:

Copy

[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:

Copy

Replace the contents of subscriber/src/main.rs with this code to subscribe to "Hello World" events:

Copy


Step 3: Write the Publisher Code

Edit publisher/Cargo.toml to use workspace dependencies:

Copy

Edit publisher/src/main.rs :

Copy


Step 4: Compile and Run:

  • Run the Subscriber:

    Copy

    This starts the subscriber, which will wait for events.

  • Run the Publisher (in a new terminal):

    Copy

  • Expected Output:

    • Subscriber terminal:

    Copy

    • Publisher terminal:

    Copy

    • Subscriber terminal:

    Copy

🎉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

Copy


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:

Copy


Step 7: Update the Subscriber

Edit subscriber/Cargo.toml to use workspace dependencies:

Copy

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

Copy


Step 8: Compile and Run

  • Run the Subscriber (in one terminal):

Copy

  • Run the Publisher (in a new terminal):

Copy

  • Expected Output:

    • Subscriber terminal:

    Copy

    • Publisher terminal:

    Copy

    • Subscriber terminal:

    Copy


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