View Source Google.Pubsub

Hex.pm Version Hexdocs.pm Hex.pm Download Total MIT

Elixir Library for interacting with Google Pubsub over GRPC, inspired by Weddell

installation

Installation

The package can be installed by adding google_grpc_pubsub to your list of dependencies in mix.exs:

def deps do
  [
    {:google_grpc_pubsub, "~> 0.4"}
  ]
end

configuration

Configuration

google_grpc_pubsub uses Goth to authenticate with Google's APIs.

You must configure Goth in your supervision tree, and then configure this library

config :google_grpc_pubsub,
  goth: MyApp.Goth

If you want to use this library against the Pubsub emulator

config :google_grpc_pubsub,
  emulator: {"localhost", 8085}

By default there can be 10 concurrent calls to the rpc channel, if you would like to increase this

config :google_grpc_pubsub,
  pool_size: 50

getting-started

Getting Started

creating-a-topic

Creating a topic

{:ok, topic} = Google.Pubsub.Topic.create(project: "my-project", topic: "my-topic")

create-a-subscription

Create a subscription

{:ok, subscription} = Google.Pubsub.Subscription.create(project: "my-project", subscription: "my-subscription", topic: "my-topic")

publishing-a-message

Publishing a message

{:ok, topic} = Google.Pubsub.Topic.get(project: "my-project", topic: "my-topic")

# Publish some string data
Google.Pubsub.Topic.publish(topic, Google.Pubsub.Message.new("my string data"))

# Or you can publish a map, which will be encoded to JSON
Google.Pubsub.Topic.publish(topic, Google.Pubsub.Message.new(%{some: "json data"}))


# You can also publish multiple messages at once
Google.Pubsub.Topic.publish(topic, [
  Google.Pubsub.Message.new(%{some: "json data"}),
  Google.Pubsub.Message.new(%{another: "message"})
])

pulling-messages

Pulling Messages

{:ok, subscription} = Google.Pubsub.Subscription.get(project: "project", subscription: "subscription")

{:ok, messages} = Google.Pubsub.Subscription.pull(subscription, max_messages: 5)

acknowledging-messages

Acknowledging Messages

{:ok, subscription} = Google.Pubsub.Subscription.get(project: "project", subscription: "subscription")

{:ok, messages} = Google.Pubsub.Subscription.pull(subscription, max_messages: 5)

...


Google.Pubsub.Subscription.acknowledge(subscription, messages)