Skip to content

izumin5210/pubee

Repository files navigation

pubee

CircleCI GoDoc GitHub release codecov Go Report Card GitHub

Example

Google Cloud Pub/Sub

ctx := context.Background()

// Initiailze a driver for Google Cloud Pub/Sub
driver, err := cloudpubsub.CreateDriver(
	ctx, "my-gcp-project", "your-topic",
	cloudpubsub.WithCreateTopicIfNeeded(),  // Create a topic when it does not exist
	cloudpubsub.WithDeleteTopicOnClose(),   // Delete the topic on close the publisher
)
if err != nil {
	// ...
}

// Initialize a new publisher instance
publisher := pubee.New(driver,
	pubee.WithJSON(),  // publish messages as JSON
	pubee.WithMetadata("content_type", "json"),
	pubee.WithInterceptors(
		// ...
	),
	pubee.WithOnFailPublish(func(msg *pubee.Message, err error) {
		// This function is called when failed to publish a message.
	}),
)
defer publisher.Close(ctx)

type Book struct {
	Title  string `json:"title"`
}

// Publish a message!
publisher.Publish(ctx, &Book{Title: "The Go Programming Language"})