Class: Nostr::Client
- Inherits:
-
Object
- Object
- Nostr::Client
- Includes:
- EventEmitter
- Defined in:
- lib/nostr/client.rb
Overview
Clients can talk with relays and can subscribe to any set of events using a subscription filters. The filter represents all the set of nostr events that a client is interested in.
There is no sign-up or account creation for a client. Every time a client connects to a relay, it submits its subscription filters and the relay streams the “interested events” to the client as long as they are connected.
Instance Method Summary collapse
-
#connect(relay) ⇒ void
Connects to the Relay’s websocket endpoint.
-
#initialize ⇒ Client
constructor
Instantiates a new Client.
-
#publish(event) ⇒ void
Sends an event to a Relay.
-
#subscribe(subscription_id: SecureRandom.hex, filter: Filter.new) ⇒ Subscription
Subscribes to a set of events using a filter.
-
#unsubscribe(subscription_id) ⇒ void
Stops a previous subscription.
Constructor Details
#initialize ⇒ Client
Instantiates a new Client
23 24 25 26 27 |
# File 'lib/nostr/client.rb', line 23 def initialize @subscriptions = {} initialize_channels end |
Instance Method Details
#connect(relay) ⇒ void
This method returns an undefined value.
Connects to the Relay’s websocket endpoint
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/nostr/client.rb', line 41 def connect(relay) execute_within_an_em_thread do client = Faye::WebSocket::Client.new(relay.url, [], { tls: { verify_peer: false } }) parent_to_child_channel.subscribe { |msg| client.send(msg) } client.on :open do child_to_parent_channel.push(type: :open) end client.on :message do |event| child_to_parent_channel.push(type: :message, data: event.data) end client.on :error do |event| child_to_parent_channel.push(type: :error, message: event.) end client.on :close do |event| child_to_parent_channel.push(type: :close, code: event.code, reason: event.reason) end end end |
#publish(event) ⇒ void
This method returns an undefined value.
Sends an event to a Relay
118 119 120 |
# File 'lib/nostr/client.rb', line 118 def publish(event) parent_to_child_channel.push([ClientMessageType::EVENT, event.to_h].to_json) end |
#subscribe(subscription_id: SecureRandom.hex, filter: Filter.new) ⇒ Subscription
Subscribes to a set of events using a filter
82 83 84 85 86 |
# File 'lib/nostr/client.rb', line 82 def subscribe(subscription_id: SecureRandom.hex, filter: Filter.new) subscriptions[subscription_id] = Subscription.new(id: subscription_id, filter:) parent_to_child_channel.push([ClientMessageType::REQ, subscription_id, filter.to_h].to_json) subscriptions[subscription_id] end |
#unsubscribe(subscription_id) ⇒ void
This method returns an undefined value.
Stops a previous subscription
102 103 104 105 |
# File 'lib/nostr/client.rb', line 102 def unsubscribe(subscription_id) subscriptions.delete(subscription_id) parent_to_child_channel.push([ClientMessageType::CLOSE, subscription_id].to_json) end |