Skip to navigation
How Nostr works
26.11.26
Unlike traditional chat apps (like WhatsApp or Discord) where Alice connects to a central company's server and the server hands the message to Bob, **Nostr works completely differently because there is no central server.** Instead, Alice and Bob use **Relays** as a neutral meeting place. Here is exactly what happens when Alice and Bob both run your Common Lisp app and want to chat: --- ### Step 1: They both connect to a Relay * **Alice's App** opens a WebSocket connection to a public relay (let's say `wss://relay.damus.io`). * **Bob's App** opens a WebSocket connection to the *same* relay (or even a different one—relays often talk to each other, but let's keep it simple: they share a relay). Neither Alice nor Bob logs into an account. They just prove who they are using their cryptographic **Public Keys**. --- ### Step 2: Bob "Subscribes" (Listens) For Bob to know when Alice is talking to him, his Lisp app sends a subscription command (`REQ`) over its WebSocket to the relay. Bob's app essentially tells the relay: > *"Hey, keep me connected and stream any new messages that mention my public key, or any messages posted in our chat room channel."* --- ### Step 3: Alice Sends a Message Alice types a message in her Lisp app: *"Hello Bob!"* Her app does the following instantly: 1. Wraps the text in a JSON event. 2. Signs the event using **Alice's Private Key** (`nsec`) so everyone knows it's authentically from her. 3. Sends that JSON event over her WebSocket to the Relay using a publish command (`EVENT`). --- ### Step 4: The Relay Broadcasts, and Bob Receives 1. The Relay receives Alice's signed JSON event. It verifies her signature (checking that it really came from Alice's public key). 2. Because Bob has an active subscription open, the Relay pushes Alice's JSON event down **Bob's WebSocket connection**. 3. **Bob's Lisp app** receives the raw JSON, parses it, extracts Alice's message, and prints it to Bob's terminal: `[Alice]: Hello Bob!` --- ### Two Ways Alice and Bob Can Chat on Nostr: Depending on how you write your Lisp app, you can build two totally different chat experiences: 1. **A Public Chat Room (Global Feed - `kind: 1`):** Alice and Bob (and anyone else in the world running a Nostr app) tune into the same channel. Anything either of them types is public for anyone on that relay to see. 2. **Encrypted Direct Messages (`kind: 4`):** If Alice wants to whisper directly to Bob, her Lisp app **encrypts** the message content using a combination of her private key and Bob's public key before sending it to the relay. * The Relay *cannot* read the message (it looks like scrambled gibberish). * When Bob's app receives it, his app uses his private key to **decrypt** it. No middleman—not even the relay server—can read their private chat! --- ### How does this sound for a project milestone? If we were to write this in Common Lisp, we would write a program that can: 1. Generate or load a keypair (`npub` / `nsec`). 2. Open a WebSocket to a relay. 3. Send a message (`EVENT`). 4. Listen for incoming messages (`REQ`). This is one of the most fascinating challenges in decentralized systems: **The "Bootstrap Problem."** Without a central company (like WhatsApp or Discord) holding a massive database of usernames and phone numbers, how do Alice and Bob actually find each other? In Nostr, there are three main ways users find each other, ranging from the most basic to the most user-friendly: --- ### 1. Out-of-Band Exchange (The Direct Way) Because public keys are just cryptographic strings (often converted to a readable format starting with `npub1...`), Alice and Bob can exchange their public keys **outside** of Nostr first. * Alice sends her `npub1...` to Bob via Signal, text message, writes it on a piece of paper, or shows a QR code at a meetup. * Bob takes Alice's `npub` and pastes it into his Common Lisp app. * Now, Bob's app knows: *"Whenever I query the relay, filter for messages originating from Alice's public key."* ### 2. Profile Metadata Events (`Kind: 0`) When Alice first sets up her Nostr app, her app broadcasts a special profile event to the relay: * **Kind:** `0` (Metadata) * **Content:** `{"name": "Alice", "about": "Building Lisp apps", "picture": "..."}` Because this event is published to the relay, Bob’s app can send a request to the relay asking: *"Give me the profile metadata for public key X."* That way, Bob's app can display "Alice" instead of a confusing 64-character hexadecimal public key. ### 3. NIP-05 (Internet Identifiers - The Email Way) Nobody wants to copy-paste 60-character cryptographic keys all day. To fix this, Nostr has a standard called **NIP-05** that lets you link your public key to a normal domain name (making it look like an email address, e.g., `alice@lispdomain.com`). Here is how it works under the hood: 1. Alice owns a website (`lispdomain.com`). 2. She puts a simple JSON file on her website at `https://lispdomain.com/.well-known/nostr.json` that maps `alice` to her public key. 3. When Bob types `alice@lispdomain.com` into his Lisp app, his app automatically fetches that JSON file over HTTPS, extracts Alice's real public key, and starts talking to her on the Nostr relay! --- ### How this translates to your Lisp App: When you build your chat app, you can start as simple as possible: 1. **Hardcode or pass a command-line argument:** When you start your Lisp script, you pass your friend's `npub` as an argument (e.g., `sbcl --script chat.lisp --connect npub1xyz...`). 2. **Subscription filter:** Your Lisp app tells the relay: *"Send me any events where the `.pubkey` matches my friend's public key."* Would you like to see what a minimal Common Lisp script looks like to connect to a WebSocket relay and listen for messages from a specific public key? Would you want to see what the raw Common Lisp code looks like to connect to a WebSocket relay and send/receive a message?
Reply
Anonymous
Information Epoch 1796210659
Make your computer work for you, instead of you working for the computer.
Home
Notebook
Contact us