Build a Hello World (TypeScript)
Prerequisites
Before you begin, ensure you have the following installed:
Step 1: Create a Bun Project
First, create a new project directory and initialize it with Bun:
mkdir hello-world-ml
cd hello-world-ml
bun init -yNext, install the required dependencies:
bun add nostr-tools @solana/web3.jsStep 2: Write the Subscriber Code
Create a file named subscriber.ts and add the following code. This script connects to the Nostr relay, subscribes to "Hello World" events (custom kind 1573), and logs any received messages.
// subscriber.ts
import { Relay } from "nostr-tools/relay";
const RELAY_URL = "wss://dev-relay.dephy.dev";
async function main() {
const relay = await Relay.connect(RELAY_URL);
console.log("Subscribed events on", RELAY_URL);
relay.subscribe(
[
{
kinds: [1573],
since: Math.floor(Date.now() / 1000),
"#s": ["hello_session"],
"#p": ["receiver_pubkey"],
},
],
{
onevent: async (event) => {
console.log("Received:", event.content);
},
}
);
}
main().catch(console.error);
Step 3: Write the Publisher Code
Create a file named publisher.ts and insert the following code. This script sends a simple "Hello World" event to the relay using a Nostr event with custom tags.
Step 4: Run the "Hello World" Example
Open two terminals (or tabs):
Run the
Subscriber:Run the
Publisher(in a new terminal):Expected Output:
Subscriberterminal:
Publisherterminal:
Subscriberterminal:
Next, Enhance the Functionality with Solana Airdrop
At this point, the basic publish and subscribe functionality is complete. Next, we will enhance this by integrating with the Solana blockchain. Specifically, when publishing a greeting like "Hello World," the publisher will include a Solana public key, and the subscriber will request a Solana airdrop.
Step 5: Update the Publisher
Edit publisher.ts to include a structured message and a Solana public key. Replace its contents with the following code:
Step 6: Update the Subscriber
Edit subscriber.ts so that it parses the structured message, extracts the Solana public key, and requests a Solana airdrop. Replace its contents with the following code:
Step 7: Run the Enhanced Example
Open two terminals (or tabs):
Run the
Subscriber:Run the
Publisher(in a new terminal):Expected Output:
Subscriberterminal:
Publisherterminal:
Subscriberterminal:
Next Steps
This example demonstrates a basic integration between the Messaging Layer and Solana using Bun and TypeScript. You can 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.
Happy coding!
Last updated

