subscriber

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package subscribe is a helper that interacts with Pub/Sub Cloud feature. It stores active websockets. When message is received, it notifies the concerned websocket if any.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSubscriber

func NewSubscriber(opt SubscriberOptions) (*subscriberImpl, error)

NewSubscriber builds a new Pub/Sub subscriber. It retrieve the subscription using the project ID and a subscription ID. The websocket store is initialized. The logger is created using the base logger configuration.

Types

type JobBody

type JobBody struct {
	Message  string `json:"message"`
	Progress int    `json:"progress"`
}

JobBody is the format of the job output retrieved from the Pub/Sub message Example: { "message": "job is still in progress", "progress": 35 }

type JobStatus

type JobStatus struct {
	// The job output
	Body JobBody `json:"body"`

	// The code attribute of the Pub/Sub message
	Code int `json:"code"`

	// The status attribute of the Pub/Sub message
	Status string `json:"status"`

	// The ordering key of the Pub/Sub message
	OrderingKey string `json:"ordering_key"`
}

JobStatus holds the jobStatus and the JobBody

type Subscriber

type Subscriber interface {

	// Close uses the cancel callback to stop receiving new messages from the
	// subscription.
	Close()

	// Listen uses sub.Receive to receive new messages from the subscription.
	// When a message is received, the configured callback is called.
	Listen() error

	// NewJobStatus converts a received Pub/Sub message into a jobBody.
	// It reads the message attributes to retrieve a code, a status and the ordering
	// key. It also parses the message data as JSON into a jobStatus entry.
	NewJobStatus(pMsg *pubsub.Message) (*JobStatus, error)
}

Subscriber interacts with Pub/Sub through a subscription. It holds the receive callback context and its cancel callback. It also has its own logger.

type SubscriberOptions

type SubscriberOptions struct {
	OnReceive      func(ctx context.Context, message *pubsub.Message)
	ProjectID      string
	SubscriptionID string
	LoggerBase     *log.Logger
}

SubscriberOptions holds the configuration to build a new subscriber

Jump to

Keyboard shortcuts

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