Skip to content

Getting started

This gem abstracts the complexity that you would face when trying to connect to relays web sockets, send and receive events, handle events callbacks and much more.

Visual overview

Begin your journey with an overview of the essential functions. A visual representation below maps out the key components we'll delve into in this section.


Code overview

Explore the provided code snippet to learn about initializing the Nostr client, generating a keypair, publishing an event, and efficiently managing event subscriptions (including event reception, filtering, and WebSocket event handling).

# Require the gem
require 'nostr'

# Instantiate a client
client =

# a) Use an existing keypair
keypair =

# b) Or build a keypair from a private key
keygen =
keypair = keygen.get_key_pair_from_private_key('your-hex-private-key')

# c) Or create a new keypair
keygen =
keypair = keygen.generate_key_pair

# Create a user with the keypair
user = 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 = 'wss://', name: 'Wine')

# Listen asynchronously for the connect event
client.on :connect do
  # Send the event to the Relay

  # Create a filter to receive the first 20 text notes
  # and encrypted direct messages from the relay that
  # were created in the previous hour
  filter =
    kinds: [
    since: - 3600, # 1 hour ago
    limit: 20,

  # Subscribe to events matching conditions of a filter
  subscription = client.subscribe(filter: filter)

  # Unsubscribe from events matching the filter above

# Listen for incoming messages and print them
client.on :message do |message|
  puts message

# Listen for error messages
client.on :error do |error_message|
  # Handle the error

# Listen for the close event
client.on :close do |code, reason|
  # You may attempt to reconnect to the relay here

# This line keeps the background client from exiting immediately.

Beyond what's covered here, the Nostr protocol and this gem boast a wealth of additional functionalities. For an in-depth exploration of these capabilities, proceed to the next page.