This directory contains some simple Go examples that can be used with Pub/Sub API. The examples show the general flow for each of the remote procedure calls (RPCs) supported by Pub/Sub API. Note that the examples are provided as a learning resource to demonstrate how to use the RPC methods in the Go language. They weren't performance tested and aren't production ready. See the Limitations
section below for more details.
common/
- This directory contains a set of common variables that are shared between all examples. Before running any of the examples, manually update these variables to ensure you're passing the correct username, password, etc. See theRunning the Examples
section below for instructions on how to prepare this file.examples/
- This directory contains standalone examples for each RPC method defined in the proto file. When running an example you will use one of themain.go
files nested in this directory as your entry point. In general, each example creates a gRPC client, fetches the necessary authentication token, and then calls the necessary RPC method(s) to demonstrate usage.grpcclient/
- This directory contains a wrapper struct that is used by the examples to interact with the gRPC server. The majority of the logic for these examples lives in this wrapper struct.oauth/
- This directory contains basic helper functions to fetch an OAuth token and user data.proto/
- This directory contains the Go-specific*.pb.go
files that are generated from thepubsub_api.proto
file found at the root of this project. The*.pb.go
files contain helper functions and structs that will be used by the Go examples to interact with Pub/Sub API.
- Install Go.
- Clone this project.
- Run
go mod vendor
from thego
directory to fetch dependencies.- NOTE: At this time the
vendor
directory has not been committed to this repo so you need to manually run thego mod vendor
command to fetch dependencies. If thevendor
directory is committed to the repo in the future, this step can be skipped.
- NOTE: At this time the
- Use a Salesforce org. Get the username, password, and login URL.
- Create a custom CarMaintenance platform event in your Salesforce org. Ensure your CarMaintenance platform event matches the following structure:
- Standard Fields
- Label: CarMaintenance
- Plural Label: CarMaintenances
- Custom Fields
- Cost (Number)
- Mileage (Number)
- WorkDescription (Text, 200)
- Create a connected app and enable OAuth in your Salesforce org. See Quick Start Prerequisites in the REST API Developer Guide. Save the consumer key and consumer secret.
- Update the relevant variables in the
common/common.go
file. These configs will apply to all examples.<CLIENT_ID>
- Fill this value in with the consumer key you received after setting up OAuth for your Salesforce org.<CLIENT_SECRET>
- Fill this value in with the consumer secret you received after setting up OAuth for your Salesforce org.<ORG_USERNAME>
- Fill this value in with the username for your Salesforce org.<ORG_PASSWORD>
- Fill this value in with the password for your Salesforce org.<ORG_LOGIN_URL>
- Fill this value in with the login URL for your Salesforce org.
- Choose an example to run from the
examples
directory. Each subdirectory is named after the RPC call flow it demonstrates and you can run the corresponding example with ago run
command. For example, if you want to run the example for the GetTopic RPC, rungo run examples/topic/main.go
from thego
directory.
Pub/Sub API allows clients using the Subscribe
RPC to specify one of the following subscription replay options:
LATEST
CUSTOM
EARLIEST
For a description of these replay options, refer to the "Replaying an Event Stream" section in the Pub/Sub API docs.
The subscribe example in this repo is compatible with all three options outlined above. To set a subscription option, update the ReplayPreset
and ReplayId
options in the common.go
file. See the following examples:
LATEST
- set
ReplayPreset = proto.ReplayPreset_LATEST
- set
ReplayId []byte = nil
- set
CUSTOM
- set
ReplayPreset = proto.ReplayPreset_CUSTOM
- set
ReplayId = []byte{0, 0, 0, 0, 0, 37, 132, 136}
- Note that this is just an example ReplayId intended to show the appropriate formatting. You need to specify a valid ReplayId in the same format demonstrated here.
- set
EARLIEST
- set
ReplayPreset = proto.ReplayPreset_EARLIEST
- set
ReplayId []byte = nil
- set
- Install protoc, protoc-gen-go, and protoc-gen-go-grpc and ensure the binaries are available in your PATH. See the gRPC quick start docs for more info.
- Fetch the most current proto file and copy it to your own project.
- Modify the
option go_package
line to match your desired import path. - Generate the Go files from the proto file with the
protoc
command. As an example, if you copied the proto file toproto/pubsub_api.proto
then you can generate the*.pb.go
files by running the following command from the root of your project:
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
proto/pubsub_api.proto
This repo can be used as a reference point for clients looking to create a Go app to integrate with Pub/Sub API. Note that the project structure and the examples included in this repo are intended for demo purposes; clients are free to implement their own Go apps in any way they see fit. A client may want to consider whether or not the error handling, retries, etc. included in these examples will be appropriate for their specific use case.
- No support for auth token refreshes - At some point the authentication token will expire. At this time, these examples do not handle re-authentication.
- No guarantees that streams will remain open - Pub/Sub API has idle timeouts and will close idle streams. If a stream is closed while running these examples, you will most likely need to stop and restart.
- NOTE: This point primarily applies to the
Subscribe
andPublishStream
RPC examples as these RPCs rely on long-lived streams.
- NOTE: This point primarily applies to the
- No support for republishing on error - If an error occurs while publishing the relevant examples will surface the error but will not attempt to republish the event.
- No security guarantees - Teams using these examples for reference will need to do their own security audits to ensure the dependencies introduced here can be safely used.
- No performance testing - These examples have not been perf tested.
- No timeouts for streaming calls - The
Subscribe
andPublishStream
RPC examples do not have timeouts configured.