Finish pass on docs
This commit is contained in:
+53
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
[](https://npmjs.com/package/@welshman/relay)
|
||||
|
||||
Core networking layer for nostr applications, handling relay connections, message management, and event delivery.
|
||||
A few utilites for storing nostr events in memory.
|
||||
|
||||
## What's Included
|
||||
|
||||
@@ -12,6 +12,58 @@ Core networking layer for nostr applications, handling relay connections, messag
|
||||
## Quick Example
|
||||
|
||||
```typescript
|
||||
import {Repository, LocalRelay} from "@welshman/relay"
|
||||
|
||||
// Create an in-memory event repository
|
||||
const repository = Repository.get()
|
||||
|
||||
// Publish events directly to the repository
|
||||
const textNote = {
|
||||
id: "event123",
|
||||
pubkey: "author-pubkey",
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
kind: 1,
|
||||
tags: [],
|
||||
content: "Hello, world!",
|
||||
sig: "signature"
|
||||
}
|
||||
|
||||
repository.publish(textNote)
|
||||
|
||||
// Query events using filters
|
||||
const recentNotes = repository.query([{kinds: [1], limit: 10}])
|
||||
console.log(`Found ${recentNotes.length} text notes`)
|
||||
|
||||
// Listen for repository updates
|
||||
repository.on("update", ({added, removed}) => {
|
||||
console.log(`Added ${added.length} events, removed ${removed.size} events`)
|
||||
})
|
||||
|
||||
// Create a local relay that adapts Nostr messages to the repository
|
||||
const relay = new LocalRelay(repository)
|
||||
|
||||
// Listen for relay messages
|
||||
relay.on("EVENT", (subId, event) => {
|
||||
console.log(`Received event ${event.id} for subscription ${subId}`)
|
||||
})
|
||||
|
||||
relay.on("OK", (eventId, success, message) => {
|
||||
console.log(`Event ${eventId} ${success ? "accepted" : "rejected"}: ${message}`)
|
||||
})
|
||||
|
||||
// Use relay protocol to publish and subscribe
|
||||
relay.send("EVENT", {
|
||||
id: "event456",
|
||||
pubkey: "another-author",
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
kind: 1,
|
||||
tags: [["t", "welshman"]],
|
||||
content: "Using LocalRelay!",
|
||||
sig: "signature"
|
||||
})
|
||||
|
||||
// Subscribe to events with hashtag
|
||||
relay.send("REQ", "tagged", {kinds: [1], "#t": ["welshman"]})
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
Reference in New Issue
Block a user