init method
- required List<
String> relaysUrl, - void onRelayListening(
- String relayUrl,
- dynamic receivedData,
- WebSocketChannel? relayWebSocket
- void onRelayConnectionError()?,
- void onRelayConnectionDone(
- String relayUrl,
- WebSocketChannel? relayWebSocket
- bool lazyListeningToRelays = false,
- bool retryOnError = false,
- bool retryOnClose = false,
- bool ensureToClearRegistriesBeforeStarting = true,
- bool ignoreConnectionException = true,
- bool shouldReconnectToRelayOnNotice = false,
- Duration connectionTimeout = const Duration(seconds: 5),
This method is responsible for initializing the connection to all relays.
It takes a List<String> of relays urls, then it connects to each relay and registers it for future use, if relayUrl
is empty, it will throw an AssertionError since it doesn't make sense to connect to an empty list of relays.
The WebSockets of the relays will start being listened to get events from them immediately after calling this method, unless you set the lazyListeningToRelays
parameter to true
, then you will have to call the startListeningToRelay method to start listening to the relays manually.
You can also pass a callback to the onRelayListening
parameter to be notified when a relay starts listening to it's websocket.
You can also pass a callback to the onRelayConnectionError
parameter to be notified when a relay websocket throws an error.
You can also pass a callback to the onRelayConnectionDone
parameter to be notified when a relay websocket is closed.
You can choose lazyListeningToRelays
to true
if you want to start listening to relays manually, this is useful if you want to start listening to relays after you called the init method.
If you want to retry connecting to relays in case of an error, you can set the retryOnError
parameter to true
, and if you want to retry connecting to relays in case of a close, you can set the retryOnClose
parameter to true
.
If you want to clear all registries before starting, you can set the ensureToClearRegistriesBeforeStarting
parameter to true
, the first time you call the init method, the registries will be always cleared, but if you want to clear them before each call to the init method (as example for implementing a reconnect mechanism), you can set this parameter to true
.
If you want to ignore connection exceptions, you can set the ignoreConnectionException
parameter to true
, this is useful if you want to ignore connection exceptions and retry connecting to relays in case of an error, you can do that by setting the retryOnError
parameter to true
.
You will need to call this method before using any other method, as example, in your main()
method to make sure that the connection is established before using any other method.
void main() async {
await Nostr.instance.relays.init(
relaysUrl: ["ws://localhost:8080"],
onRelayListening: (relayUrl) {
print("relay with url: $relayUrl is listening");
},
onRelayConnectionError: (relayUrl, error) {
print("relay with url: $relayUrl has thrown an error: $error");
},
onRelayConnectionDone: (relayUrl) {
print("relay with url: $relayUrl is closed");
},
);
runApp(MyApp());
}
You can also use this method to re-connect to all relays in case of a connection failure.
Implementation
@override
Future<void> init({
required List<String> relaysUrl,
void Function(
String relayUrl,
dynamic receivedData,
WebSocketChannel? relayWebSocket,
)? onRelayListening,
void Function(
String relayUrl, Object? error, WebSocketChannel? relayWebSocket,)?
onRelayConnectionError,
void Function(String relayUrl, WebSocketChannel? relayWebSocket)?
onRelayConnectionDone,
bool lazyListeningToRelays = false,
bool retryOnError = false,
bool retryOnClose = false,
bool ensureToClearRegistriesBeforeStarting = true,
bool ignoreConnectionException = true,
bool shouldReconnectToRelayOnNotice = false,
Duration connectionTimeout = const Duration(seconds: 5),
}) async {
assert(
relaysUrl.isNotEmpty,
"initiating relays with an empty list doesn't make sense, please provide at least one relay url.",
);
relaysList = List.of(relaysUrl);
return _startConnectingAndRegisteringRelays(
relaysUrl: relaysUrl,
onRelayListening: onRelayListening,
onRelayConnectionError: onRelayConnectionError,
onRelayConnectionDone: onRelayConnectionDone,
lazyListeningToRelays: lazyListeningToRelays,
retryOnError: retryOnError,
retryOnClose: retryOnClose,
ignoreConnectionException: ignoreConnectionException,
shouldReconnectToRelayOnNotice: shouldReconnectToRelayOnNotice,
connectionTimeout: connectionTimeout,
);
}