Zend_Feed_Pubsubhubbub.xml 37 KB

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