PubSub class abstract

A Cloud Pub/Sub client.

Connects to the Cloud Pub/Sub service and gives access to its operations.

Google Cloud Pub/Sub is a reliable, many-to-many, asynchronous messaging service from Google Cloud Platform. A detailed overview is available on Pub/Sub docs.

To access Pub/Sub, an authenticate HTTP client is required. This client should as a minimum provide access to the scopes PubSub.Scopes.

The following example shows how to access Pub/Sub using a service account and pull a message from a subscription.

import 'package:http/http.dart' as http;
import 'package:googleapis_auth/auth_io.dart' as auth;
import 'package:gcloud/pubsub.dart';

Future<http.Client> createClient() {
  // Service account credentials retrieved from Cloud Console.
  String creds =
      r'''
      {
        "private_key_id": ...,
        "private_key": ...,
        "client_email": ...,
        "client_id": ...,
        "type": "service_account"
      }''';
  return auth.clientViaServiceAccount(
      new auth.ServiceAccountCredentials.fromJson(creds),
      PubSub.Scopes);
}

main() {
  var project = 'my-project';
  var client;
  var pubsub;
  createClient().then((c) {
    client = c;
    pubsub = new PubSub(client, project);
    return pubsub.lookupSubscription('my-subscription');
  })
  .then((Subscription subscription) => subscription.pull())
  .then((PullEvent event) => print('Message ${event.message.asString}'))
  .whenComplete(() => client.close());
}

When working with topics and subscriptions they are referred to using names. These names can be either relative names or absolute names.

An absolute name of a topic starts with projects/ and has the form:

projects/<project-id>/topics/<relative-name>

When a relative topic name is used, its absolute name is generated by pre-pending projects/<project-id>/topics/, where <project-id> is the project id passed to the constructor.

An absolute name of a subscription starts with projects/ and has the form:

projects/<project-id>/subscriptions/<relative-name>

When a relative subscription name is used, its absolute name is generated by pre-pending projects/<project-id>/subscriptions/, where <project-id> is the project id passed to the constructor.

Constructors

PubSub(Client client, String project)
Access Pub/Sub using an authenticated client.
factory

Properties

hashCode int
The hash code for this object.
no setterinherited
project String
The name of the project.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

createSubscription(String name, String topic, {Uri endpoint}) Future<Subscription>
Create a new subscription named name listening on topic topic.
createTopic(String name) Future<Topic>
Create a new topic named name.
deleteSubscription(String name) Future
Delete subscription named name.
deleteTopic(String name) Future
Delete topic named name.
listSubscriptions([String query]) Stream<Subscription>
List subscriptions.
listTopics() Stream<Topic>
Lists all topics.
lookupSubscription(String name) Future<Subscription>
Lookup subscription with named name.
lookupTopic(String name) Future<Topic>
Look up topic named name.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pageSubscriptions({String topic, int pageSize = 50}) Future<Page<Subscription>>
Start paging through subscriptions.
pageTopics({int pageSize = 50}) Future<Page<Topic>>
Start paging through all topics.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Constants

SCOPES → const List<String>
List of required OAuth2 scopes for Pub/Sub operation.