Zend_Feed_Pubsubhubbub.xml 34 KB

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