Skip to content

Commit aef68ec

Browse files
Update README.rst sample. (#3879)
1 parent 886f706 commit aef68ec

2 files changed

Lines changed: 57 additions & 8 deletions

File tree

README.rst

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,67 @@ to Cloud Pub/Sub using this Client Library.
4747

4848
.. _Pub/Sub documentation: http://google-cloud-python.readthedocs.io/en/latest/pubsub/index.html
4949

50-
To get started with this API, you'll need to create
5150

52-
.. code:: python
51+
Publishing
52+
----------
5353

54+
To publish data to Cloud Pub/Sub you must create a topic, and then publish
55+
messages to it
56+
57+
.. code-block:: python
58+
59+
import os
5460
from google.cloud import pubsub
5561
56-
client = pubsub.Client()
57-
topic = client.topic('topic_name')
58-
topic.create()
62+
publisher = pubsub.PublisherClient()
63+
topic = 'projects/{project_id}/topics/{topic}'.format(
64+
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
65+
topic='MY_TOPIC_NAME', # Set this to something appropriate.
66+
)
67+
publisher.create_topic()
68+
publisher.publish(topic, b'My first message!', spam='eggs')
69+
70+
To learn more, consult the :doc:`publishing documentation`_.
71+
72+
.. _publishing documentation: http://google-cloud-python.readthedocs.io/en/latest/pubsub/publisher/index.html
73+
74+
75+
Subscribing
76+
-----------
77+
78+
To subscribe to data in Cloud Pub/Sub, you create a subscription based on
79+
the topic, and subscribe to that.
80+
81+
.. code-block:: python
82+
83+
import os
84+
from google.cloud import pubsub
85+
86+
subscriber = pubsub.SubscriberClient()
87+
topic = 'projects/{project_id}/topics/{topic}'.format(
88+
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
89+
topic='MY_TOPIC_NAME', # Set this to something appropriate.
90+
)
91+
subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format(
92+
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
93+
sub='MY_SUBSCRIPTION_NAME', # Set this to something appropriate.
94+
)
95+
subscription = subscriber.create_subscription(topic, subscription)
96+
97+
The subscription is opened asychronously, and messages are processed by
98+
use of a callback.
99+
100+
.. code-block:: python
101+
102+
def callback(message):
103+
print(message.data)
104+
message.ack()
105+
subscription.open(callback)
106+
107+
To learn more, consult the :doc:`subscriber documentation`_.
108+
109+
.. _subscriber documentation: http://google-cloud-python.readthedocs.io/en/latest/pubsub/subscriber/index.html
59110

60-
topic.publish('this is the message_payload',
61-
attr1='value1', attr2='value2')
62111

63112
.. |pypi| image:: https://img.shields.io/pypi/v/google-cloud-pubsub.svg
64113
:target: https://pypi.org/project/google-cloud-pubsub/

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
setup(
6363
name='google-cloud-pubsub',
64-
version='0.28.1',
64+
version='0.28.2',
6565
description='Python Client for Google Cloud Pub/Sub',
6666
long_description=README,
6767
namespace_packages=[

0 commit comments

Comments
 (0)