summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/basic/main.go39
1 files changed, 7 insertions, 32 deletions
diff --git a/examples/basic/main.go b/examples/basic/main.go
index 0c99dd9..1a4061a 100644
--- a/examples/basic/main.go
+++ b/examples/basic/main.go
@@ -53,11 +53,8 @@ func main() {
53 ExampleRelay() 53 ExampleRelay()
54} 54}
55 55
56// ExampleRelay demonstrates connecting to a relay (requires network).
57// This is a documentation example - run with: go test -v -run ExampleRelay
58func ExampleRelay() { 56func ExampleRelay() {
59 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 57 ctx := context.Background()
60 defer cancel()
61 58
62 // Connect to a public relay 59 // Connect to a public relay
63 relay, err := nostr.Connect(ctx, "wss://relay.damus.io") 60 relay, err := nostr.Connect(ctx, "wss://relay.damus.io")
@@ -66,38 +63,16 @@ func ExampleRelay() {
66 return 63 return
67 } 64 }
68 defer relay.Close() 65 defer relay.Close()
69
70 fmt.Println("Connected to relay!") 66 fmt.Println("Connected to relay!")
71 67
72 // Subscribe to recent text notes 68 ctx, cancel := context.WithTimeout(ctx, 25*time.Second)
73 since := time.Now().Add(-1 * time.Hour).Unix() 69 defer cancel()
74 sub, err := relay.Subscribe(ctx, "my-sub", nostr.Filter{ 70
71 filter := nostr.Filter{
75 Kinds: []int{nostr.KindTextNote}, 72 Kinds: []int{nostr.KindTextNote},
76 Since: &since,
77 Limit: 5, 73 Limit: 5,
78 })
79 if err != nil {
80 fmt.Printf("Failed to subscribe: %v\n", err)
81 os.Exit(1)
82 } 74 }
83 75 for event := range relay.Fetch(ctx, filter).Events {
84 // Listen for events in the background 76 fmt.Printf("Received event from %s...\n", event)
85 go relay.Listen(ctx)
86
87 // Collect events until EOSE
88 eventCount := 0
89 for {
90 select {
91 case event := <-sub.Events:
92 eventCount++
93 fmt.Printf("Received event from %s...\n", event)
94 case <-sub.EOSE:
95 fmt.Printf("Received %d events before EOSE\n", eventCount)
96 sub.Close(ctx)
97 return
98 case <-ctx.Done():
99 fmt.Println("Timeout")
100 return
101 }
102 } 77 }
103} 78}