Zend_Feed_Pubsubhubbub.xml 35 KB

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