Documentation ¶
Overview ¶
Package gcppubsubdemo is a tool for teaching and learning Google Cloud Platform's pub/sub API. This package is not meant to be used as an API but it is documented like one for instructional purposes.
Quick usage examples for the package are shown below. For more information, see the documentation for the individual items.
Publishing ¶
To use the API for publishing, call the GetPublisher function to obtain a Publisher function to call. Call the Publisher function giving the data to publish as the only parameter. To shut down the Publisher, call the Publisher function with nil as the parameter. Failing to do so will leave pub/sub code and network operations initialized.
if publish, err := GetPublisher("GCP Name", "Topic Name"); err != nil { fmt.Fprintln(os.Stderr, err) } else { publish([]byte("Payload")) publish(nil) }
Subscribing ¶
Subscribe to a topic by callng the Subscribe function, giving it a callback to execute when data is received. The callback should return true when it no longer wants to retrieve messages and shut down the subscription.
if err := Subscribe("GCP Name", "Topic Name", "Subscription ID", func(data []byte) bool { fmt.Println(string(data) return true }); err != nil { fmt.Fprintln(os.Stderr, err) }
Logging ¶
Basic logging is implemented using the standard log package. Logging is disabled by default. When enabled with a call to LogLevel(), log output goes to os.Stderr but can be redirected by using LogOutput().
writer, _ := os.Create("logfile.log") LogOutput(writer) LogLevel(DebugLogLevel)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LogLevel ¶
func LogLevel(level LogType)
LogLevel sets the log level. The supported levels are error (1), debug (2), trace (3). A level of 0 disables all logging. A level of 3 or higher enables everything.
Example ¶
// Enable error and debug logging LogLevel(DebugLogLevel)
Output:
func LogOutput ¶
LogOutput sets the logging output to an io.Writer. This allows the API user to direct log output to a file, for example.
Example ¶
writer, _ := os.Create("logfile.log") LogOutput(writer)
Output:
func Subscribe ¶
func Subscribe(gcpName, topicName, subscriptionID string, callback PubsubReceiveCallback) error
Subscribe subscribes and retrieves pub/sub messages from a given Google Cloud Project and topic then calls the callback so the caller can process each message. The callback should return true when it no longer wants to retrieve messages and shut down the subscription.
If an error is encountered during initialization for the subscription, it is returned.
Example ¶
Subscribe("GCP Name", "Topic Name", "Subscription ID", func(data []byte) bool { fmt.Println("Received Data:", string(data)) return true })
Output:
Types ¶
type LogType ¶
type LogType int
LogType represents the type of logging to perform. Current types are DisableLogLeve, ErrorLogLevel, DebugLogLevel, and TraceLogLevel.
type Publisher ¶
Publisher is a function that is returned to caller from GetPublisher() and is used to by the caller to publish data
func GetPublisher ¶
GetPublisher initializes a pub/sub client and topic returning a Publisher function to the caller. The caller can then call the Publisher function with the data to be published. The caller can shut down the Publisher by calling it with a nil parameter. Failing to do so will leave pub/sub code and network operations initialized.
If an error is encountered during initialization, it is returned and the caller should not attempt to use the Publisher function.
Example ¶
publish, _ := GetPublisher("GCP Name", "Topic Name") publish([]byte("Payload"))
Output:
type PubsubReceiveCallback ¶
PubsubReceiveCallback is called with the data returned from the subscriber as a parameter. If the implemented callback returns true the subscription will be deleted and Subscribe() will return.
Example ¶
var callback PubsubReceiveCallback = func(data []byte) bool { fmt.Println("Received Data:", string(data)) return true } callback([]byte("data"))
Output: