21
docs/relays/connecting-to-a-relay.md
Normal file
21
docs/relays/connecting-to-a-relay.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# Connecting to a Relay
|
||||
|
||||
You must connect your nostr [Client](../core/client) to a relay in order to send and receive [Events](../events).
|
||||
Instantiate a [`Nostr::Client`](https://www.rubydoc.info/gems/nostr/Nostr/Client) and a
|
||||
[`Nostr::Relay`](https://www.rubydoc.info/gems/nostr/Nostr/Relay) giving it the `url` of your relay. The `name`
|
||||
attribute is just descriptive.
|
||||
Calling [`Client#connect`](https://www.rubydoc.info/gems/nostr/Nostr/Client#connect-instance_method) attempts to
|
||||
establish a WebSocket connection between the Client and the Relay.
|
||||
|
||||
```ruby
|
||||
client = Nostr::Client.new
|
||||
relay = Nostr::Relay.new(url: 'wss://relay.damus.io', name: 'Damus')
|
||||
|
||||
# Listen for the connect event
|
||||
client.on :connect do
|
||||
# When this block executes, you're connected to the relay
|
||||
end
|
||||
|
||||
# Connect to a relay asynchronously
|
||||
client.connect(relay)
|
||||
```
|
||||
29
docs/relays/publishing-events.md
Normal file
29
docs/relays/publishing-events.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Publishing events
|
||||
|
||||
Create a [signed event](../core/keys) and call the method
|
||||
[`Nostr::Client#publish`](https://www.rubydoc.info/gems/nostr/Nostr/Client#publish-instance_method) to send the
|
||||
event to the relay.
|
||||
|
||||
```ruby{4-8,17}
|
||||
# Create a user with the keypair
|
||||
user = Nostr::User.new(keypair: keypair)
|
||||
|
||||
# Create a signed event
|
||||
text_note_event = user.create_event(
|
||||
kind: Nostr::EventKind::TEXT_NOTE,
|
||||
content: 'Your feedback is appreciated, now pay $8'
|
||||
)
|
||||
|
||||
# Connect asynchronously to a relay
|
||||
relay = Nostr::Relay.new(url: 'wss://nostr.wine', name: 'Wine')
|
||||
client.connect(relay)
|
||||
|
||||
# Listen asynchronously for the connect event
|
||||
client.on :connect do
|
||||
# Send the event to the relay
|
||||
client.publish(text_note_event)
|
||||
end
|
||||
```
|
||||
|
||||
The relay will verify the signature of the event with the public key. If the signature is valid, the relay should
|
||||
broadcast the event to all subscribers.
|
||||
6
docs/relays/receiving-events.md
Normal file
6
docs/relays/receiving-events.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Receiving events
|
||||
|
||||
To receive events from Relays, you must create a subscription on the relay. A subscription is a filter that defines the
|
||||
events you want to receive.
|
||||
|
||||
For more information, read the [Subscription](../subscriptions/creating-a-subscription.md) section.
|
||||
Reference in New Issue
Block a user