pubsub

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 6 Imported by: 0

README

kit-pubsub

Go Report Card codecov Go Reference

A simplified, testable Pub/Sub V2 client wrapper for Go.

Installation

go get github.com/DeltaArchitect/kit-pubsub

Usage

Initialization
import "github.com/DeltaArchitect/kit-pubsub"

ctx := context.Background()
client, err := pubsub.NewClient(ctx, "your-project-id")
if err != nil {
    // handle error
}
defer client.Close()
Publishing Messages
topicID := "your-topic"
data := []byte("Hello, Pub/Sub!")
id, err := client.Publish(ctx, topicID, data)
if err != nil {
    // handle error
}
fmt.Printf("Published message with ID: %s\n", id)
Subscribing to Messages
subID := "your-subscription"
err := client.Subscribe(ctx, subID, func(ctx context.Context, msg *pubsub.Message) {
    fmt.Printf("Received message: %s\n", string(msg.Data))
    msg.Ack()
})
if err != nil {
    // handle error
}

Testing

This library includes helpers for testing your application code that uses Pub/Sub.

You can use pubsub.NewClient with option.WithGRPCConn(conn) to connect to a fake Pub/Sub server (like pstest).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyProjectID = errors.New("project ID is required")
	ErrTopicNotFound  = errors.New("topic not found")
	ErrSubNotFound    = errors.New("subscription not found")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	*pubsub.Client
	ProjectID string
}

Client wraps the Google Cloud Pub/Sub client

func NewClient

func NewClient(ctx context.Context, projectID string, opts ...option.ClientOption) (*Client, error)

NewClient creates a new Pub/Sub client

func (*Client) Close

func (c *Client) Close() error

Close closes the Pub/Sub client

func (*Client) Publish

func (c *Client) Publish(ctx context.Context, topicID string, data []byte) (string, error)

Publish publishes a message to the specified topic

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, subID string, handler func(context.Context, *pubsub.Message)) error

Subscribe subscribes to the specified subscription

type Publisher

type Publisher interface {
	Publish(ctx context.Context, topicID string, data []byte) (string, error)
}

Publisher defines the interface for publishing messages

type Subscriber

type Subscriber interface {
	Subscribe(ctx context.Context, subID string, handler func(context.Context, *pubsub.Message)) error
}

Subscriber defines the interface for subscribing to messages

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL