Documentation
¶
Overview ¶
Package ws provides both relay and client websocket implementations, including a pool for fanning out to multiple relays and managing subscriptions.
Package ws implements nostr websockets with their authentication state.
Index ¶
- Constants
- type Client
- func (r *Client) Auth(c context.T, sign signer.I) error
- func (r *Client) Close() error
- func (r *Client) Connect(c context.T) error
- func (r *Client) ConnectWithTLS(ctx context.T, tlsConfig *tls.Config) error
- func (r *Client) Context() context.T
- func (r *Client) IsConnected() bool
- func (r *Client) PrepareSubscription(c context.T, ff *filters.T, opts ...SubscriptionOption) *Subscription
- func (r *Client) Publish(c context.T, ev *event.E) error
- func (r *Client) QuerySync(ctx context.T, f *filter.F, opts ...SubscriptionOption) ([]*event.E, error)
- func (r *Client) String() string
- func (r *Client) Subscribe(c context.T, ff *filters.T, opts ...SubscriptionOption) (*Subscription, error)
- func (r *Client) Write(msg []byte) <-chan error
- type Connection
- type DirectedFilters
- type EventMessage
- type IncomingEvent
- type Listener
- func (ws *Listener) AuthRequested() (read bool)
- func (ws *Listener) AuthedPubkey() []byte
- func (ws *Listener) Challenge() []byte
- func (ws *Listener) Close() (err error)
- func (ws *Listener) IsAuthed() bool
- func (ws *Listener) RealRemote() string
- func (ws *Listener) Req() *http.Request
- func (ws *Listener) RequestAuth()
- func (ws *Listener) SetAuthed(b bool)
- func (ws *Listener) SetAuthedPubkey(b []byte)
- func (ws *Listener) SetChallenge(b []byte)
- func (ws *Listener) Write(p []byte) (n int, err error)
- func (ws *Listener) WriteJSON(any interface{}) error
- func (ws *Listener) WriteMessage(t int, b []byte) error
- type Pool
- func (pool *Pool) BatchedSubMany(c context.T, dfs []DirectedFilters) chan IncomingEvent
- func (pool *Pool) BatchedSubManyEose(c context.T, dfs []DirectedFilters) chan IncomingEvent
- func (pool *Pool) EnsureRelay(url string) (*Client, error)
- func (pool *Pool) QuerySingle(c context.T, urls []string, f *filter.F) *IncomingEvent
- func (pool *Pool) SubMany(c context.T, urls []string, ff *filters.T) chan IncomingEvent
- func (pool *Pool) SubManyEose(c context.T, urls []string, ff *filters.T) chan IncomingEvent
- func (pool *Pool) SubManyEoseNonUnique(c context.T, urls []string, ff *filters.T) chan IncomingEvent
- func (pool *Pool) SubManyNonUnique(c context.T, urls []string, ff *filters.T) chan IncomingEvent
- type PoolOption
- type RelayOption
- type Subscription
- type SubscriptionOption
- type WithAuthHandler
- type WithEventMiddleware
- type WithLabel
- type WithNoticeHandler
- type WithSignatureChecker
Constants ¶
const MaxLocks = 50
MaxLocks is the maximum number of sync.Mutex locks used in a pool todo: is this too few?
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
URL string
RequestHeader http.Header // e.g. for origin header
Connection *Connection
Subscriptions *xsync.MapOf[string, *Subscription]
ConnectionError error
AssumeValid bool // this will skip verifying signatures for events received from this relay
// contains filtered or unexported fields
}
func NewRelay ¶
func NewRelay(c context.T, url string, opts ...RelayOption) *Client
NewRelay returns a new relay. The relay connection will be closed when the context is canceled.
func RelayConnect ¶
RelayConnect returns a relay object connected to url. Once successfully connected, cancelling ctx has no effect. To close the connection, call r.Close().
func (*Client) Auth ¶
Auth sends an "AUTH" command client->relay as in NIP-42 and waits for an OK response.
func (*Client) Connect ¶
Connect tries to establish a websocket connection to r.URL. If the context expires before the connection is complete, an error is returned. Once successfully connected, context expiration has no effect: call r.Close to close the connection.
The underlying relay connection will use a background context. If you want to pass a custom context to the underlying relay connection, use NewRelay() and then Client.Connect().
func (*Client) ConnectWithTLS ¶
ConnectWithTLS tries to establish a secured websocket connection to r.URL using customized tls.Config (CA's, etc.).
func (*Client) Context ¶
Context retrieves the context that is associated with this relay connection.
func (*Client) IsConnected ¶
IsConnected returns true if the connection to this relay seems to be active.
func (*Client) PrepareSubscription ¶
func (r *Client) PrepareSubscription( c context.T, ff *filters.T, opts ...SubscriptionOption, ) *Subscription
PrepareSubscription creates a subscription, but doesn't fire it.
Remember to cancel subscriptions, either by calling `.Unsub()` on them or ensuring their `context.Context` will be canceled at some point. Failure to do that will result in a huge number of halted goroutines being created.
func (*Client) Publish ¶
Publish sends an "EVENT" command to the relay r as in NIP-01 and waits for an OK response.
func (*Client) QuerySync ¶
func (r *Client) QuerySync( ctx context.T, f *filter.F, opts ...SubscriptionOption, ) ([]*event.E, error)
QuerySync is only used in tests. The realy query method is synchronous now anyway (it ensures sort order is respected).
func (*Client) Subscribe ¶
func (r *Client) Subscribe( c context.T, ff *filters.T, opts ...SubscriptionOption, ) (*Subscription, error)
Subscribe sends a "REQ" command to the relay r as in NIP-01. Events are returned through the channel sub.Events. The subscription is closed when context ctx is cancelled ("CLOSE" in NIP-01).
Remember to cancel subscriptions, either by calling `.Unsub()` on them or ensuring their `context.Context` will be canceled at some point. Failure to do that will result in a huge number of halted goroutines being created.
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection is an outbound client -> relay connection.
func NewConnection ¶
func NewConnection( c context.T, url string, requestHeader http.Header, tlsConfig *tls.Config, ) (connection *Connection, errResult error)
NewConnection creates a new Connection.
func (*Connection) ReadMessage ¶
ReadMessage picks up the next incoming message on a Connection.
func (*Connection) WriteMessage ¶
func (cn *Connection) WriteMessage(c context.T, data []byte) (err error)
WriteMessage dispatches a message through the Connection.
type DirectedFilters ¶
type EventMessage ¶
EventMessage is an event, with the associated relay URL attached.
type IncomingEvent ¶
func (IncomingEvent) String ¶
func (ie IncomingEvent) String() string
type Listener ¶
type Listener struct {
Conn *websocket.Conn
Request *http.Request
// contains filtered or unexported fields
}
Listener is a websocket implementation for a relay listener.
func NewListener ¶
NewListener creates a new Listener for listening for inbound connections for a relay.
func (*Listener) AuthRequested ¶
AuthRequested returns whether the Listener has asked for auth from the client.
func (*Listener) AuthedPubkey ¶
func (*Listener) RealRemote ¶
RealRemote returns the stored remote address of the client.
func (*Listener) Req ¶
Req returns the http.Request associated with the client connection to the Listener.
func (*Listener) RequestAuth ¶
func (ws *Listener) RequestAuth()
RequestAuth stores when auth has been required from a client.
func (*Listener) SetAuthedPubkey ¶
func (*Listener) SetChallenge ¶
type Pool ¶
type Pool struct {
Relays *xsync.MapOf[string, *Client]
Context context.T
// custom things not often used
SignatureChecker func(*event.E) bool
// contains filtered or unexported fields
}
func (*Pool) BatchedSubMany ¶
func (pool *Pool) BatchedSubMany( c context.T, dfs []DirectedFilters, ) chan IncomingEvent
BatchedSubMany fires subscriptions only to specific relays but batches them when they are the same.
func (*Pool) BatchedSubManyEose ¶
func (pool *Pool) BatchedSubManyEose( c context.T, dfs []DirectedFilters, ) chan IncomingEvent
BatchedSubManyEose is like BatchedSubMany, but ends upon receiving EOSE from all relays.
func (*Pool) EnsureRelay ¶
EnsureRelay connects a pool to a relay or fails.
func (*Pool) QuerySingle ¶
QuerySingle returns the first event returned by the first relay, cancels everything else.
func (*Pool) SubMany ¶
SubMany opens a subscription with the given filters to multiple relays the subscriptions only end when the context is canceled
func (*Pool) SubManyEose ¶
SubManyEose is like SubMany, but it stops subscriptions and closes the channel when gets a EOSE
func (*Pool) SubManyEoseNonUnique ¶
func (pool *Pool) SubManyEoseNonUnique( c context.T, urls []string, ff *filters.T, ) chan IncomingEvent
SubManyEoseNonUnique is like SubManyEose, but returns duplicate events if they come from different relays
func (*Pool) SubManyNonUnique ¶
SubManyNonUnique is like SubMany, but returns duplicate events if they come from different relays
type PoolOption ¶
type PoolOption interface {
ApplyPoolOption(*Pool)
}
type RelayOption ¶
type RelayOption interface {
ApplyRelayOption(*Client)
}
RelayOption is the type of the argument passed for that.
type Subscription ¶
type Subscription struct {
Relay *Client
Filters *filters.T
// The Events channel emits all EVENTs that come in a Subscription will be
// closed when the subscription ends
Events event.C
// The EndOfStoredEvents channel is closed when an EOSE comes for that
// subscription
EndOfStoredEvents chan struct{}
// The ClosedReason channel emits the reason when a CLOSED message is
// received
ClosedReason chan string
// Context will be .Done() when the subscription ends
Context context.T
// contains filtered or unexported fields
}
Subscription is a client interface for a subscription (what REQ turns into after EOSE).
func (*Subscription) Close ¶
func (sub *Subscription) Close()
Close just sends a CLOSE message. You probably want Unsub() instead.
func (*Subscription) Fire ¶
func (sub *Subscription) Fire() (err error)
Fire sends the "REQ" command to the relay.
func (*Subscription) GetID ¶
func (sub *Subscription) GetID() (id *subscription.Id)
GetID return the Nostr subscription Id as given to the Client it is a concatenation of the label and a serial number.
func (*Subscription) Sub ¶
func (sub *Subscription) Sub(_ context.T, ff *filters.T)
Sub sets sub.Filters and then calls sub.Fire(ctx). The subscription will be closed if the context expires.
func (*Subscription) Unsub ¶
func (sub *Subscription) Unsub()
Unsub closes the subscription, sending "CLOSE" to relay as in NIP-01. Unsub() also closes the channel sub.Events and makes a new one.
type SubscriptionOption ¶
type SubscriptionOption interface {
IsSubscriptionOption()
}
SubscriptionOption is the type of the argument passed for that. Some examples are WithLabel.
type WithAuthHandler ¶
WithAuthHandler must be a function that signs the auth event when called. it will be called whenever any relay in the pool returns a `CLOSED` message with the "auth-required:" prefix, only once for each relay
func (WithAuthHandler) ApplyPoolOption ¶
func (h WithAuthHandler) ApplyPoolOption(pool *Pool)
type WithEventMiddleware ¶
type WithEventMiddleware func(IncomingEvent)
WithEventMiddleware is a function that will be called with all events received. more than one can be passed at a time.
func (WithEventMiddleware) ApplyPoolOption ¶
func (h WithEventMiddleware) ApplyPoolOption(pool *Pool)
type WithLabel ¶
type WithLabel string
WithLabel puts a label on the subscription (it is prepended to the automatic id) that is sent to relays.
func (WithLabel) IsSubscriptionOption ¶
func (_ WithLabel) IsSubscriptionOption()
type WithNoticeHandler ¶
type WithNoticeHandler func(notice []byte)
WithNoticeHandler just takes notices and is expected to do something with them. when not given, defaults to logging the notices.
func (WithNoticeHandler) ApplyRelayOption ¶
func (nh WithNoticeHandler) ApplyRelayOption(r *Client)
type WithSignatureChecker ¶
WithSignatureChecker must be a function that checks the signature of an event and returns true or false.
func (WithSignatureChecker) ApplyRelayOption ¶
func (sc WithSignatureChecker) ApplyRelayOption(r *Client)