|
| 1 | +.. _pubsub: |
| 2 | + |
| 3 | +Publish-Subscribe integration (Pub/Sub) |
| 4 | +======================================= |
| 5 | + |
| 6 | +pygeoapi supports Publish-Subscribe (Pub/Sub) integration by implementing |
| 7 | +the `OGC API Publish-Subscribe Workflow - Part 1: Core`_ (draft) specification. |
| 8 | + |
| 9 | +Pub/Sub integration can be enabled by defining a broker that pycsw can use to |
| 10 | +publish notifications on given topics using CloudEvents (as per the specification). |
| 11 | + |
| 12 | +When enabled, core functionality of Pub/Sub includes: |
| 13 | + |
| 14 | +- providing an AsyncAPI document (JSON and HTML) |
| 15 | +- providing the following links on the OGC API landing page: |
| 16 | + |
| 17 | + - the broker link (``rel=hub`` link relation) |
| 18 | + - the AsyncAPI JSON link (``rel=service-desc`` link relation and ``type=application/asyncapi+json`` media type) |
| 19 | + - the AsyncAPI HTML link (``rel=service-doc`` link relation and ``type=text/html`` media type) |
| 20 | + |
| 21 | +- sending a notification message on the following events: |
| 22 | + |
| 23 | + - feature or record transactions (create, replace, update, delete) |
| 24 | + - process executions/job creation |
| 25 | + |
| 26 | +AsyncAPI |
| 27 | +-------- |
| 28 | + |
| 29 | +`AsyncAPI`_ is the event-driven equivalent to :ref:`openapi` |
| 30 | + |
| 31 | +The official AsyncAPI specification can be found on the `AsyncAPI`_ website. pygeoapi supports AsyncAPI version 3.0.0. |
| 32 | + |
| 33 | +AsyncAPI is an optional capability in pygeoapi. To enable AsyncAPI, the following steps are required: |
| 34 | + |
| 35 | +- defining a ``pubsub`` section in configuration (see :ref:`configuration` and :ref:`brokers` for more information) |
| 36 | +- generating an AsyncAPI document |
| 37 | +- setting the ``PYGEOAPI_ASYNCAPI`` environment variable |
| 38 | + |
| 39 | +Creating the AsyncAPI document |
| 40 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 41 | + |
| 42 | +The AsyncAPI document is a YAML or JSON configuration which is generated from the pygeoapi configuration, and describes the server information, channels and the message payloads structures. |
| 43 | + |
| 44 | +To generate the AsyncAPI document, run the following: |
| 45 | + |
| 46 | +.. code-block:: bash |
| 47 | +
|
| 48 | + pygeoapi asyncapi generate /path/to/my-pygeoapi-config.yml |
| 49 | +
|
| 50 | +This will dump the AsyncAPI document as YAML to your system's ``stdout``. To save to a file on disk, run: |
| 51 | + |
| 52 | +.. code-block:: bash |
| 53 | +
|
| 54 | + pygeoapi asyncapi generate /path/to/my-pygeoapi-config.yml --output-file /path/to/my-pygeoapi-asyncapi.yml |
| 55 | +
|
| 56 | +To generate the AsyncAPI document as JSON, run: |
| 57 | + |
| 58 | +.. code-block:: bash |
| 59 | +
|
| 60 | + pygeoapi asyncapi generate /path/to/my-pygeoapi-config.yml --format json --output-file /path/to/my-pygeoapi-asyncapi.json |
| 61 | +
|
| 62 | +.. note:: |
| 63 | + Generate as YAML or JSON? If your AsyncAPI YAML definition is slow to render as JSON, |
| 64 | + saving as JSON to disk will help with performance at run-time. |
| 65 | + |
| 66 | +.. note:: |
| 67 | + The AsyncAPI document provides detailed information on query parameters, and dataset |
| 68 | + property names and their data types. Whenever you make changes to your pygeoapi configuration, |
| 69 | + always refresh the accompanying AsyncAPI document. |
| 70 | + |
| 71 | +Validating the AsyncAPI document |
| 72 | +-------------------------------- |
| 73 | + |
| 74 | +To ensure your AsyncAPI document is valid, pygeoapi provides a validation |
| 75 | +utility that can be run as follows: |
| 76 | + |
| 77 | +.. code-block:: bash |
| 78 | +
|
| 79 | + pygeoapi asyncapi validate /path/to/my-pygeoapi-asyncapi.yml |
| 80 | +
|
| 81 | +.. _brokers: |
| 82 | + |
| 83 | +Brokers |
| 84 | +------- |
| 85 | + |
| 86 | +The following protocols are supported: |
| 87 | + |
| 88 | +MQTT |
| 89 | +^^^^ |
| 90 | + |
| 91 | +Example directive: |
| 92 | + |
| 93 | +.. code-block:: yaml |
| 94 | +
|
| 95 | + pubsub: |
| 96 | + name: MQTT |
| 97 | + broker: |
| 98 | + url: mqtt://localhost:1883 |
| 99 | + channel: messages/a/data # optional |
| 100 | + hidden: false # default |
| 101 | +
|
| 102 | +HTTP |
| 103 | +^^^^ |
| 104 | + |
| 105 | +Example directive: |
| 106 | + |
| 107 | +.. code-block:: yaml |
| 108 | +
|
| 109 | + pubsub: |
| 110 | + name: HTTP |
| 111 | + broker: |
| 112 | + url: https://ntfy.sh |
| 113 | + channel: messages-a-data # optional |
| 114 | + hidden: true # default false |
| 115 | +
|
| 116 | +.. note:: |
| 117 | + |
| 118 | + For any Pub/Sub endpoints requiring authentication, encode the ``url`` value as follows: |
| 119 | + |
| 120 | + * ``mqtt://username:password@localhost:1883`` |
| 121 | + * ``https://username:password@localhost`` |
| 122 | + |
| 123 | + As with any section of the pygeoapi configuration, environment variables may be used as needed, for example |
| 124 | + to set username/password information in a URL. If ``pubsub.broker.url`` contains authentication, and |
| 125 | + ``pubsub.broker.hidden`` is ``false``, the authentication information will be stripped from the URL |
| 126 | + before displaying it on the landing page. |
| 127 | + |
| 128 | +.. note:: |
| 129 | + |
| 130 | + If a ``channel`` is defined, it is used as a prefix to the relevant OGC API endpoint used. |
| 131 | + |
| 132 | + If a ``channel`` is not defined, only the relevant OGC API endpoint is used. |
| 133 | + |
| 134 | +.. _`OGC API Publish-Subscribe Workflow - Part 1: Core`: https://docs.ogc.org/DRAFTS/25-030.html |
| 135 | +.. _`AsyncAPI`: https://www.asyncapi.com |
0 commit comments