google.pubsub
#
Description#
Publishes and subscribes to Google Pub/Sub topics.
You’ll need a Google Cloud active project and a set of credentials to use this plugin:
Create a project on the Google Cloud console if you don’t have one already.
In the Google Cloud API console create a new service account key. Select “New Service Account”, choose the role “Pub/Sub Editor” and leave the key type as JSON.
Download the JSON service credentials file. By default Platypush will look for the credentials file under
<WORKDIR>/credentials/google/pubsub.json
.
Configuration#
google.pubsub:
# [Optional]
# Path to the JSON credentials file for Google
# pub/sub (default: ``~/.credentials/platypush/google/pubsub.json``)
# credentials_file: /var/lib/platypush/credentials/google/pubsub.json # type=str
# [Optional]
# List of topics to subscribe. You can either specify the
# full topic name in the format
# ``projects/<project_id>/topics/<topic_name>``, where
# ``<project_id>`` must be the ID of your Google Pub/Sub project, or
# just ``<topic_name>`` - in such case it's implied that you refer to
# the ``topic_name`` under the ``project_id`` of your service
# credentials.
# topics: () # type=Iterable[str]
# [Optional]
# How often the `RunnablePlugin.loop <https://docs.platypush.tech/platypush/plugins/.html#platypush.plugins.RunnablePlugin.loop>`_ function should be
# executed (default: 15 seconds). *NOTE*: For back-compatibility
# reasons, the `poll_seconds` argument is also supported, but it's
# deprecated.
# poll_interval: 15 # type=Optional[float]
# [Optional]
# How long we should wait for any running
# threads/processes to stop before exiting (default: 5 seconds).
# stop_timeout: 5 # type=Optional[float]
# [Optional]
# If set to True then the plugin will not monitor
# for new events. This is useful if you want to run a plugin in
# stateless mode and only leverage its actions, without triggering any
# events. Defaults to False.
# disable_monitor: False # type=bool
Dependencies#
pip
pip install oauth2client httplib2 google-auth google-api-python-client google-cloud-pubsub
Alpine
apk add py3-google-auth py3-google-api-python-client py3-oauth2client py3-httplib2
Debian
apt install python3-google-auth python3-oauth2client python3-httplib2
Fedora
yum install python-google-api-client python-oauth2client python-httplib2 python-google-auth
Arch Linux
pacman -S python-google-api-python-client python-oauth2client python-httplib2 python-google-auth
Triggered events#
Actions#
Module reference#
- class platypush.plugins.google.pubsub.GooglePubsubPlugin(credentials_file: str = '/var/lib/platypush/credentials/google/pubsub.json', topics: Iterable[str] = (), **kwargs)[source]#
Bases:
RunnablePlugin
Publishes and subscribes to Google Pub/Sub topics.
You’ll need a Google Cloud active project and a set of credentials to use this plugin:
Create a project on the Google Cloud console if you don’t have one already.
In the Google Cloud API console create a new service account key. Select “New Service Account”, choose the role “Pub/Sub Editor” and leave the key type as JSON.
Download the JSON service credentials file. By default Platypush will look for the credentials file under
<WORKDIR>/credentials/google/pubsub.json
.
- __init__(credentials_file: str = '/var/lib/platypush/credentials/google/pubsub.json', topics: Iterable[str] = (), **kwargs)[source]#
- Parameters:
credentials_file – Path to the JSON credentials file for Google pub/sub (default:
~/.credentials/platypush/google/pubsub.json
)topics – List of topics to subscribe. You can either specify the full topic name in the format
projects/<project_id>/topics/<topic_name>
, where<project_id>
must be the ID of your Google Pub/Sub project, or just<topic_name>
- in such case it’s implied that you refer to thetopic_name
under theproject_id
of your service credentials.
- publish(topic: str, msg, **kwargs)[source]#
Publish a message to a topic
- Parameters:
topic – Topic/channel where the message will be delivered. You can either specify the full topic name in the format
projects/<project_id>/topics/<topic_name>
, where<project_id>
must be the ID of your Google Pub/Sub project, or just<topic_name>
- in such case it’s implied that you refer to thetopic_name
under theproject_id
of your service credentials.msg – Message to be sent. It can be a list, a dict, or a Message object
kwargs – Extra arguments to be passed to .publish()
- start()#
Start the plugin.
- subscribe(topic: str)[source]#
Subscribe to a topic.
- Parameters:
topic – Topic/channel where the message will be delivered. You can either specify the full topic name in the format
projects/<project_id>/topics/<topic_name>
, where<project_id>
must be the ID of your Google Pub/Sub project, or just<topic_name>
- in such case it’s implied that you refer to thetopic_name
under theproject_id
of your service credentials.
- unsubscribe(topic: str)[source]#
Unsubscribe from a topic.
- Parameters:
topic – Topic/channel where the message will be delivered. You can either specify the full topic name in the format
projects/<project_id>/topics/<topic_name>
, where<project_id>
must be the ID of your Google Pub/Sub project, or just<topic_name>
- in such case it’s implied that you refer to thetopic_name
under theproject_id
of your service credentials.
- wait_stop(timeout=None)#
Wait until a stop event is received.