Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
A Client is a Nostr client that connects to a relay server.
func NewClient ¶
NewClient creates a new Nostr client. It establishes a websocket connection to the relay server.
func (*Client) Publish ¶
Publish submits an event to the relay server and waits for the command result.
Example ¶
package main
import (
"context"
"time"
"github.com/shota3506/go-nostr"
)
func main() {
client, err := nostr.NewClient("URL") // TODO: Replace URL with a relay server URL.
if err != nil {
// TODO: Handle error.
}
defer client.Close()
event := &nostr.Event{
CreatedAt: time.Now().Unix(),
Kind: nostr.EventKindTextNote,
Content: "Hello, World!",
Tags: []nostr.Tag{},
}
event.Sign("PRIVATE_KEY") // TODO: Replace PRIVATE_KEY with your private key.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
result, err := client.Publish(ctx, event)
if err != nil {
// TODO: Handle error.
}
_ = result // TODO: Check result.
}
Output:
func (*Client) Subscribe ¶
Subscribe creates a subscription to the relay server with the given filters.
Example ¶
package main
import (
"context"
"time"
"github.com/shota3506/go-nostr"
)
func main() {
client, err := nostr.NewClient("URL") // TODO: Replace URL with a relay server URL.
if err != nil {
// TODO: Handle error.
}
defer client.Close()
ctx := context.Background()
sub, err := client.Subscribe(ctx, []nostr.Filter{{}})
if err != nil {
// TODO: Handle error.
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
err = sub.Receive(ctx, func(ctx context.Context, event *nostr.Event) {
// TODO: Do something with event.
})
}
Output:
type CloseMessage ¶
type CloseMessage struct {
SubscriptionID string
}
A CloseMessage is a close message. It's used to stop previous subscriptions.
func (*CloseMessage) MarshalJSON ¶
func (m *CloseMessage) MarshalJSON() ([]byte, error)
type CommandResult ¶
A CommandResult is a result of a command.
type EOSEMessage ¶
type EOSEMessage struct {
SubscriptionID string
}
A EOSEMessage is a EOSE message. It's used to notify clients all stored events have been sent.
type Event ¶
type Event struct {
ID string `json:"id"`
PubKey string `json:"pubkey"`
CreatedAt int64 `json:"created_at"`
Kind EventKind `json:"kind"`
Tags []Tag `json:"tags"`
Content string `json:"content"`
Sig string `json:"sig"`
}
Event is an Nostr event.
type EventKind ¶
type EventKind int64
EventKind is the kind of an event.
const ( EventKindSetMetadata EventKind = 0 // NIP-01 EventKindTextNote EventKind = 1 // NIP-01 EventKindRecommendServer EventKind = 2 // NIP-01 EventKindContacts EventKind = 3 // NIP-02 EventKindEncryptedDirectMessages EventKind = 4 // NIP-04 EventKindEventDeletion EventKind = 5 // NIP-09 EventKindReposts EventKind = 6 // NIP-18 EventKindReaction EventKind = 7 // NIP-25 EventKindBadgeAward EventKind = 8 // NIP-58 EventKindChannelCreation EventKind = 40 // NIP-28 EventKindChannelMetadata EventKind = 41 // NIP-28 EventKindChannelMessage EventKind = 42 // NIP-28 EventKindChannelHideMessage EventKind = 43 // NIP-28 EventKindChannelMuteUser EventKind = 44 // NIP-28 EventKindFileMetadata EventKind = 1063 // NIP-94 EventKindReporting EventKind = 1984 // NIP-56 EventKindZapRequest EventKind = 9734 // NIP-57 EventKindZap EventKind = 9735 // NIP-57 )
type EventMessage ¶
A EventMessagek is an event message. It's used to publish events from clients or to send events requested to clients
func (*EventMessage) MarshalJSON ¶
func (m *EventMessage) MarshalJSON() ([]byte, error)
type Filter ¶
type Filter struct {
IDs []string `json:"ids,omitempty"`
Kinds []EventKind `json:"kinds,omitempty"`
Authors []string `json:"authors,omitempty"`
Tags []Tag `json:"-,omitempty"`
Since int64 `json:"since,omitempty"` // TODO: use box type
Until int64 `json:"until,omitempty"` // TODO: use box type
Limit int `json:"limit,omitempty"`
Search string `json:"search,omitempty"`
}
A Filter is a filter for subscription.
type MessageType ¶
type MessageType string
MessageType is the type of a message.
const ( MessageTypeEvent MessageType = "EVENT" // NIP-01 MessageTypeReq MessageType = "REQ" // NIP-01 MessageTypeClose MessageType = "CLOSE" // NIP-01 MessageTypeNotice MessageType = "NOTICE" // NIP-01 MessageTypeEOSE MessageType = "EOSE" // NIP-01 MessageTypeOK MessageType = "OK" // NIP-20 )
type NoticeMessage ¶
type NoticeMessage struct {
Message string
}
A NoticeMessage is a notice message. It's used to send human-readable messages to clients.
type OKMessage ¶
A OKMessage is a OK message. It's used to notify clients if an EVENT was successful.
type ReqMessage ¶
A ReqMessage is a request message. It's used to request events and subscribe to new updates.
func (*ReqMessage) MarshalJSON ¶
func (m *ReqMessage) MarshalJSON() ([]byte, error)
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
A Subscription is a subscription to a channel.