mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-17 19:13:56 +02:00
problem: example is broken
This commit is contained in:
@@ -24,7 +24,7 @@ for notice := range pool.Notices {
|
|||||||
### Listening for events
|
### Listening for events
|
||||||
|
|
||||||
```go
|
```go
|
||||||
subId, events, unsub := pool.Sub(nostr.Filters{
|
subId, events, unsubscribe := pool.Sub(nostr.Filters{
|
||||||
{
|
{
|
||||||
Authors: []string{"0ded86bf80c76847320b16f22b7451c08169434837a51ad5fe3b178af6c35f5d"},
|
Authors: []string{"0ded86bf80c76847320b16f22b7451c08169434837a51ad5fe3b178af6c35f5d"},
|
||||||
Kinds: []int{nostr.KindTextNote}, // or {1}
|
Kinds: []int{nostr.KindTextNote}, // or {1}
|
||||||
@@ -38,7 +38,7 @@ go func() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
unsub()
|
unsubscribe()
|
||||||
```
|
```
|
||||||
|
|
||||||
### Publishing an event
|
### Publishing an event
|
||||||
|
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// some nostr relay in the wild
|
// some nostr relay in the wild
|
||||||
var relayURL = "wss://nostr-relay.wlvs.space"
|
var relayURL = "wss://nostr.688.org"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// create key pair
|
// create key pair
|
||||||
@@ -26,13 +26,15 @@ func main() {
|
|||||||
pool.SecretKey = &secretKey
|
pool.SecretKey = &secretKey
|
||||||
|
|
||||||
// add a nostr relay to our pool
|
// add a nostr relay to our pool
|
||||||
err = pool.Add(relayURL, nostr.SimplePolicy{Read: true, Write: true})
|
errchan := pool.Add(relayURL, nostr.SimplePolicy{Read: true, Write: true})
|
||||||
if err != nil {
|
go func() {
|
||||||
fmt.Printf("error calling Add(): %s\n", err.Error())
|
for err := range errchan {
|
||||||
}
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
// subscribe to relays in our pool, with filtering
|
// subscribe to relays in our pool, with filtering
|
||||||
sub := pool.Sub(nostr.Filters{
|
_, events, unsubscribe := pool.Sub(nostr.Filters{
|
||||||
{
|
{
|
||||||
Authors: []string{publicKey},
|
Authors: []string{publicKey},
|
||||||
Kinds: []int{nostr.KindTextNote},
|
Kinds: []int{nostr.KindTextNote},
|
||||||
@@ -41,7 +43,7 @@ func main() {
|
|||||||
|
|
||||||
// listen for events from our subscriptions
|
// listen for events from our subscriptions
|
||||||
go func() {
|
go func() {
|
||||||
for event := range sub.UniqueEvents {
|
for event := range nostr.Unique(events) {
|
||||||
fmt.Printf("Received Event: %+v\n\n", event)
|
fmt.Printf("Received Event: %+v\n\n", event)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -69,7 +71,7 @@ func main() {
|
|||||||
// after 20 seconds, unsubscribe from our pool and terminate program
|
// after 20 seconds, unsubscribe from our pool and terminate program
|
||||||
time.Sleep(20 * time.Second)
|
time.Sleep(20 * time.Second)
|
||||||
fmt.Println("unsubscribing from nostr subscription")
|
fmt.Println("unsubscribing from nostr subscription")
|
||||||
sub.Unsub()
|
unsubscribe()
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle events from out publish events
|
// handle events from out publish events
|
||||||
|
@@ -124,7 +124,10 @@ func (r *RelayPool) Remove(url string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RelayPool) Sub(filters Filters) (string, chan EventMessage, func()) {
|
//Sub subscribes to events matching the passed filters and returns the subscription ID,
|
||||||
|
//a channel which you should pass into Unique to get unique events, and a function which
|
||||||
|
//you should call to clean up and close your subscription so that the relay doesn't block you.
|
||||||
|
func (r *RelayPool) Sub(filters Filters) (subID string, events chan EventMessage, unsubscribe func()) {
|
||||||
random := make([]byte, 7)
|
random := make([]byte, 7)
|
||||||
rand.Read(random)
|
rand.Read(random)
|
||||||
id := hex.EncodeToString(random)
|
id := hex.EncodeToString(random)
|
||||||
|
Reference in New Issue
Block a user