Class: Nostr::Client

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

Constructor Details

#initializeClient

Instantiates a new Client

Examples:

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

client = Nostr::Client.new(debug: true)


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

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



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



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

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. A random string.

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

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

Returns:



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

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.



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