在XMPP世界里,你已经熟悉发布/订阅模型,因为XMPP原语出席是发布/订阅的一个特殊形式,更多发布/订阅模型的通用形式将由一个专门的XMPP扩展叫做PubSub的提供
直接进入示例(发布订阅整体流程):
订阅ejabberd.org服务上的beta_proclamations节点
<iq from="user@ejabberd.org/Psi"
id="gh921nx3"
to="ejabberd.org"
type="set">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<subscribe node="beta_proclamations" jid="user@ejabberd.org"/>
</pubsub>
</iq>
订阅成功通知用户(添加了一个subscription属性,值为subscribed指示订阅请求成功)
<iq from="ejabberd.org"
id="gh921nx3"
to="user@ejabberd.org/Psi"
type="result">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<subscription node="beta_proclamations" jid="user@ejabberd.org" subscription="subscribed"/>
</pubsub>
</iq>
发布博文
<iq from="beta@ejabberd.org"
id="ma019r58"
to="ejabberd.org"
type="set">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<publish node="beta_proclamations"> %%发布条目
<item> %%有效载荷
<entry xmlns="http://www.w3.org/2005/Atom">
<title>A new thought</title>
<summary>Off with their heads!</summary>
<link rel="alternate" type="text/html" href="http://ejabberd.org/1865/"/>
<id>tag:ejabberd.org,1865:entry-42</id>
<published>1865-12-13T18:30:02Z</published>
<updated>1865-12-13T18:30:02Z</updated>
</entry>
</item>
</publish>
</pubsub>
</iq>
博文发布到节点,给user发送通告(采用消息发送,因为不知用户是否在线,直接交付给XMPP服务器处理)
<message from="ejabberd.org" to="user@ejabberd.org">
<body>A new thought: off with their heads!</body>
<event xmlns="http://jabber.org/protocol/pubsub#event">
<items node="beta_proclamations" id="bl38pahu98h">
<item id="zi2ba967">
<entry xmlns="http://www.w3.org/2005/Atom">
<title>A new thought</title>
<summary>Off with their heads!</summary>
<link rel="alternate" type="text/html" href="http://ejabberd.org/1865/"/>
<id>tag: ejabberd.org,1865:entry-42</id>
<published>1865-12-13T18:30:02Z</published>
<updated>1865-12-13T18:30:02Z</updated>
</entry>
</item>
</items>
</event>
</message>
退订ejabberd.org服务上的beta_proclamations节点
<iq from="user@ejabberd.org/Psi"
id="vd923k66"
to= "ejabberd.org"
type="set">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<unsubscribe node="beta_proclamations" jid="user@ejabberd.org"/>
</pubsub>
</iq>
退订成功通知用户
<iq from="ejabberd.org"
id="vd923k66"
to="user@ejabberd.org/Psi"
type="result"/>
PubSub配置
PubSub节点配置,面临两个基本的选择:
1 节点发送出去的通告是否将包含有效载荷(配置deliver_payloads选项,产生相应的payload-included节点和notification-only节点)
2 节点发布的条目是否存储以便以后被检索(配置persist_items选项)
Payloads:To send or not to send ?
不带载荷的通告:
<message from="ejabberd.org" to="user@ejabberd.org">
<event xmlns="http://jabber.org/protocol/pubsub#event">
<items node="beta_proclamations">
<item id="zi2ba967"/>
</items>
</event>
</message>
Items:To store or not to store ?
订阅某节点特定有效载荷:
<iq from="user@ejabberd.org/Psi"
id="ka03p485"
to="ejabberd.org"
type="get"> 1
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<items node="beta_proclamations">
<item id="zi2ba967"/> 2
</items>
</pubsub>
</iq>
节点发现(参考4发现世界)
节点管理
创建beta_Proclamations节点
<iq from="beta@ejabberd.org/Psi"
id="cr561nd0"
to="ejabberd.org"
type="set">
<pubsub xmlns="http://jabber.org/protocol/pubsub">
<create node="beta_proclamations"/>
</pubsub>
</iq>
删除节点
<iq from="beta@ejabberd.org/Psi"
id="d3l3t41t"
to="ejabberd.org"
type="set">
<pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
<create node="beta_proclamations"/>
</pubsub>
</iq>
配置节点(通过数据表单配置)
管理节点读取
1 Open access model允许任何人订阅
2 Whitelist access model 仅允许预先选择好的 JIDs 订阅或检索节点
3 Authorize access model 需要节点拥有者批准任何订阅请求
4 Presence access model 允许已经对节点拥有者有出席订阅的那些 JIDs 订阅
5 Roster access model 让你能够限制在特定名册分组的 JID 订阅
示例:
user1只对朋友开放
首先检查当前从属的博客的JID
<iq from="user1@ejabberd.org/Psi"
id="wl62hf87"
to="ejabberd.org"
type="get">
<pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
<affiliations node="user1’s_blog"/>
</pubsub>
</iq>
<iq from="ejabberd.org"
id="wl62hf87"
to="user1@ejabberd.org/rabbithole"
type="result">
<pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
<affiliations node="user1’s_blog">
<affiliation jid="user1@ejabberd.org" affiliation="owner"/>
<affiliation jid="gmz@ejabberd.org" affiliation="member"/>
</affiliations>
</pubsub>
</iq>
当前user1拥有这个博客节点,gmz允许读取(所属的成员)
user2对每个请求使用授权模型(会收到每个用户订阅的申请批准)
-略