annotate vendor/zendframework/zend-feed/doc/book/pubsubhubbub.md @ 7:848c88cfe644

More layout
author Chris Cannam
date Fri, 05 Jan 2018 13:59:44 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 # Zend\\Feed\\PubSubHubbub
Chris@0 2
Chris@0 3 `Zend\Feed\PubSubHubbub` is an implementation of the [PubSubHubbub Core 0.2/0.3
Chris@0 4 Specification (Working Draft)](http://pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.3.html).
Chris@0 5 It offers implementations of a Pubsubhubbub Publisher and Subscriber suited to
Chris@0 6 PHP applications.
Chris@0 7
Chris@0 8 ## What is PubSubHubbub?
Chris@0 9
Chris@0 10 Pubsubhubbub is an open, simple, web-scale, pubsub protocol. A common use case
Chris@0 11 to enable blogs (Publishers) to "push" updates from their RSS or Atom feeds
Chris@0 12 (Topics) to end Subscribers. These Subscribers will have subscribed to the
Chris@0 13 blog's RSS or Atom feed via a Hub, a central server which is notified of any
Chris@0 14 updates by the Publisher, and which then distributes these updates to all
Chris@0 15 Subscribers. Any feed may advertise that it supports one or more Hubs using an
Chris@0 16 Atom namespaced link element with a rel attribute of "hub" (i.e., `rel="hub"`).
Chris@0 17
Chris@0 18 Pubsubhubbub has garnered attention because it is a pubsub protocol which is
Chris@0 19 easy to implement and which operates over HTTP. Its philosophy is to replace the
Chris@0 20 traditional model where blog feeds have been polled at regular intervals to
Chris@0 21 detect and retrieve updates. Depending on the frequency of polling, this can
Chris@0 22 take a lot of time to propagate updates to interested parties from planet
Chris@0 23 aggregators to desktop readers. With a pubsub system in place, updates are not
Chris@0 24 simply polled by Subscribers, they are pushed to Subscribers, eliminating any
Chris@0 25 delay. For this reason, Pubsubhubbub forms part of what has been dubbed the
Chris@0 26 real-time web.
Chris@0 27
Chris@0 28 The protocol does not exist in isolation. Pubsub systems have been around for a
Chris@0 29 while, such as the familiar Jabber Publish-Subscribe protocol,
Chris@0 30 [XEP-0060](http://www.xmpp.org/extensions/xep-0060.html), or the less well-known
Chris@0 31 [rssCloud](http://www.rssboard.org/rsscloud-interface) (described in 2001).
Chris@0 32 However, these have not achieved widespread adoption due to either their
Chris@0 33 complexity, poor timing, or lack of suitability for web applications. rssCloud,
Chris@0 34 which was recently revived as a response to the appearance of Pubsubhubbub, has
Chris@0 35 also seen its usage increase significantly, though it lacks a formal
Chris@0 36 specification and currently does not support Atom 1.0 feeds.
Chris@0 37
Chris@0 38 Perhaps surprisingly given its relative early age, Pubsubhubbub is already in
Chris@0 39 use including in Google Reader and Feedburner, and there are plugins available
Chris@0 40 for Wordpress blogs.
Chris@0 41
Chris@0 42 ## Architecture
Chris@0 43
Chris@0 44 `Zend\Feed\PubSubHubbub` implements two sides of the Pubsubhubbub 0.2/0.3
Chris@0 45 Specification: a Publisher and a Subscriber. It does not currently implement a
Chris@0 46 Hub Server.
Chris@0 47
Chris@0 48 A Publisher is responsible for notifying all supported Hubs (many can be
Chris@0 49 supported to add redundancy to the system) of any updates to its feeds, whether
Chris@0 50 they be Atom or RSS based. This is achieved by pinging the supported Hub Servers
Chris@0 51 with the URL of the updated feed. In Pubsubhubbub terminology, any updatable
Chris@0 52 resource capable of being subscribed to is referred to as a Topic. Once a ping
Chris@0 53 is received, the Hub will request the updated feed, process it for updated
Chris@0 54 items, and forward all updates to all Subscribers subscribed to that feed.
Chris@0 55
Chris@0 56 A Subscriber is any party or application which subscribes to one or more Hubs to
Chris@0 57 receive updates from a Topic hosted by a Publisher. The Subscriber never
Chris@0 58 directly communicates with the Publisher since the Hub acts as an intermediary,
Chris@0 59 accepting subscriptions and sending updates to Subscribers. The Subscriber
Chris@0 60 therefore communicates only with the Hub, either to subscribe or unsubscribe to
Chris@0 61 Topics, or when it receives updates from the Hub. This communication design
Chris@0 62 ("Fat Pings") effectively removes the possibility of a "Thundering Herd" issue.
Chris@0 63 (Thundering Herds occur in a pubsub system where the Hub merely informs
Chris@0 64 Subscribers that an update is available, prompting all Subscribers to
Chris@0 65 immediately retrieve the feed from the Publisher, giving rise to a traffic
Chris@0 66 spike.) In Pubsubhubbub, the Hub distributes the actual update in a "Fat Ping"
Chris@0 67 so the Publisher is not subjected to any traffic spike.
Chris@0 68
Chris@0 69 `Zend\Feed\PubSubHubbub` implements Pubsubhubbub Publishers and Subscribers with
Chris@0 70 the classes `Zend\Feed\PubSubHubbub\Publisher` and
Chris@0 71 `Zend\Feed\PubSubHubbub\Subscriber`. In addition, the Subscriber implementation
Chris@0 72 may handle any feed updates forwarded from a Hub by using
Chris@0 73 `Zend\Feed\PubSubHubbub\Subscriber\Callback`. These classes, their use cases,
Chris@0 74 and etheir APIs are covered in subsequent sections.
Chris@0 75
Chris@0 76 ## Zend\\Feed\\PubSubHubbub\\Publisher
Chris@0 77
Chris@0 78 In Pubsubhubbub, the Publisher is the party publishing a live feed with content
Chris@0 79 updates. This may be a blog, an aggregator, or even a web service with a public
Chris@0 80 feed based API. In order for these updates to be pushed to Subscribers, the
Chris@0 81 Publisher must notify all of its supported Hubs that an update has occurred
Chris@0 82 using a simple HTTP POST request containing the URI of the updated Topic (i.e.,
Chris@0 83 the updated RSS or Atom feed). The Hub will confirm receipt of the notification,
Chris@0 84 fetch the updated feed, and forward any updates to any Subscribers who have
Chris@0 85 subscribed to that Hub for updates from the relevant feed.
Chris@0 86
Chris@0 87 By design, this means the Publisher has very little to do except send these Hub
Chris@0 88 pings whenever its feeds change. As a result, the Publisher implementation is
Chris@0 89 extremely simple to use and requires very little work to setup and use when
Chris@0 90 feeds are updated.
Chris@0 91
Chris@0 92 `Zend\Feed\PubSubHubbub\Publisher` implements a full Pubsubhubbub Publisher. Its
Chris@0 93 setup for use primarily requires that it is configured with the URI endpoint for
Chris@0 94 all Hubs to be notified of updates, and the URIs of all Topics to be included in
Chris@0 95 the notifications.
Chris@0 96
Chris@0 97 The following example shows a Publisher notifying a collection of Hubs about
Chris@0 98 updates to a pair of local RSS and Atom feeds. The class retains a collection of
Chris@0 99 errors which include the Hub URLs, so that notification can be attempted again
Chris@0 100 later and/or logged if any notifications happen to fail. Each resulting error
Chris@0 101 array also includes a "response" key containing the related HTTP response
Chris@0 102 object. In the event of any errors, it is strongly recommended to attempt the
Chris@0 103 operation for failed Hub Endpoints at least once more at a future time. This may
Chris@0 104 require the use of either a scheduled task for this purpose or a job queue,
Chris@0 105 though such extra steps are optional.
Chris@0 106
Chris@0 107 ```php
Chris@0 108 use Zend\Feed\PubSubHubbub\Publisher;
Chris@0 109
Chris@0 110 $publisher = Publisher;
Chris@0 111 $publisher->addHubUrls([
Chris@0 112 'http://pubsubhubbub.appspot.com/',
Chris@0 113 'http://hubbub.example.com',
Chris@0 114 ]);
Chris@0 115 $publisher->addUpdatedTopicUrls([
Chris@0 116 'http://www.example.net/rss',
Chris@0 117 'http://www.example.net/atom',
Chris@0 118 ]);
Chris@0 119 $publisher->notifyAll();
Chris@0 120
Chris@0 121 if (! $publisher->isSuccess()) {
Chris@0 122 // check for errors
Chris@0 123 $errors = $publisher->getErrors();
Chris@0 124 $failedHubs = [];
Chris@0 125 foreach ($errors as $error) {
Chris@0 126 $failedHubs[] = $error['hubUrl'];
Chris@0 127 }
Chris@0 128 }
Chris@0 129
Chris@0 130 // reschedule notifications for the failed Hubs in $failedHubs
Chris@0 131 ```
Chris@0 132
Chris@0 133 If you prefer having more concrete control over the Publisher, the methods
Chris@0 134 `addHubUrls()` and `addUpdatedTopicUrls()` pass each array value to the singular
Chris@0 135 `addHubUrl()` and `addUpdatedTopicUrl()` public methods. There are also matching
Chris@0 136 `removeUpdatedTopicUrl()` and `removeHubUrl()` methods.
Chris@0 137
Chris@0 138 You can also skip setting Hub URIs, and notify each in turn using the
Chris@0 139 `notifyHub()` method which accepts the URI of a Hub endpoint as its only
Chris@0 140 argument.
Chris@0 141
Chris@0 142 There are no other tasks to cover. The Publisher implementation is very simple
Chris@0 143 since most of the feed processing and distribution is handled by the selected
Chris@0 144 Hubs. It is, however, important to detect errors and reschedule notifications as
Chris@0 145 soon as possible (with a reasonable maximum number of retries) to ensure
Chris@0 146 notifications reach all Subscribers. In many cases, as a final alternative, Hubs
Chris@0 147 may frequently poll your feeds to offer some additional tolerance for failures
Chris@0 148 both in terms of their own temporary downtime or Publisher errors or downtime.
Chris@0 149
Chris@0 150 ## Zend\\Feed\\PubSubHubbub\\Subscriber
Chris@0 151
Chris@0 152 In Pubsubhubbub, the Subscriber is the party who wishes to receive updates to
Chris@0 153 any Topic (RSS or Atom feed). They achieve this by subscribing to one or more of
Chris@0 154 the Hubs advertised by that Topic, usually as a set of one or more Atom 1.0
Chris@0 155 links with a rel attribute of "hub" (i.e., `rel="hub"`). The Hub from that point
Chris@0 156 forward will send an Atom or RSS feed containing all updates to that
Chris@0 157 Subscriber's callback URL when it receives an update notification from the
Chris@0 158 Publisher. In this way, the Subscriber need never actually visit the original
Chris@0 159 feed (though it's still recommended at some level to ensure updates are
Chris@0 160 retrieved if ever a Hub goes offline). All subscription requests must contain
Chris@0 161 the URI of the Topic being subscribed and a callback URL which the Hub will use
Chris@0 162 to confirm the subscription and to forward updates.
Chris@0 163
Chris@0 164 The Subscriber therefore has two roles. The first is to *create* and *manage*
Chris@0 165 subscriptions, including subscribing for new Topics with a Hub, unsubscribing
Chris@0 166 (if necessary), and periodically renewing subscriptions, since they may have an
Chris@0 167 expiry set by the Hub. This is handled by `Zend\Feed\PubSubHubbub\Subscriber`.
Chris@0 168
Chris@0 169 The second role is to *accept updates* sent by a Hub to the Subscriber's
Chris@0 170 callback URL, i.e. the URI the Subscriber has assigned to handle updates. The
Chris@0 171 callback URL also handles events where the Hub contacts the Subscriber to
Chris@0 172 confirm all subscriptions and unsubscriptions. This is handled by using an
Chris@0 173 instance of `Zend\Feed\PubSubHubbub\Subscriber\Callback` when the callback URL
Chris@0 174 is accessed.
Chris@0 175
Chris@0 176 > ### Query strings in callback URLs
Chris@0 177 >
Chris@0 178 > `Zend\Feed\PubSubHubbub\Subscriber` implements the Pubsubhubbub 0.2/0.3
Chris@0 179 > specification. As this is a new specification version, not all Hubs currently
Chris@0 180 > implement it. The new specification allows the callback URL to include a query
Chris@0 181 > string which is used by this class, but not supported by all Hubs. In the
Chris@0 182 > interests of maximising compatibility, it is therefore recommended that the
Chris@0 183 > query string component of the Subscriber callback URI be presented as a path
Chris@0 184 > element, i.e. recognised as a parameter in the route associated with the
Chris@0 185 > callback URI and used by the application's router.
Chris@0 186
Chris@0 187 ### Subscribing and Unsubscribing
Chris@0 188
Chris@0 189 `Zend\Feed\PubSubHubbub\Subscriber` implements a full Pubsubhubbub Subscriber
Chris@0 190 capable of subscribing to, or unsubscribing from, any Topic via any Hub
Chris@0 191 advertised by that Topic. It operates in conjunction with
Chris@0 192 `Zend\Feed\PubSubHubbub\Subscriber\Callback`, which accepts requests from a Hub
Chris@0 193 to confirm all subscription or unsubscription attempts (to prevent third-party
Chris@0 194 misuse).
Chris@0 195
Chris@0 196 Any subscription (or unsubscription) requires the relevant information before
Chris@0 197 proceeding, i.e. the URI of the Topic (Atom or RSS feed) to be subscribed to for
Chris@0 198 updates, and the URI of the endpoint for the Hub which will handle the
Chris@0 199 subscription and forwarding of the updates. The lifetime of a subscription may
Chris@0 200 be determined by the Hub, but most Hubs should support automatic subscription
Chris@0 201 refreshes by checking with the Subscriber. This is supported by
Chris@0 202 `Zend\Feed\PubSubHubbub\Subscriber\Callback` and requires no other work on your
Chris@0 203 part. It is still strongly recommended that you use the Hub-sourced subscription
Chris@0 204 time-to.live (ttl) to schedule the creation of new subscriptions (the process is
Chris@0 205 identical to that for any new subscription) to refresh it with the Hub. While it
Chris@0 206 should not be necessary per se, it covers cases where a Hub may not support
Chris@0 207 automatic subscription refreshing, and rules out Hub errors for additional
Chris@0 208 redundancy.
Chris@0 209
Chris@0 210 With the relevant information to hand, a subscription can be attempted as
Chris@0 211 demonstrated below:
Chris@0 212
Chris@0 213 ```php
Chris@0 214 use Zend\Feed\PubSubHubbub\Model\Subscription;
Chris@0 215 use Zend\Feed\PubSubHubbub\Subscriber;
Chris@0 216
Chris@0 217 $storage = new Subscription;
Chris@0 218 $subscriber = new Subscriber;
Chris@0 219 $subscriber->setStorage($storage);
Chris@0 220 $subscriber->addHubUrl('http://hubbub.example.com');
Chris@0 221 $subscriber->setTopicUrl('http://www.example.net/rss.xml');
Chris@0 222 $subscriber->setCallbackUrl('http://www.mydomain.com/hubbub/callback');
Chris@0 223 $subscriber->subscribeAll();
Chris@0 224 ```
Chris@0 225
Chris@0 226 In order to store subscriptions and offer access to this data for general use,
Chris@0 227 the component requires a database (a schema is provided later in this section).
Chris@0 228 By default, it is assumed the table name is "subscription", and it utilises
Chris@0 229 `Zend\Db\TableGateway\TableGateway` in the background, meaning it will use the
Chris@0 230 default adapter you have set for your application. You may also pass a specific
Chris@0 231 custom `Zend\Db\TableGateway\TableGateway` instance into the associated model
Chris@0 232 `Zend\Feed\PubSubHubbub\Model\Subscription`. This custom adapter may be as
Chris@0 233 simple in intent as changing the table name to use or as complex as you deem
Chris@0 234 necessary.
Chris@0 235
Chris@0 236 While this model is offered as a default ready-to-roll solution, you may create
Chris@0 237 your own model using any other backend or database layer (e.g. Doctrine) so long
Chris@0 238 as the resulting class implements the interface
Chris@0 239 `Zend\Feed\PubSubHubbub\Model\SubscriptionInterface`.
Chris@0 240
Chris@0 241 An example schema (MySQL) for a subscription table accessible by the provided
Chris@0 242 model may look similar to:
Chris@0 243
Chris@0 244 ```sql
Chris@0 245 CREATE TABLE IF NOT EXISTS `subscription` (
Chris@0 246 `id` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
Chris@0 247 `topic_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
Chris@0 248 `hub_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
Chris@0 249 `created_time` datetime DEFAULT NULL,
Chris@0 250 `lease_seconds` bigint(20) DEFAULT NULL,
Chris@0 251 `verify_token` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
Chris@0 252 `secret` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
Chris@0 253 `expiration_time` datetime DEFAULT NULL,
Chris@0 254 `subscription_state` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,
Chris@0 255 PRIMARY KEY (`id`)
Chris@0 256 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Chris@0 257 ```
Chris@0 258
Chris@0 259 Behind the scenes, the Subscriber above will send a request to the Hub endpoint
Chris@0 260 containing the following parameters (based on the previous example):
Chris@0 261
Chris@0 262 Parameter | Value | Explanation
Chris@0 263 --------- | ----- | -----------
Chris@0 264 `hub.callback` | `http://www.mydomain.com/hubbub/callback?xhub.subscription=5536df06b5dcb966edab3a4c4d56213c16a8184` | The URI used by a Hub to contact the Subscriber and either request confirmation of a (un)subscription request, or send updates from subscribed feeds. The appended query string contains a custom parameter (hence the xhub designation). It is a query string parameter preserved by the Hub and re-sent with all Subscriber requests. Its purpose is to allow the Subscriber to identify and look up the subscription associated with any Hub request in a backend storage medium. This is a non-standard parameter used by this component in preference to encoding a subscription key in the URI path, which is difficult to enforce generically. Nevertheless, since not all Hubs support query string parameters, we still strongly recommend adding the subscription key as a path component in the form `http://www.mydomain.com/hubbub/callback/5536df06b5dcb966edab3a4c4d56213c16a8184`. This requires defining a route capable of parsing out the final value of the key, retrieving the value, and passing it to the Subscriber callback object. The value should be passed into the method `Zend\PubSubHubbub\Subscriber\Callback::setSubscriptionKey()`. A detailed example is offered later.
Chris@0 265 `hub.lease_seconds` | `2592000` | The number of seconds for which the Subscriber would like a new subscription to remain valid (i.e. a TTL). Hubs may enforce their own maximum subscription period. All subscriptions should be renewed by re-subscribing before the subscription period ends to ensure continuity of updates. Hubs should additionally attempt to automatically refresh subscriptions before they expire by contacting Subscribers (handled automatically by the `Callback` class).
Chris@0 266 `hub.mode` | `subscribe` | Value indicating this is a subscription request. Unsubscription requests would use the "unsubscribe" value.
Chris@0 267 `hub.topic` | `http://www.example.net/rss.xml` | The URI of the Topic (i.e. Atom or RSS feed) which the Subscriber wishes to subscribe to for updates.
Chris@0 268 `hub.verify` | `sync` or `async` | Indicates to the Hub the preferred mode of verifying subscriptions or unsubscriptions. It is repeated twice in order of preference. Technically this component does not distinguish between the two modes and treats both equally.
Chris@0 269 `hub.verify_token` | `3065919804abcaa7212ae89.879827871253878386` | A verification token returned to the Subscriber by the Hub when it is confirming a subscription or unsubscription. Offers a measure of reliance that the confirmation request originates from the correct Hub to prevent misuse.
Chris@0 270
Chris@0 271 You can modify several of these parameters to indicate a different preference.
Chris@0 272 For example, you can set a different lease seconds value using
Chris@0 273 `Zend\Feed\PubSubHubbub\Subscriber::setLeaseSeconds(),` or show a preference for
Chris@0 274 the `async` verify mode by using `setPreferredVerificationMode(Zend\Feed\PubSubHubbub\PubSubHubbub::VERIFICATION_MODE_ASYNC)`.
Chris@0 275 However, the Hubs retain the capability to enforce their own preferences, and
Chris@0 276 for this reason the component is deliberately designed to work across almost any
Chris@0 277 set of options with minimum end-user configuration required. Conventions are
Chris@0 278 great when they work!
Chris@0 279
Chris@0 280 > ### Verification modes
Chris@0 281 >
Chris@0 282 > While Hubs may require the use of a specific verification mode (both are
Chris@0 283 > supported by `Zend\Feed\PubSubHubbub`), you may indicate a specific preference
Chris@0 284 > using the `setPreferredVerificationMode()` method. In `sync` (synchronous)
Chris@0 285 > mode, the Hub attempts to confirm a subscription as soon as it is received,
Chris@0 286 > and before responding to the subscription request. In `async` (asynchronous)
Chris@0 287 > mode, the Hub will return a response to the subscription request immediately,
Chris@0 288 > and its verification request may occur at a later time. Since
Chris@0 289 > `Zend\Feed\PubSubHubbub` implements the Subscriber verification role as a
Chris@0 290 > separate callback class and requires the use of a backend storage medium, it
Chris@0 291 > actually supports both transparently. In terms of end-user performance,
Chris@0 292 > asynchronous verification is very much preferred to eliminate the impact of a
Chris@0 293 > poorly performing Hub tying up end-user server resources and connections for
Chris@0 294 > too long.
Chris@0 295
Chris@0 296 Unsubscribing from a Topic follows the exact same pattern as the previous
Chris@0 297 example, with the exception that we should call `unsubscribeAll()` instead. The
Chris@0 298 parameters included are identical to a subscription request with the exception
Chris@0 299 that `hub.mode` is set to "unsubscribe".
Chris@0 300
Chris@0 301 By default, a new instance of `Zend\PubSubHubbub\Subscriber` will attempt to use
Chris@0 302 a database backed storage medium which defaults to using the default zend-db
Chris@0 303 adapter with a table name of "subscription". It is recommended to set a custom
Chris@0 304 storage solution where these defaults are not apt either by passing in a new
Chris@0 305 model supporting the required interface or by passing a new instance of
Chris@0 306 `Zend\Db\TableGateway\TableGateway` to the default model's constructor to change
Chris@0 307 the used table name.
Chris@0 308
Chris@0 309 ### Handling Subscriber Callbacks
Chris@0 310
Chris@0 311 Whenever a subscription or unsubscription request is made, the Hub must verify
Chris@0 312 the request by forwarding a new verification request to the callback URL set in
Chris@0 313 the subscription or unsubscription parameters. To handle these Hub requests,
Chris@0 314 which will include all future communications containing Topic (feed) updates,
Chris@0 315 the callback URL should trigger the execution of an instance of
Chris@0 316 `Zend\Feed\PubSubHubbub\Subscriber\Callback` to handle the request.
Chris@0 317
Chris@0 318 The `Callback` class should be configured to use the same storage medium as the
Chris@0 319 `Subscriber` class. The bulk of the work is handled internal to these classes.
Chris@0 320
Chris@0 321 ```php
Chris@0 322 use Zend\Feed\PubSubHubbub\Model\Subscription;
Chris@0 323 use Zend\Feed\PubSubHubbub\Subscriber\Callback;
Chris@0 324
Chris@0 325 $storage = new Subscription();
Chris@0 326 $callback = new Callback();
Chris@0 327 $callback->setStorage($storage);
Chris@0 328 $callback->handle();
Chris@0 329 $callback->sendResponse();
Chris@0 330
Chris@0 331 /*
Chris@0 332 * Check if the callback resulting in the receipt of a feed update.
Chris@0 333 * Otherwise it was either a (un)sub verification request or invalid request.
Chris@0 334 * Typically we need do nothing other than add feed update handling; the rest
Chris@0 335 * is handled internally by the class.
Chris@0 336 */
Chris@0 337 if ($callback->hasFeedUpdate()) {
Chris@0 338 $feedString = $callback->getFeedUpdate();
Chris@0 339 /*
Chris@0 340 * Process the feed update asynchronously to avoid a Hub timeout.
Chris@0 341 */
Chris@0 342 }
Chris@0 343 ```
Chris@0 344
Chris@0 345 > #### Query and body parameters
Chris@0 346 >
Chris@0 347 > It should be noted that `Zend\Feed\PubSubHubbub\Subscriber\Callback` may
Chris@0 348 > independently parse any incoming query string and other parameters. This is
Chris@0 349 > necessary since PHP alters the structure and keys of a query string when it is
Chris@0 350 > parsed into the `$_GET` or `$_POST` superglobals; for example, all duplicate
Chris@0 351 > keys are ignored and periods are converted to underscores. Pubsubhubbub
Chris@0 352 > features both of these in the query strings it generates.
Chris@0 353
Chris@0 354 > #### Always delay feed processing
Chris@0 355 >
Chris@0 356 > It is essential that developers recognise that Hubs are only concerned with
Chris@0 357 > sending requests and receiving a response which verifies its receipt. If a
Chris@0 358 > feed update is received, it should never be processed on the spot since this
Chris@0 359 > leaves the Hub waiting for a response. Rather, any processing should be
Chris@0 360 > offloaded to another process or deferred until after a response has been
Chris@0 361 > returned to the Hub. One symptom of a failure to promptly complete Hub
Chris@0 362 > requests is that a Hub may continue to attempt delivery of the update or
Chris@0 363 > verification request leading to duplicated update attempts being processed by
Chris@0 364 > the Subscriber. This appears problematic, but in reality a Hub may apply a
Chris@0 365 > timeout of just a few seconds, and if no response is received within that time
Chris@0 366 > it may disconnect (assuming a delivery failure) and retry later. Note that
Chris@0 367 > Hubs are expected to distribute vast volumes of updates so their resources are
Chris@0 368 > stretched; please process feeds asynchronously (e.g. in a separate process or
Chris@0 369 > a job queue or even a cronjob) as much as possible.
Chris@0 370
Chris@0 371 ### Setting Up And Using A Callback URL Route
Chris@0 372
Chris@0 373 As noted earlier, the `Zend\Feed\PubSubHubbub\Subscriber\Callback` class
Chris@0 374 receives the combined key associated with any subscription from the Hub via one
Chris@0 375 of two methods. The technically preferred method is to add this key to the
Chris@0 376 callback URL employed by the Hub in all future requests using a query string
Chris@0 377 parameter with the key `xhub.subscription`. However, for historical reasons
Chris@0 378 (primarily that this was not supported in Pubsubhubbub 0.1, and a late addition
Chris@0 379 to 0.2 ), it is strongly recommended to use the most compatible means of adding
Chris@0 380 this key to the callback URL by appending it to the URL's path.
Chris@0 381
Chris@0 382 Thus the URL `http://www.example.com/callback?xhub.subscription=key` would become
Chris@0 383 `http://www.example.com/callback/key`.
Chris@0 384
Chris@0 385 Since the query string method is the default in anticipation of a greater level
Chris@0 386 of future support for the full 0.2/0.3 specification, this requires some
Chris@0 387 additional work to implement.
Chris@0 388
Chris@0 389 The first step is to make the `Zend\Feed\PubSubHubbub\Subscriber\Callback` class
Chris@0 390 aware of the path contained subscription key. It's manually injected; therefore
Chris@0 391 it also requires manually defining a route for this purpose. This is achieved by
Chris@0 392 called the method `Zend\Feed\PubSubHubbub\Subscriber\Callback::setSubscriptionKey()`
Chris@0 393 with the parameter being the key value available from the router. The example
Chris@0 394 below demonstrates this using a zend-mvc controller.
Chris@0 395
Chris@0 396 ```php
Chris@0 397 use Zend\Feed\PubSubHubbub\Model\Subscription;
Chris@0 398 use Zend\Feed\PubSubHubbub\Subscriber\Callback;
Chris@0 399 use Zend\Mvc\Controller\AbstractActionController;
Chris@0 400
Chris@0 401 class CallbackController extends AbstractActionController
Chris@0 402 {
Chris@0 403
Chris@0 404 public function indexAction()
Chris@0 405 {
Chris@0 406 $storage = new Subscription();
Chris@0 407 $callback = new Callback();
Chris@0 408 $callback->setStorage($storage);
Chris@0 409
Chris@0 410 /*
Chris@0 411 * Inject subscription key parsing from URL path using
Chris@0 412 * a parameter from the router.
Chris@0 413 */
Chris@0 414 $subscriptionKey = $this->params()->fromRoute('subkey');
Chris@0 415 $callback->setSubscriptionKey($subscriptionKey);
Chris@0 416 $callback->handle();
Chris@0 417 $callback->sendResponse();
Chris@0 418
Chris@0 419 /*
Chris@0 420 * Check if the callback resulting in the receipt of a feed update.
Chris@0 421 * Otherwise it was either a (un)sub verification request or invalid
Chris@0 422 * request. Typically we need do nothing other than add feed update
Chris@0 423 * handling; the rest is handled internally by the class.
Chris@0 424 */
Chris@0 425 if ($callback->hasFeedUpdate()) {
Chris@0 426 $feedString = $callback->getFeedUpdate();
Chris@0 427 /*
Chris@0 428 * Process the feed update asynchronously to avoid a Hub timeout.
Chris@0 429 */
Chris@0 430 }
Chris@0 431 }
Chris@0 432 }
Chris@0 433 ```
Chris@0 434
Chris@0 435 The example below illustrates adding a route mapping the path segment to a route
Chris@0 436 parameter, using zend-mvc:
Chris@0 437
Chris@0 438 ```php
Chris@0 439 use Zend\Mvc\Router\Http\Segment as SegmentRoute;;
Chris@0 440
Chris@0 441 // Route defininition for enabling appending of a PuSH Subscription's lookup key
Chris@0 442 $route = SegmentRoute::factory([
Chris@0 443 'route' => '/callback/:subkey',
Chris@0 444 'constraints' => [
Chris@0 445 'subkey' => '[a-z0-9]+',
Chris@0 446 ],
Chris@0 447 'defaults' => [
Chris@0 448 'controller' => 'application-index',
Chris@0 449 'action' => 'index',
Chris@0 450 ]
Chris@0 451 ]);
Chris@0 452 ```