Skip to content

openfort-xyz/pubsub

Repository files navigation

pubsub

import "go.openfort.xyz/pubsub"

Index

type Event

Event represents a message published to a topic.

type Event struct {
    Topic    Topic    `json:"event"`
    Metadata Metadata `json:"metadata,omitempty"`
    Payload  []byte   `json:"payload"`
}

func NewEvent

func NewEvent(topic Topic, payload []byte) *Event

NewEvent creates a new event.

type Handler

Handler is the function that processes the event.

type Handler func(ctx context.Context, event *Event) error

type Listener

Listener is the interface that subscriber uses to connect to the message broker agnostic of the underlying implementation.

type Listener interface {
    // Connect connects to the message broker.
    Connect(ctx context.Context) error

    // Subscribe subscribes to a topic and returns messages to the subscription channel.
    Subscribe(ctx context.Context, subscription *Subscription) error

    // Close closes the connection to the message broker.
    Close() error
}

type Metadata

Metadata represents the key-value pairs of an event.

type Metadata map[string]interface{}

func NewMetadata

func NewMetadata() Metadata

NewMetadata creates a new metadata.

func (Metadata) Del

func (m Metadata) Del(key string)

Del deletes the key-value pair from the metadata.

func (Metadata) Get

func (m Metadata) Get(key string) (interface{}, bool)

Get returns the value associated with the key.

func (Metadata) Keys

func (m Metadata) Keys() []string

Keys returns the keys of the metadata.

func (Metadata) Set

func (m Metadata) Set(key string, value interface{})

Set sets the key-value pair in the metadata.

type Middleware

Middleware is the function that wraps a Handler to add functionality.

type Middleware func(next Handler) Handler

type Publisher

Publisher is the struct that sends messages to the message broker using the Transport interface agnostic of the underlying implementation.

type Publisher struct {
    Transport Transport
}

func NewPublisher

func NewPublisher(transport Transport) *Publisher

NewPublisher creates a new publisher with the given transport.

func (*Publisher) Publish

func (p *Publisher) Publish(ctx context.Context, event *Event) error

Publish sends the event to the message broker.

func (*Publisher) Use

func (p *Publisher) Use(transport TransportWrapper)

Use adds a transport wrapper to the publisher.

type Subscriber

Subscriber is the struct that receives events from the listener and calls the appropriate Handler using the Listener interface agnostic of the underlying implementation.

type Subscriber struct {
    // contains filtered or unexported fields
}

func NewSubscriber

func NewSubscriber(listener Listener) *Subscriber

NewSubscriber creates a new Subscriber with the given Listener.

func (*Subscriber) HandleFunc

func (s *Subscriber) HandleFunc(topic Topic, handler Handler)

HandleFunc adds a Handler to the Subscriber for the given Topic.

func (*Subscriber) Start

func (s *Subscriber) Start(ctx context.Context) error

Start starts the Subscriber and listens for events from the Listener.

func (*Subscriber) Stop

func (s *Subscriber) Stop(_ context.Context) error

Stop stops gracefully the Subscriber and closes the Listener.

func (*Subscriber) Use

func (s *Subscriber) Use(middleware Middleware)

Use adds a Middleware to the Subscriber.

type Subscription

Subscription is the struct that represents a subscription to a Topic with a channel to receive Event.

type Subscription struct {
    Topic    Topic
    Consumer string
    Channel  chan *Event
}

func NewSubscription

func NewSubscription(topic Topic) *Subscription

NewSubscription creates a new Subscription for the given Topic.

type Topic

Topic represents a message topic.

type Topic string

const (
    // SendNotificationTopic is the topic for sending notifications.
    SendNotificationTopic Topic = "send.notification"

    // UserOperationTopic is the topic for user operations.
    UserOperationTopic Topic = "user.operation"

    // TransactionUpdatedTopic is the topic for transaction updates.
    TransactionUpdatedTopic Topic = "transaction.updated"

    // WriteMetricTopic is the topic for writing metrics.
    WriteMetricTopic Topic = "write.metric"
)

func (Topic) String

func (t Topic) String() string

String returns the string representation of the topic.

type Transport

Transport is the interface that publisher uses to send messages to the message broker agnostic of the underlying implementation.

type Transport interface {
    Send(ctx context.Context, event *Event) error
}

type TransportFunc

TransportFunc is a function type that implements the Transport interface.

type TransportFunc func(ctx context.Context, event *Event) error

func (TransportFunc) Send

func (f TransportFunc) Send(ctx context.Context, event *Event) error

Send calls f(ctx, event).

type TransportWrapper

TransportWrapper is a function that wraps a transport with additional functionality.

type TransportWrapper func(Transport) Transport

Generated by gomarkdoc

About

Messaging library for Go with publisher/subscriber capabilities. Simplifies building scalable, event-driven applications.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages