Class: Nostr::Client
- Inherits:
-
Object
- Object
- Nostr::Client
- Includes:
- EventEmitter
- Defined in:
- lib/nostr/client.rb,
lib/nostr/client/logger.rb,
lib/nostr/client/color_logger.rb,
lib/nostr/client/plain_logger.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.
Defined Under Namespace
Classes: ColorLogger, Logger, PlainLogger
Instance Method Summary collapse
-
#connect(relay) ⇒ void
Connects to the Relay’s websocket endpoint.
-
#initialize(logger: ColorLogger.new) ⇒ 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(logger: ColorLogger.new) ⇒ Client
Instantiates a new Client
29 30 31 32 33 34 35 |
# File 'lib/nostr/client.rb', line 29 def initialize(logger: ColorLogger.new) @subscriptions = {} @logger = logger logger&.attach_to(self) initialize_channels end |
Instance Method Details
#connect(relay) ⇒ void
This method returns an undefined value.
Connects to the Relay’s websocket endpoint
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/nostr/client.rb', line 49 def connect(relay) execute_within_an_em_thread do client = build_websocket_client(relay.url) parent_to_child_channel.subscribe { |msg| client.send(msg) && emit(:send, msg) } client.on :open do child_to_parent_channel.push(type: :open, relay:) 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
127 128 129 |
# File 'lib/nostr/client.rb', line 127 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
91 92 93 94 95 |
# File 'lib/nostr/client.rb', line 91 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
111 112 113 114 |
# File 'lib/nostr/client.rb', line 111 def unsubscribe(subscription_id) subscriptions.delete(subscription_id) parent_to_child_channel.push([ClientMessageType::CLOSE, subscription_id].to_json) end |