Skip to content

Relay

WebSocket connection to a single Nostr relay.

Import

ts
import { Relay } from 'nostr-core'

Constructor

ts
new Relay(url: string, opts?: {
  websocketImplementation?: typeof WebSocket
})
ParameterTypeDescription
urlstringRelay WebSocket URL
opts.websocketImplementationtypeof WebSocket?Custom WebSocket class

The URL is normalized automatically (see normalizeURL).

Properties

url

ts
relay.url: string // readonly

The normalized relay URL.

connected

ts
relay.connected: boolean // getter

Whether the WebSocket connection is active.

eoseTimeout

ts
relay.eoseTimeout: number // default: 4400

Milliseconds to wait for EOSE before timing out a subscription.

publishTimeout

ts
relay.publishTimeout: number // default: 4400

Milliseconds to wait for OK response after publishing.

openSubs

ts
relay.openSubs: Map<string, Subscription>

Currently active subscriptions, keyed by subscription ID.

Methods

connect

ts
await relay.connect(opts?: { timeout?: number }): Promise<void>

Establishes the WebSocket connection. All subscriptions are closed if the connection drops.

ParameterTypeDescription
opts.timeoutnumber?Connection timeout in ms

Throws: Error on connection failure or timeout.

publish

ts
await relay.publish(event: NostrEvent): Promise<string>

Publishes an event and waits for the relay's OK response.

ParameterTypeDescription
eventNostrEventSigned event to publish

Returns: string - reason from the relay's OK message.

Throws: Error on timeout or NACK (negative acknowledgement).

subscribe

ts
relay.subscribe(filters: Filter[], params: SubscriptionParams & { id?: string }): Subscription

Creates a subscription and immediately starts receiving events.

ParameterTypeDescription
filtersFilter[]Array of event filters
paramsSubscriptionParamsCallbacks and options
params.idstring?Custom subscription ID

Returns: Subscription

send

ts
relay.send(message: string): void

Sends a raw message over the WebSocket.

Throws: Error if not connected.

close

ts
relay.close(): void

Closes all subscriptions and the WebSocket connection.


Subscription

Represents an active subscription to a relay.

Properties

PropertyTypeDescription
relayRelayParent relay (readonly)
idstringSubscription ID (readonly)
closedbooleanWhether closed
eosedbooleanWhether EOSE received
filtersFilter[]Active filters

Callbacks

CallbackTypeDescription
onevent(evt: NostrEvent) => voidCalled for each matching event
oneose(() => void)?Called when stored events are exhausted
onclose((reason: string) => void)?Called when subscription closes

Methods

close

ts
sub.close(reason?: string): void

Sends a CLOSE message to the relay and calls onclose.

fire

ts
sub.fire(): void

Sends the REQ message to the relay. Called automatically by relay.subscribe().


SubscriptionParams

ts
type SubscriptionParams = {
  onevent?: (evt: NostrEvent) => void
  oneose?: () => void
  onclose?: (reason: string) => void
  eoseTimeout?: number
}

Released under the MIT License.