Class: Nostr::Client

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(logger: ColorLogger.new) ⇒ Client

Instantiates a new Client

Examples:

Instantiating a client that logs all the events it sends and receives

client = Nostr::Client.new

Instantiating a client with no logging

client = Nostr::Client.new(logger: nil)

Instantiating a client with your own logger

client = Nostr::Client.new(logger: YourLogger.new)


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

Examples:

Connecting to a relay

relay = Nostr::Relay.new(url: 'wss://relay.damus.io', name: 'Damus')
client.connect(relay)

Parameters:

  • relay (Relay)

    The relay to connect to



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.message)
    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

Examples:

Sending an event to a relay

client.publish(event)

Parameters:

  • event (Event)

    The event to be sent 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

Examples:

Creating a subscription with no id and no filters

subscription = client.subscribe

Creating a subscription with an ID

subscription = client.subscribe(subscription_id: 'my-subscription')

Subscribing to all events created after a certain time

subscription = client.subscribe(filter: Nostr::Filter.new(since: 1230981305))

Parameters:

  • subscription_id (String) (defaults to: SecureRandom.hex)

    The subscription id. An arbitrary, non-empty string of max length 64 chars used to represent a subscription.

  • filter (Filter) (defaults to: Filter.new)

    A set of attributes that represent the events that the client is interested in.

Returns:



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

Examples:

Stopping a subscription

client.unsubscribe(subscription.id)

Stopping a subscription

client.unsubscribe('my-subscription')

Parameters:

  • subscription_id (String)

    ID of a previously created 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