Step-by-Step Guide to Send a Nostr Message
This guide will walk you through sending a Nostr message in different environments using JavaScript. We’ll cover how to generate keys, create and sign an event, connect to relays, and publish the message to the Nostr network using WebSockets.
Step 1: Import the Nostr Tools Library
This tutorial requires the nostr-tools
library to generate keys, create events, and interact with the Nostr network. We will use version 2.10.4
of the library. Here are the steps to import the library in different environments:
To import the Nostr Tools library in a browser environment, we need to first create an HTML file. Below is an HTML layout with a form to send a Nostr message with some basic styling.
Now, we can import the Nostr Tools library to generate keys and create events. We’ll use the esm.sh
CDN to import it directly in the browser:
Below is a guide to import the nostr-tools
library in other runtimes like Node.js/Bun (or any npm-compatible runtime), Deno (or any runtime that supports loading modules directly from URLs), or the browser’s devtools console using dynamic import.
If you are using Node.js/Bun or a similar runtime that supports loading modules from npm, you need to first install the nostr-tools
package:
Once you have installed the package, we need to create a .js
file (.mjs
for Node.js
) and import the library like this:
If you are using a deno or a similar runtime that supports loading modules directly from URLs, you can import the library like this without any installation:
If you want to run this code in the browser’s devtools console, since the browser doesn’t support loading ES modules outside of a <script type="module">
tag, we need to use dynamic import to load the module.
Step 2: Define the Required Variables
Let’s define the required variables that we will use in the following steps.
In the web, we need to get the required elements and listen to the form submission event. We’ll also define the message content & list of relays where we want to publish the message.
For any other non-browser runtimes, we are going to define the message content and list of relays where we want to publish the message.
Step 3: Key Generation
First, we generate a new private key, which can be in different formats: Uint8Array (byte array for raw data processing), hex (human-readable), or Bech32 (error-resistant, human-readable format). Here is the key generation process:
Step 4: Create and Sign an Event
Next, we create a Kind 1 event (a note) and sign it with the private key:
Step 5: Connect to Relays and Send the Event
To broadcast the event to the Nostr network, we connect to relay servers via WebSockets. You can find a list of known relays at https://nostr.watch/. The relays handle the event in JSON format. Here’s how we send the event:
Additional Relay Information
-
Why use multiple relays? Using multiple relays increases the likelihood that your content will be distributed and stay accessible, even if some relays go offline. Relays operate independently, meaning they don’t automatically sync data with each other. Connecting to several relays ensures broader distribution of your messages and improves redundancy.
-
Free vs. paid relays: There are both free and paid relays in the Nostr ecosystem. Free relays are great for testing and learning, while paid relays typically offer better performance and reliability because they can afford to maintain higher-quality infrastructure.
-
Using relays based on follower lists: Some Nostr clients dynamically select relays based on the user’s follower list. This ensures that you’re connected to the same relays as the people you follow, improving message delivery and visibility.
Summary of the Process
To recap, here’s the process for sending a Nostr message:
-
Key Generation: We generated a private/public key pair using Nostr tools.
-
Creating and Signing an Event: We created a Kind 1 event (a note) and signed it with the private key.
-
Connecting to Relays: We connected to multiple relay servers using WebSockets to ensure the message was distributed across the Nostr network.
-
Displaying Published Relays: We displayed the relays where the message was successfully published.
By following this guide, you’ve successfully sent a Nostr message using HTML, JavaScript, and WebSockets. This provides a foundation for understanding how to interact with the Nostr protocol and work with its key features.