1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2025 Rust Nostr Developers
// Distributed under the MIT software license
//! Client
use std::borrow::Cow;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::{Arc, Weak};
use std::time::Duration;
use futures::{Stream, StreamExt};
use nostr::prelude::*;
use nostr_database::prelude::*;
use tokio::sync::oneshot;
mod api;
mod builder;
mod gossip;
mod notification;
mod url;
pub use self::api::*;
pub use self::builder::*;
use self::gossip::*;
pub use self::notification::*;
pub use self::url::*;
use crate::error::Error;
use crate::monitor::Monitor;
use crate::pool::{RelayPool, RelayPoolBuilder};
#[cfg(not(target_arch = "wasm32"))]
use crate::proxy::Proxy;
use crate::relay::{
Relay, RelayCapabilities, RelayLimits, RelayOptions, SleepWhenIdle, SyncOptions,
};
use crate::stream::NotificationStream;
#[derive(Debug)]
struct ClientConfig {
#[cfg(not(target_arch = "wasm32"))]
proxy: Option<Proxy>,
gossip_config: GossipConfig,
connect_timeout: Duration,
relay_limits: RelayLimits,
max_avg_latency: Option<Duration>,
sleep_when_idle: SleepWhenIdle,
verify_subscriptions: bool,
ban_relay_on_mismatch: bool,
}
#[derive(Debug)]
struct InnerClient {
pool: RelayPool,
gossip: Option<Gossip>,
config: ClientConfig,
}
#[derive(Debug)]
struct WeakClient(Weak<InnerClient>);
impl WeakClient {
/// Upgrade to [`Client`].
///
/// Returns `None` if the all the instances of the [`Client`] has been dropped.
#[inline]
fn upgrade(&self) -> Option<Client> {
Some(Client(self.0.upgrade()?))
}
}
/// Nostr client
#[derive(Debug, Clone)]
pub struct Client(Arc<InnerClient>);
impl Default for Client {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl Client {
/// Construct a new default client
///
/// Use the [`Client::builder`] to configure the client (i.e., set a signer).
#[inline]
pub fn new() -> Self {
Self::builder().build()
}
/// Construct client
///
/// # Example
/// ```rust,no_run
/// use std::time::Duration;
///
/// use nostr_sdk::prelude::*;
///
/// let signer = Keys::generate();
/// let authenticator = SignerAuthenticator::new(signer);
/// let client: Client = Client::builder().authenticator(authenticator).build();
/// ```
#[inline]
pub fn builder() -> ClientBuilder {
ClientBuilder::default()
}
fn from_builder(builder: ClientBuilder) -> Self {
// Construct relay pool builder
let pool_builder: RelayPoolBuilder = RelayPoolBuilder {
websocket_transport: builder.websocket_transport,
gossip: builder.gossip.clone(),
admit_policy: builder.admit_policy,
authenticator: builder.authenticator,
monitor: builder.monitor,
database: builder.database,
max_relays: builder.max_relays,
notification_channel_size: builder.notification_channel_size,
};
// Construct the inner client
let inner = InnerClient {
pool: pool_builder.build(),
gossip: builder.gossip.map(Gossip::new),
config: ClientConfig {
#[cfg(not(target_arch = "wasm32"))]
proxy: builder.proxy,
gossip_config: builder.gossip_config,
connect_timeout: builder.connect_timeout,
relay_limits: builder.relay_limits,
max_avg_latency: builder.max_avg_latency,
sleep_when_idle: builder.sleep_when_idle,
verify_subscriptions: builder.verify_subscriptions,
ban_relay_on_mismatch: builder.ban_relay_on_mismatch,
},
};
// Construct the client
let client = Self(Arc::new(inner));
client.spawn_gossip_background_refresher();
client
}
#[inline]
fn pool(&self) -> &RelayPool {
&self.0.pool
}
#[inline]
fn config(&self) -> &ClientConfig {
&self.0.config
}
#[inline]
fn gossip(&self) -> Option<&Gossip> {
self.0.gossip.as_ref()
}
#[inline]
fn weak_clone(&self) -> WeakClient {
WeakClient(Arc::downgrade(&self.0))
}
/// Get database
#[inline]
pub fn database(&self) -> &Arc<dyn NostrDatabase> {
self.pool().database()
}
/// Get the relay monitor
#[inline]
pub fn monitor(&self) -> Option<&Monitor> {
self.pool().monitor()
}
/// Check if the client is shutting down
#[inline]
pub fn is_shutdown(&self) -> bool {
self.pool().is_shutdown()
}
/// Explicitly shutdown the client
///
/// This method will shut down the client and all its relays.
#[inline]
pub async fn shutdown(&self) {
self.pool().shutdown().await
}
/// Get a new notification stream
///
/// The stream terminates when the client shutdowns.
///
/// <div class="warning">When you call this method, you subscribe to the notifications channel from that precise moment. Anything received by relay/s before that moment is not included in the channel!</div>
#[inline]
pub fn notifications(&self) -> Pin<Box<dyn Stream<Item = ClientNotification> + Send>> {
if self.is_shutdown() {
return Box::pin(futures::stream::empty());
}
// Subscribe to notifications
let rx = self.pool().notifications();
// Create a oneshot channel
let (tx, rx_done) = oneshot::channel();
let mut tx: Option<oneshot::Sender<()>> = Some(tx);
Box::pin(
NotificationStream::new(rx)
.inspect(move |notification| {
if let ClientNotification::Shutdown = ¬ification {
// Take the sender and send the oneshot notification
if let Some(tx) = tx.take() {
let _ = tx.send(());
}
}
})
.take_until(rx_done),
)
}
/// Get relays from the relay pool.
///
/// # Configuration
///
/// By default:
///
/// - Only relays with [`RelayCapabilities::READ`] or [`RelayCapabilities::WRITE`]
/// are returned.
///
/// To customize this behavior, the returned [`GetRelays`] can be
/// configured before awaiting it:
///
/// - [`GetRelays::all`]: return all relays in the pool, regardless of capabilities
/// - [`GetRelays::with_capabilities`]: return relays matching specific
/// [`RelayCapabilities`]
#[inline]
pub fn relays(&self) -> GetRelays<'_> {
GetRelays::new(self)
}
/// Get a previously added [`Relay`] by URL.
///
/// It returns the relay **only if it has already been added**
/// to the client via [`Client::add_relay`].
///
/// - Returns `Ok(None)` if the relay is not found in the pool.
/// - Returns `Err(_)` if the provided URL cannot be parsed as a relay URL.
///
/// # Example
///
/// ```
/// # use nostr_sdk::prelude::*;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::default();
/// // Not added yet
/// let relay = client.relay("wss://relay.example.com").await?;
/// assert!(relay.is_none());
///
/// // Add it
/// client.add_relay("wss://relay.example.com").await?;
///
/// // Now it can be retrieved
/// let relay = client.relay("wss://relay.example.com").await?;
/// assert!(relay.is_some());
/// # Ok(())
/// # }
/// ```
pub async fn relay<'a, U>(&self, url: U) -> Result<Option<Relay>, Error>
where
U: Into<RelayUrlArg<'a>>,
{
let url: RelayUrlArg<'a> = url.into();
let url: Cow<RelayUrl> = url.try_as_relay_url()?;
Ok(self.pool().relay(&url).await)
}
fn compose_relay_opts<'a>(&self, _url: &'a RelayUrlArg<'a>) -> RelayOptions {
let mut opts: RelayOptions = RelayOptions::new();
// Set proxy
#[cfg(not(target_arch = "wasm32"))]
if let Some(proxy) = &self.config().proxy {
opts = opts.proxy(proxy.clone());
}
// Set sleep when idle
opts = opts.sleep_when_idle(self.config().sleep_when_idle);
// Set limits
opts.connect_timeout(self.config().connect_timeout)
.limits(self.config().relay_limits.clone())
.max_avg_latency(self.config().max_avg_latency)
.verify_subscriptions(self.config().verify_subscriptions)
.ban_relay_on_mismatch(self.config().ban_relay_on_mismatch)
}
/// Add relay
///
/// By default, relays added with this method will have both [`RelayCapabilities::READ`] and [`RelayCapabilities::WRITE`] capabilities enabled.
///
/// Returns `true` only when a new relay is actually inserted into the pool.
/// If the relay already exists, its capabilities are updated and `false` is returned.
/// If the relay is rejected by the admission policy, this method also returns `false`.
///
/// To add a relay with specific capabilities, use [`AddRelay::capabilities`].
///
/// Connection is **NOT** automatically started with relay!
#[inline]
pub fn add_relay<'client, 'url, U>(&'client self, url: U) -> AddRelay<'client, 'url>
where
U: Into<RelayUrlArg<'url>>,
{
let url: RelayUrlArg<'url> = url.into();
let opts: RelayOptions = self.compose_relay_opts(&url);
AddRelay::new(self, url).opts(opts)
}
/// Add discovery relay
///
/// If relay already exists, this method automatically add the [`RelayCapabilities::DISCOVERY`] flag to it and return `false`.
///
/// <https://github.com/nostr-protocol/nips/blob/master/65.md>
#[deprecated(
since = "0.45.0",
note = "Use `Client::add_relay(url).capabilities(RelayCapabilities::DISCOVERY)` instead."
)]
pub async fn add_discovery_relay<'u, U>(&self, url: U) -> Result<bool, Error>
where
U: Into<RelayUrlArg<'u>>,
{
self.add_relay(url)
.capabilities(RelayCapabilities::DISCOVERY)
.await
}
/// Add read relay
#[deprecated(
since = "0.45.0",
note = "Use `Client::add_relay(url).capabilities(RelayCapabilities::READ)` instead."
)]
pub async fn add_read_relay<'u, U>(&self, url: U) -> Result<bool, Error>
where
U: Into<RelayUrlArg<'u>>,
{
self.add_relay(url)
.capabilities(RelayCapabilities::READ)
.await
}
/// Add write relay
#[deprecated(
since = "0.45.0",
note = "Use `Client::add_relay(url).capabilities(RelayCapabilities::WRITE)` instead."
)]
pub async fn add_write_relay<'u, U>(&self, url: U) -> Result<bool, Error>
where
U: Into<RelayUrlArg<'u>>,
{
self.add_relay(url)
.capabilities(RelayCapabilities::WRITE)
.await
}
/// Add gossip relay
#[deprecated(
since = "0.45.0",
note = "Use `Client::add_relay(url).capabilities(RelayCapabilities::GOSSIP)` instead."
)]
pub async fn add_gossip_relay<'u, U>(&self, url: U) -> Result<bool, Error>
where
U: Into<RelayUrlArg<'u>>,
{
self.add_relay(url)
.capabilities(RelayCapabilities::GOSSIP)
.await
}
/// Remove and disconnect relay
///
/// If the relay has [`RelayCapabilities::GOSSIP`], it will not be removed from the pool and its
/// capabilities will be updated (remove [`RelayCapabilities::READ`],
/// [`RelayCapabilities::WRITE`] and [`RelayCapabilities::DISCOVERY`] capabilities).
#[inline]
pub fn remove_relay<'p, 'u, U>(&'p self, url: U) -> RemoveRelay<'p, 'u>
where
U: Into<RelayUrlArg<'u>>,
{
RemoveRelay::new(self, url.into())
}
/// Force remove and disconnect relay
///
/// Note: this method will remove the relay, also if it's in use for the gossip model or other service!
#[deprecated(since = "0.45.0", note = "use `remove_relay(url).force()` instead")]
pub async fn force_remove_relay<'u, U>(&self, url: U) -> Result<(), Error>
where
U: Into<RelayUrlArg<'u>>,
{
self.remove_relay(url).force().await
}
/// Disconnect and remove all relays
///
/// Some relays (i.e., the gossip ones) will not be disconnected and removed unless you
/// use [`RemoveAllRelays::force()`].
#[inline]
pub fn remove_all_relays(&self) -> RemoveAllRelays<'_> {
RemoveAllRelays::new(self)
}
/// Disconnect and force remove all relays
#[deprecated(
since = "0.45.0",
note = "use `remove_all_relays(url).force()` instead"
)]
pub async fn force_remove_all_relays(&self) {
let _ = self.remove_all_relays().force().await;
}
/// Connect to a previously added relay
#[inline]
pub async fn connect_relay<'a, U>(&self, url: U) -> Result<(), Error>
where
U: Into<RelayUrlArg<'a>>,
{
let url: RelayUrlArg<'a> = url.into();
let url: Cow<RelayUrl> = url.try_as_relay_url()?;
self.pool().connect_relay(&url).await
}
/// Try to connect to a previously added relay
#[inline]
pub async fn try_connect_relay<'a, U>(&self, url: U, timeout: Duration) -> Result<(), Error>
where
U: Into<RelayUrlArg<'a>>,
{
let url: RelayUrlArg<'a> = url.into();
let url: Cow<RelayUrl> = url.try_as_relay_url()?;
self.pool().try_connect_relay(&url, timeout).await
}
/// Disconnect relay
#[inline]
pub async fn disconnect_relay<'a, U>(&self, url: U) -> Result<(), Error>
where
U: Into<RelayUrlArg<'a>>,
{
let url: RelayUrlArg<'a> = url.into();
let url: Cow<RelayUrl> = url.try_as_relay_url()?;
self.pool().disconnect_relay(&url).await
}
/// Connect to relays
///
/// Attempts to initiate a connection with relays.
///
/// At most **one connection per relay** is allowed at any time.
/// If a relay is already connected or currently attempting to connect,
/// this method does nothing for that relay.
///
/// If a relay is disconnected, sleeping, or otherwise inactive, a
/// background task is spawned to initiate a connection.
///
/// For further details, see the documentation of [`Relay::connect`].
///
/// # Configuration
///
/// By default:
///
/// - Doesn't wait that relays connect
///
/// To customize this behavior, the returned [`Connect`] can be
/// configured before awaiting it:
///
/// - [`Connect::and_wait`]: wait for relays connections at most for the specified `timeout`
#[inline]
pub fn connect(&self) -> Connect<'_> {
Connect::new(self)
}
/// Waits for relays connections
///
/// Wait for relays connections at most for the specified `timeout`.
/// The code continues when the relays are connected or the `timeout` is reached.
#[deprecated(
since = "0.45.0",
note = "use `client.connect().and_wait(timeout).await` instead"
)]
pub async fn wait_for_connection(&self, timeout: Duration) {
self.connect().and_wait(timeout).await;
}
/// Try to establish a connection with relays.
///
/// # Overview
///
/// Attempts to initiate a connection with relays.
///
/// At most **one connection per relay** is allowed at any time.
/// If a relay is already connected or currently attempting to connect,
/// this method does nothing for that relay.
///
/// If the initial connection attempt succeeds, a background task is spawned
/// to maintain the connection and handle future reconnections.
/// If the initial attempt fails, no background task is spawned and no
/// automatic retries are scheduled.
///
/// Use [`Client::connect`] if you want to always spawn a background
/// connection task, regardless of whether the initial attempt succeeds.
///
/// For further details, see the documentation of [`Relay::try_connect`].
///
/// # Configuration
///
/// By default:
///
/// - Connection timeout is set to 60 secs
///
/// To customize this behavior, the returned [`TryConnect`] can be
/// configured before awaiting it:
///
/// - [`TryConnect::timeout`]: set a maximum timeout
#[inline]
pub fn try_connect(&self) -> TryConnect<'_> {
TryConnect::new(self)
}
/// Disconnect from all relays
#[inline]
pub async fn disconnect(&self) {
self.pool().disconnect().await
}
/// Get subscriptions
#[inline]
pub async fn subscriptions(&self) -> HashMap<SubscriptionId, HashMap<RelayUrl, Vec<Filter>>> {
self.pool().subscriptions().await
}
/// Get subscription
#[inline]
pub async fn subscription(&self, id: &SubscriptionId) -> HashMap<RelayUrl, Vec<Filter>> {
self.pool().subscription(id).await
}
/// Subscribe to events from relays.
///
/// # Overview
///
/// Creates a long-lived event subscription.
///
/// The subscription remains active until it is explicitly closed or until
/// auto-close conditions are met.
///
/// For short-lived, request-style event streams, use [`Client::stream_events`] or [`Client::fetch_events`].
///
/// # Configuration
///
/// By default:
///
/// - a random subscription ID is generated
/// - no auto-close condition are set
///
/// The returned [`Subscribe`] builder can be configured before execution:
///
/// - [`Subscribe::with_id`]: set an explicit subscription ID
/// - [`Subscribe::close_on`]: configure automatic closing conditions
///
/// # Target Resolution
///
/// The request target determines which relays are queried:
///
/// - [`ReqTarget::auto`]: Sends the subscription to all relays with
/// [`RelayCapabilities::READ`]. If gossip is enabled
/// ([`ClientBuilder::gossip`]), NIP-65 relays are also included.
/// - [`ReqTarget::single`] / [`ReqTarget::manual`]: Sends the subscription only to
/// the explicitly specified relays.
///
/// # Event Semantics
///
/// - Event signatures are **validated**.
/// - Events are **verified against the requested filters** if
/// [`ClientBuilder::verify_subscriptions`] is enabled.
/// - Event replacements, deletions, and other stateful event semantics
/// depend on the [`NostrDatabase`] implementation in use.
///
/// # Lifetime
///
/// The subscription terminates when:
///
/// - It is explicitly closed,
/// - Auto-close conditions are met (if configured),
/// - Or the relay closes it remotely.
///
/// # Errors
///
/// Returns an error if:
///
/// - The resolved target contains no relays,
/// - A specified relay does not exist in the pool,
/// - Target resolution fails.
///
/// # Examples
///
/// ## Automatic target resolution
///
/// ```rust,no_run
/// # use nostr_sdk::prelude::*;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::default();
/// # client.add_relay("wss://relay1.example.com").await?;
/// # client.add_relay("wss://relay2.example.com").await?;
///
/// // Subscribe with a single filter to all relays
/// let filter = Filter::new().kind(Kind::TextNote).since(Timestamp::now());
///
/// // Subscribe to notifications
/// let mut notifications = client.notifications();
///
/// // Send REQ
/// let output = client.subscribe(filter).await?;
/// println!("Subscription ID: {}", output.id());
/// println!("Successful relays: {:?}", output.success);
/// println!("Failed relays: {:?}", output.failed);
///
/// // Handle notifications
/// while let Some(notification) = notifications.next().await {
/// if let ClientNotification::Event { relay_url, subscription_id, event } = notification {
/// if output.id() == &subscription_id {
/// println!("Received an event from '{relay_url}' relay for the subscription '{subscription_id}': {}", event.as_json());
/// }
/// }
/// }
/// # Ok(())
/// # }
/// ```
///
/// ## Target specific relays
///
/// ```rust,no_run
/// # use std::collections::HashMap;
/// # use nostr_sdk::prelude::*;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::default();
/// # client.add_relay("wss://relay1.example.com").await?;
/// # client.add_relay("wss://relay2.example.com").await?;
/// // Subscribe to notifications
/// let mut notifications = client.notifications();
///
/// // Subscribe with different filters per relay
/// let mut targets = HashMap::new();
/// targets.insert(
/// "wss://relay1.example.com",
/// vec![Filter::new().kind(Kind::TextNote).limit(10)],
/// );
/// targets.insert(
/// "wss://relay2.example.com",
/// vec![Filter::new().kind(Kind::Metadata).limit(5)],
/// );
///
/// // Send REQ
/// let output = client.subscribe(targets).await?;
/// println!("Subscription ID: {}", output.id());
/// println!("Successful relays: {:?}", output.success);
/// println!("Failed relays: {:?}", output.failed);
///
/// // Handle notifications
/// while let Some(notification) = notifications.next().await {
/// if let ClientNotification::Event { relay_url, subscription_id, event } = notification {
/// if output.id() == &subscription_id {
/// println!("Received an event from '{relay_url}' relay for the subscription '{subscription_id}': {}", event.as_json());
/// }
/// }
/// }
/// # Ok(())
/// # }
/// ```
///
/// ## With custom ID and auto-close
///
/// ```rust,no_run
/// # use std::time::Duration;
/// # use nostr_sdk::prelude::*;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::default();
/// # client.add_relay("wss://relay1.example.com").await?;
/// # client.add_relay("wss://relay2.example.com").await?;
///
/// // Subscribe to notifications
/// let mut notifications = client.notifications();
///
/// let filter = Filter::new().kind(Kind::TextNote).limit(10);
/// let custom_id = SubscriptionId::generate();
///
/// let auto_close =
/// SubscribeAutoCloseOptions::default()
/// .exit_policy(ReqExitPolicy::WaitForEventsAfterEOSE(10))
/// .idle_timeout(Some(Duration::from_secs(60)));
///
/// // Send REQ
/// let output = client
/// .subscribe(filter)
/// .with_id(custom_id)
/// .close_on(auto_close)
/// .await?;
///
/// println!("Successful relays: {:?}", output.success);
/// println!("Failed relays: {:?}", output.failed);
///
/// // Handle notifications
/// while let Some(notification) = notifications.next().await {
/// if let ClientNotification::Event { relay_url, subscription_id, event } = notification {
/// if output.id() == &subscription_id {
/// println!("Received an event from '{relay_url}' relay for the subscription '{subscription_id}': {}", event.as_json());
/// }
/// }
/// }
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn subscribe<'client, 'url, F>(&'client self, target: F) -> Subscribe<'client, 'url>
where
F: Into<ReqTarget<'url>>,
{
Subscribe::new(self, target.into())
}
/// Unsubscribe from a REQ
#[inline]
pub fn unsubscribe<'id>(&self, id: &'id SubscriptionId) -> Unsubscribe<'_, 'id> {
Unsubscribe::new(self, id)
}
/// Unsubscribe from all REQs
#[inline]
pub fn unsubscribe_all(&self) -> UnsubscribeAll<'_> {
UnsubscribeAll::new(self)
}
/// Stream events from relays.
///
/// # Overview
///
/// Creates a short-lived event subscription and returns a stream of events.
///
/// For long-lived subscriptions, use [`Client::subscribe`].
///
/// # Configuration
///
/// By default:
///
/// - No timeout is set
/// - Exit policy is [`ReqExitPolicy::ExitOnEOSE`](crate::relay::ReqExitPolicy::ExitOnEOSE)
///
/// To customize this behavior, the returned [`StreamEvents`] can be
/// configured before awaiting it:
///
/// - [`StreamEvents::with_id`]: use a specific subscription ID
/// - [`StreamEvents::timeout`]: set a maximum duration for the stream
/// - [`StreamEvents::policy`]: control when the stream terminates
///
/// # Target Resolution
///
/// The request target determines which relays are queried:
///
/// - [`ReqTarget::auto`]: Streams events from all relays with
/// [`RelayCapabilities::READ`]. If gossip is enabled
/// ([`ClientBuilder::gossip`]), NIP-65 relays are also included.
/// - [`ReqTarget::single`] / [`ReqTarget::manual`]: Streams events only from
/// the explicitly specified relays.
///
/// # Event Semantics
///
/// - Events are **deduplicated** across relays by event ID.
/// - Event signatures are **validated**.
/// - Events are **verified against the requested filters** if
/// [`ClientBuilder::verify_subscriptions`] is enabled.
/// - Event replacements, deletions, and other stateful event semantics
/// depend on the [`NostrDatabase`] implementation in use.
///
/// # Termination
///
/// The stream terminates when:
///
/// - The exit policy condition is met (i.e., EOSE),
/// - All relay streams terminate,
/// - Or an optional timeout expires.
///
/// # Errors
///
/// Returns an error if:
///
/// - The resolved target contains no relays,
/// - A specified relay does not exist in the pool,
/// - Target resolution fails.
///
/// Network or relay-specific errors are reported **inside the stream**
/// as `Err(relay::Error)` items.
///
/// # Examples
///
/// ## Single-filter REQ and custom exit policy
///
/// ```rust,no_run
/// # use std::time::Duration;
/// # use nostr_sdk::prelude::*;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::default();
/// let filter = Filter::new().kind(Kind::TextNote).limit(10);
///
/// let mut stream = client
/// .stream_events(filter)
/// .timeout(Duration::from_secs(10)) // Custom timeout
/// .policy(ReqExitPolicy::WaitForEvents(10)) // Custom policy
/// .await?;
///
/// while let Some((url, result)) = stream.next().await {
/// let event: Event = result?;
/// println!("Received an event from '{url}': {}", event.as_json());
/// }
/// # Ok(()) }
/// ```
///
/// ## Streams from the explicitly specified relays
///
/// ```rust,no_run
/// # use std::time::Duration;
/// # use std::collections::HashMap;
/// # use nostr_sdk::prelude::*;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::default();
/// // Subscribe with different filters per relay
/// let mut targets = HashMap::new();
/// targets.insert(
/// "wss://relay1.example.com",
/// vec![Filter::new().kind(Kind::TextNote).limit(10)],
/// );
/// targets.insert(
/// "wss://relay2.example.com",
/// vec![Filter::new().kind(Kind::Metadata).limit(5)],
/// );
///
/// let mut stream = client
/// .stream_events(targets)
/// .timeout(Duration::from_secs(10))
/// .await?;
///
/// while let Some((url, result)) = stream.next().await {
/// let event: Event = result?;
/// println!("Received an event from '{url}': {}", event.as_json());
/// }
/// # Ok(()) }
/// ```
#[inline]
pub fn stream_events<'client, 'url, F>(&'client self, target: F) -> StreamEvents<'client, 'url>
where
F: Into<ReqTarget<'url>>,
{
StreamEvents::new(self, target.into())
}
/// Fetch events from relays.
///
/// # Overview
///
/// Creates a short-lived event subscription and returns a list of events.
/// Compared to [`Client::stream_events`], this buffers events internally and returns them only after the stream terminates.
///
/// For long-lived subscriptions, use [`Client::subscribe`].
///
/// # Configuration
///
/// By default:
///
/// - No timeout is set
/// - Exit policy is [`ReqExitPolicy::ExitOnEOSE`](crate::relay::ReqExitPolicy::ExitOnEOSE)
///
/// To customize this behavior, the returned [`FetchEvents`] can be
/// configured before awaiting it:
///
/// - [`FetchEvents::timeout`]: set a maximum duration for the stream
/// - [`FetchEvents::policy`]: control when the stream terminates
/// - [`FetchEvents::max_events`]: set the maximum number of events buffered in memory
///
/// # Target Resolution, Event Semantics and Termination
///
/// See [`Client::stream_events`] for details on:
///
/// - Target resolution
/// - Event semantics
/// - Stream termination conditions
///
/// # Errors
///
/// Returns an error if:
///
/// - The resolved target contains no relays,
/// - A specified relay does not exist in the pool,
/// - Target resolution fails.
///
/// # Examples
///
/// ## Single-filter REQ and custom exit policy
///
/// ```rust,no_run
/// # use std::time::Duration;
/// # use nostr_sdk::prelude::*;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::default();
/// let filter = Filter::new().kind(Kind::TextNote).limit(10);
///
/// let events = client
/// .fetch_events(filter)
/// .timeout(Duration::from_secs(10)) // Custom timeout
/// .policy(ReqExitPolicy::WaitForEvents(10)) // Custom policy
/// .await?;
///
/// for event in events {
/// println!("{}", event.as_json());
/// }
/// # Ok(()) }
/// ```
///
/// ## Fetch from the explicitly specified relays
///
/// ```rust,no_run
/// # use std::time::Duration;
/// # use std::collections::HashMap;
/// # use nostr_sdk::prelude::*;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::default();
/// // Subscribe with different filters per relay
/// let mut targets = HashMap::new();
/// targets.insert(
/// "wss://relay1.example.com",
/// vec![Filter::new().kind(Kind::TextNote).limit(10)],
/// );
/// targets.insert(
/// "wss://relay2.example.com",
/// vec![Filter::new().kind(Kind::Metadata).limit(5)],
/// );
///
/// let events = client
/// .fetch_events(targets)
/// .timeout(Duration::from_secs(10))
/// .await?;
///
/// for event in events {
/// println!("{}", event.as_json());
/// }
/// # Ok(()) }
/// ```
#[inline]
pub fn fetch_events<'client, 'url, F>(&'client self, target: F) -> FetchEvents<'client, 'url>
where
F: Into<ReqTarget<'url>>,
{
FetchEvents::new(self, target.into())
}
/// Synchronize events with relays using negentropy.
///
/// # Overview
///
/// Performs a negentropy-based reconciliation between the local database
/// and one or more relays.
///
/// # Configuration
///
/// The returned [`SyncEvents`] builder can be configured before execution:
///
/// - [`SyncEvents::with`]: explicitly select which relays to synchronize with
/// - [`SyncEvents::opts`]: configure reconciliation behavior
///
/// If no relays are explicitly specified, the target set is resolved
/// automatically (see *Target Resolution*).
///
/// # Target Resolution
///
/// The set of relays to synchronize with is determined as follows:
///
/// - If relays are explicitly provided via [`SyncEvents::with`], only those
/// relays are used.
/// - Otherwise, if gossip is enabled ([`ClientBuilder::gossip`]), NIP-65 relays
/// are automatically discovered and used as targets.
/// - Otherwise, all relays in the pool with
/// [`RelayCapabilities::READ`] or [`RelayCapabilities::WRITE`] are used.
///
/// Each target relay receives the same filter, scoped to the events relevant
/// for reconciliation.
///
/// # Reconciliation Semantics
///
/// - Reconciliation is performed using NIP-77 negentropy
/// (<https://github.com/nostr-protocol/nips/blob/master/77.md>).
/// - Event transfer occurs **only** for events determined to be missing
/// on either side.
///
/// # Errors
///
/// Returns an error if:
///
/// - Target resolution fails,
/// - A specified relay URL is invalid,
/// - Database access fails,
/// - Or reconciliation cannot be initiated.
///
/// Relay-specific failures during event transfer are reported in the
/// returned [`SyncSummary`].
#[inline]
pub fn sync<'url>(&self, filter: Filter) -> SyncEvents<'_, 'url> {
SyncEvents::new(self, filter)
}
/// Sync events with specific relays (negentropy reconciliation)
///
/// <https://github.com/hoytech/negentropy>
#[deprecated(
since = "0.45.0",
note = "use `client.sync(filter).with(urls).await` instead"
)]
pub async fn sync_with<'a, I, U>(
&self,
urls: I,
filter: Filter,
opts: &SyncOptions,
) -> Result<Output<SyncSummary>, Error>
where
I: IntoIterator<Item = U>,
U: Into<RelayUrlArg<'a>>,
{
self.sync(filter).with(urls).opts(opts.clone()).await
}
/// Send a client message to relays.
///
/// # Overview
///
/// Sends a protocol-level [`ClientMessage`] to one or more relays.
///
/// This API is intended for sending raw client messages (e.g. subscription
/// control, close messages, or other relay commands) and does **not** perform
/// event persistence, gossip routing, or event-specific processing.
///
/// The operation completes once the message has been dispatched according
/// to the selected policy and optional waiting behavior.
///
/// # Configuration
///
/// By default:
/// - broadcast the message to [`RelayCapabilities::READ`] or [`RelayCapabilities::WRITE`] relays
/// - doesn't wait that the message is sent
///
/// The returned [`SendMessage`] builder can be configured before execution:
///
/// - [`SendMessage::broadcast`]: send the message to all relays with
/// [`RelayCapabilities::READ`] or [`RelayCapabilities::WRITE`]
/// - [`SendMessage::to`]: send the message only to explicitly specified relays
/// - [`SendMessage::wait_until_sent`]: wait until the message is sent or a
/// timeout expires
///
/// # Delivery Semantics
///
/// - Messages are sent once to each resolved relay.
/// - No retries are performed if delivery fails.
/// - Message ordering and relay acknowledgment depend on relay behavior.
///
/// If [`SendMessage::wait_until_sent`] is configured, the operation waits
/// until the message is sent or the timeout expires.
///
/// # Errors
///
/// Returns an error if:
///
/// - A specified relay URL is invalid,
/// - Or a specified relay does not exist in the pool.
///
/// Relay-specific delivery failures are reported in the returned [`Output`].
#[inline]
pub fn send_msg<'msg, 'url>(&self, msg: ClientMessage<'msg>) -> SendMessage<'_, 'msg, 'url> {
SendMessage::new(self, msg)
}
/// Send an event to relays.
///
/// # Overview
///
/// Sends an event to one or more relays and returns the event ID on
/// successful delivery.
///
/// By default, events are routed using the gossip engine (if configured in the [`ClientBuilder`]),
/// allowing the client to automatically discover appropriate relays and
/// establish connections as needed.
///
/// The operation completes once delivery attempts have finished according
/// to the selected routing, acknowledgement policy, and timeouts.
///
/// # Configuration
///
/// The returned [`SendEvent`] builder can be configured before execution:
///
/// - [`SendEvent::broadcast`]: send the event to all relays with
/// [`RelayCapabilities::WRITE`]
/// - [`SendEvent::to`]: send the event only to explicitly specified relays
/// - [`SendEvent::to_nip17`]: send the event to NIP-17 relays (requires gossip)
/// - [`SendEvent::to_nip65`]: send the event to NIP-65 relays (requires gossip)
/// - [`SendEvent::save_into_database`]: control whether the event is saved
/// locally before sending
/// - [`SendEvent::ack_policy`]: control relay `OK` acknowledgement behavior
/// - [`SendEvent::ok_timeout`]: set a timeout for waiting for `OK` responses
/// - [`SendEvent::authentication_timeout`]: set a timeout for relay authentication
///
/// # Target Resolution
///
/// The destination relays are resolved as follows:
///
/// - If [`SendEvent::to`] is used, the event is sent only to the specified
/// relays.
/// - If [`SendEvent::broadcast`] is used, the event is sent to all relays in
/// the pool with [`RelayCapabilities::WRITE`].
/// - If [`SendEvent::to_nip17`] or [`SendEvent::to_nip65`] is used, relay
/// selection is delegated to the gossip engine.
/// - If no explicit policy is set:
/// - Gossip is used when available,
/// - Otherwise, the event is broadcast to all WRITE relays.
///
/// # Persistence
///
/// By default, the event is saved into the local database **before** being
/// sent to relays.
///
/// This behavior can be disabled via [`SendEvent::save_into_database`].
///
/// # Acknowledgement Policy
///
/// By default, [`AckPolicy::all`] is used, so each selected relay send waits
/// for an `OK` response.
///
/// You can disable waiting for relay `OK` via [`SendEvent::ack_policy`] with
/// [`AckPolicy::none`]. In that mode, relay results are reported after
/// dispatching the `EVENT` message without waiting for relay confirmation.
///
/// # Errors
///
/// Returns an error if:
///
/// - Gossip-based routing is requested but gossip is not configured,
/// - A specified relay URL is invalid,
/// - A specified relay does not exist in the pool,
/// - The event cannot be saved to the database,
/// - Or sending cannot be initiated.
///
/// Relay-specific delivery results are reported in the returned [`SendEventOutput`].
#[inline]
pub fn send_event<'event, 'url>(&self, event: &'event Event) -> SendEvent<'_, 'event, 'url> {
SendEvent::new(self, event)
}
/// Send event to specific relays
///
/// # Gossip
///
/// If `gossip` is enabled and the [`Event`] is a NIP17/NIP65 relay list,
/// the gossip data will be updated.
#[deprecated(
since = "0.45.0",
note = "use `client.send_event(event).to(urls).await` instead"
)]
pub async fn send_event_to<'a, I, U>(
&self,
urls: I,
event: &Event,
) -> Result<SendEventOutput, Error>
where
I: IntoIterator<Item = U>,
U: Into<RelayUrlArg<'a>>,
{
self.send_event(event).to(urls).await
}
}
#[cfg(test)]
mod tests {
use nostr_gossip_memory::prelude::*;
use super::*;
use crate::error::ErrorKind;
use crate::local_relay::*;
use crate::relay::RelayStatus;
#[tokio::test]
async fn test_shutdown() {
let mock = MockRelay::run().await.unwrap();
let url = mock.url().await;
let client = Client::default();
client.add_relay(&url).await.unwrap();
client.connect().await;
assert!(!client.is_shutdown());
tokio::time::sleep(Duration::from_secs(1)).await;
client.shutdown().await;
// All relays must be removed
assert!(client.relays().all().await.is_empty());
// Client must be marked as shutdown
assert!(client.is_shutdown());
let err = client.add_relay(url).await.unwrap_err();
assert_eq!(err.kind(), ErrorKind::State);
assert_eq!(err.to_string(), "shutdown");
}
#[tokio::test]
async fn test_shutdown_on_drop() {
let mock = MockRelay::run().await.unwrap();
let url = mock.url().await;
let relay: Relay = {
let client: Client = Client::default();
client.add_relay(&url).and_connect().await.unwrap();
assert!(!client.is_shutdown());
tokio::time::sleep(Duration::from_millis(500)).await;
let relay = client.relay(&url).await.unwrap().unwrap();
assert!(relay.status().is_connected());
relay
};
// Client is dropped here
tokio::time::sleep(Duration::from_secs(1)).await;
// When the client is dropped, all relays are shutdown
assert_eq!(relay.status(), RelayStatus::Shutdown);
}
#[tokio::test]
async fn test_shutdown_on_drop_with_weak_clone() {
let weak: WeakClient = {
let client: Client = Client::default();
assert!(!client.is_shutdown());
// Weak clone
let weak: WeakClient = client.weak_clone();
// The client is still alive, so the upgrade must success
assert!(weak.upgrade().is_some());
weak
};
// Client is dropped here
// The client is dropped, so the upgrade must fail
assert!(weak.upgrade().is_none());
}
#[tokio::test]
async fn test_shutdown_on_drop_with_gossip_background_refresher_enabled() {
let weak: WeakClient = {
let gossip = NostrGossipMemory::unbounded();
let client: Client = Client::builder().gossip(gossip).build();
tokio::time::sleep(Duration::from_secs(1)).await;
assert!(!client.is_shutdown());
assert!(client.gossip().unwrap().is_background_refresher_spawned());
tokio::time::sleep(Duration::from_secs(1)).await;
// Check number of atomic references for the client
// Must be 2 because the background refresher is using the client
assert_eq!(Arc::strong_count(&client.0), 2);
tokio::time::sleep(Duration::from_secs(4)).await;
// Now should be just one atomic reference, as the background refresher is sleeping
assert_eq!(Arc::strong_count(&client.0), 1);
// Weak clone
let weak: WeakClient = client.weak_clone();
// The client is still alive, so the upgrade must success
assert!(weak.upgrade().is_some());
weak
};
// Client is dropped here
// The client is dropped, so the upgrade must fail
assert!(weak.upgrade().is_none());
}
#[tokio::test]
async fn test_terminate_notification_stream_on_shutdown() {
// Mock relay
let mock = MockRelay::run().await.unwrap();
let url = mock.url().await;
let client: Client = Client::default();
client.add_relay(&url).and_connect().await.unwrap();
// Shutdown after some time
let c = client.clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(3)).await;
c.shutdown().await;
});
assert!(!client.is_shutdown());
let fut = async {
let mut notifications = client.notifications();
let mut received = false;
// The stream must terminate after receiving the shutdown status
while let Some(n) = notifications.next().await {
if let ClientNotification::Shutdown = n {
received = true;
}
}
// Make sure we received the shutdown status
assert!(received);
};
tokio::time::timeout(Duration::from_secs(5), fut)
.await
.unwrap();
assert!(client.is_shutdown());
// Try to get a new stream
let mut notifications = client.notifications();
let res = tokio::time::timeout(Duration::from_secs(1), notifications.next())
.await
.unwrap();
// Must return None, as it's empty
assert!(res.is_none());
}
}