• Strophe
  • Punjab
  • Palaver
  • Speeqe
  • Stanziq
Strophe Logo
  • Login
  • Preferences
  • Help/Guide
  • OpenID Login
  • Wiki
  • Timeline
  • Roadmap
  • View Tickets
  • Search

Context Navigation

  • Back to Ticket #67

Ticket #67: strophe.pubsub.js

File strophe.pubsub.js, 12.2 KB (added by Liam, 6 months ago)

Whole file. A diff with the original isn't useful.

Line 
1/*
2    This program is distributed under the terms of the MIT license.
3    Please see the LICENSE file for details.
4
5    Copyright 2008, Stanziq  Inc.
6
7    Overhauled in October 2009 by Liam Breck [How does this affect copyright?]
8*/
9
10/** File: strophe.pubsub.js
11 *  A Strophe plugin for XMPP Publish-Subscribe.
12 *
13 *  Provides Strophe.Connection.pubsub object,
14 *  parially implementing XEP 0060.
15 *
16 *  Strophe.Builder.prototype methods should probably move to strophe.js
17 */
18
19/** Function: Strophe.Builder.form
20 *  Add an options form child element.
21 *
22 *  Does not change the current element.
23 *
24 *  Parameters:
25 *    (String) ns - form namespace.
26 *    (Object) options - form properties.
27 *
28 *  Returns:
29 *    The Strophe.Builder object.
30 */
31Strophe.Builder.prototype.form = function (ns, options)
32{
33    var aX = this.node.appendChild(Strophe.xmlElement('x', {xmlns: "jabber:x:data", type: "submit"}));
34
35    aX.appendChild(Strophe.xmlElement('field', {'var': "FORM_TYPE", type: "hidden"}))
36      .appendChild(Strophe.xmlElement('value'))
37      .appendChild(Strophe.xmlTextNode(ns));
38
39    for (var i in options) {
40      aX.appendChild(Strophe.xmlElement('field', {'var': i}))
41        .appendChild(Strophe.xmlElement('value'))
42        .appendChild(Strophe.xmlTextNode(options[i]));
43    }
44    return this;
45};
46
47/** Function: Strophe.Builder.list
48 *  Add many child elements.
49 *
50 *  Does not change the current element.
51 *
52 *  Parameters:
53 *    (String) tag - tag name for children.
54 *    (Array) array - members have format:
55 *          { attrs: { (String):(String), ... }, // optional attributes of each tag element
56 *             text: (String) }                  // optional contents of each tag element
57 *
58 *  Returns:
59 *    The Strophe.Builder object.
60 */
61Strophe.Builder.prototype.list = function (tag, array)
62{
63    for (var i=0; i < array.length; ++i) {
64        var el = this.node.appendChild(Strophe.xmlElement(tag, array[i].attrs))
65        if (array[i].text) {
66          el.appendChild(Strophe.xmlTextNode(array[i].text));
67        }
68    }
69    return this;
70};
71
72/* extend name space
73 *  NS.PUBSUB - XMPP Publish Subscribe namespace from XEP 0060. 
74 */
75Strophe.addNamespace('PUBSUB',                         "http://jabber.org/protocol/pubsub");
76Strophe.addNamespace('PUBSUB_SUBSCRIBE_OPTIONS',       Strophe.NS.PUBSUB+"#subscribe_options");
77Strophe.addNamespace('PUBSUB_ERRORS',                  Strophe.NS.PUBSUB+"#errors");
78Strophe.addNamespace('PUBSUB_EVENT',                   Strophe.NS.PUBSUB+"#event");
79Strophe.addNamespace('PUBSUB_OWNER',                   Strophe.NS.PUBSUB+"#owner");
80Strophe.addNamespace('PUBSUB_AUTO_CREATE',             Strophe.NS.PUBSUB+"#auto-create");
81Strophe.addNamespace('PUBSUB_PUBLISH_OPTIONS',         Strophe.NS.PUBSUB+"#publish-options");
82Strophe.addNamespace('PUBSUB_NODE_CONFIG',             Strophe.NS.PUBSUB+"#node_config");
83Strophe.addNamespace('PUBSUB_CREATE_AND_CONFIGURE',    Strophe.NS.PUBSUB+"#create-and-configure");
84Strophe.addNamespace('PUBSUB_SUBSCRIBE_AUTHORIZATION', Strophe.NS.PUBSUB+"#subscribe_authorization");
85Strophe.addNamespace('PUBSUB_GET_PENDING',             Strophe.NS.PUBSUB+"#get-pending");
86Strophe.addNamespace('PUBSUB_MANAGE_SUBSCRIPTIONS',    Strophe.NS.PUBSUB+"#manage-subscriptions");
87Strophe.addNamespace('PUBSUB_META_DATA',               Strophe.NS.PUBSUB+"#meta-data");
88       
89/* Extend Strophe.Connection to have member 'pubsub'. 
90 */
91Strophe.addConnectionPlugin('pubsub',
92{
93    // Called by Strophe.Connection constructor
94    init: function(conn)
95    {
96        this._connection = conn;
97        this._autoService = true;
98        this._service = null;
99    },
100
101    // Called by Strophe on connection event
102    statusChanged: function(status, condition)
103    {
104        if (this._autoService)
105            this._service = status === Strophe.Status.CONNECTED ? 'pubsub.'+Strophe.getDomainFromJid(this._connection.jid) : null;
106    },
107
108    /** Function: setService
109     *  Set pubsub service address. Use if 'pubsub.domain' isn't it.
110     * 
111     *  Parameters:
112     *    (String) service - service name
113     */
114    setService: function(service)
115    {
116        this._autoService = false;
117        this._service = service;
118    },
119
120    /** Function: createNode
121     *  Create a pubsub node on the service with the given node name.
122     * 
123     *  Parameters:
124     *    (String) node -  The name of the pubsub node.
125     *    (Object) options -  The configuration options for the  node.
126     *    (Function) call_back - Called on server response.
127     * 
128     *  Returns:
129     *    Iq id
130     */
131    createNode: function(node, options, call_back)
132    {
133        var iqid = this._connection.getUniqueId("createnode");
134       
135        var iq = $iq({from:this._connection.jid, to:this._service, type:'set', id:iqid})
136          .c('pubsub', {xmlns:Strophe.NS.PUBSUB})
137          .c('create',{node:node});
138        if(options) {
139            iq.up().c('configure').form(Strophe.NS.PUBSUB_NODE_CONFIG, options);
140        }
141       
142        this._connection.addHandler(call_back, null, 'iq', null, iqid, null);
143        this._connection.send(iq.tree());
144
145        return iqid;
146    },
147
148    /** Function: deleteNode
149     *  Delete a pubsub node.
150     * 
151     *  Parameters:
152     *    (String) node -  The name of the pubsub node.
153     *    (Function) call_back - Called on server response.
154     * 
155     *  Returns:
156     *    Iq id
157     */
158    deleteNode: function(node, call_back)
159    {
160        var iqid = this._connection.getUniqueId("deletenode");
161
162        var iq = $iq({from:this._connection.jid, to:this._service, type:'set', id:iqid})
163          .c('pubsub', {xmlns:Strophe.NS.PUBSUB_OWNER})
164          .c('delete', {node:node});
165
166        this._connection.addHandler(call_back, null, 'iq', null, iqid, null);
167        this._connection.send(iq.tree());
168
169        return iqid;
170    },
171
172    /** Function: getConfig
173     *  Get node configuration form.
174     * 
175     *  Parameters:
176     *    (String) node -  The name of the pubsub node.
177     *    (Function) call_back - Receives config form.
178     * 
179     *  Returns:
180     *    Iq id
181     */
182    getConfig: function(node, call_back)
183    {
184        var iqid = this._connection.getUniqueId("configurenode");
185
186        var iq = $iq({from:this._connection.jid, to:this._service, type:'get', id:iqid})
187          .c('pubsub', {xmlns:Strophe.NS.PUBSUB_OWNER})
188          .c('configure', {node:node});
189
190        this._connection.addHandler(call_back, null, 'iq', null, iqid, null);
191        this._connection.send(iq.tree());
192
193        return iqid;
194    },
195
196    /** Function: affiliate
197     *  Set affiliations for node.
198     * 
199     *  Parameters:
200     *    (String) node -  The name of the pubsub node.
201     *    (Array) list - See Strophe.Builder.list; attrs objects require jid & affiliation members.
202     *    (Function) call_back - Called on server response.
203     * 
204     *  Returns:
205     *    Iq id
206     */
207    affiliate: function(node, list, call_back)
208    {
209        var iqid = this._connection.getUniqueId("affiliatenode");
210
211        var iq = $iq({from:this._connection.jid, to:this._service, type:'set', id:iqid})
212          .c('pubsub', {xmlns:Strophe.NS.PUBSUB_OWNER})
213          .c('affiliations', {node:node})
214          .list('affiliation', list);
215
216        this._connection.addHandler(call_back, null, 'iq', null, iqid, null);
217        this._connection.send(iq.tree());
218
219        return iqid;
220    },
221
222    /** Function: subscribe
223     *  Subscribe to a node in order to receive event items.
224     * 
225     *  Parameters:
226     *    (String) node -  The name of the pubsub node.
227     *    (Object) options -  The configuration options for the  node.
228     *    (Function) event_cb - Used to recieve subscription events (optional).
229     *    (Function) call_back - Called on server response.
230     * 
231     *  Returns:
232     *    Iq id
233     */
234    subscribe: function(node, options, event_cb, call_back)
235    {
236        var iqid = this._connection.getUniqueId("subscribenode");
237       
238        var iq = $iq({from:this._connection.jid, to:this._service, type:'set', id:iqid})
239          .c('pubsub', { xmlns:Strophe.NS.PUBSUB })
240          .c('subscribe', {node:node, jid:this._connection.jid});
241        if(options) {
242            iq.up().c('options').form(Strophe.NS.PUBSUB_SUBSCRIBE_OPTIONS, options);
243        }
244       
245        this._connection.addHandler(call_back, null, 'iq', null, iqid, null);
246        if (event_cb) this._connection.addHandler(event_cb, null, 'message', null, null, null);
247        this._connection.send(iq.tree());
248       
249        return iqid;
250    },
251
252    /** Function: unsubscribe
253     *  Unsubscribe from a node.
254     * 
255     *  Parameters:
256     *    (String) node -  The name of the pubsub node.
257     *    (String) subid - The subscription id (optional).
258     *    (Function) call_back - Called on server response.       
259     * 
260     *  Returns:
261     *    Iq id
262     */   
263    unsubscribe: function(node, subid, call_back)
264    {
265        var iqid = this._connection.getUniqueId("unsubscribenode");
266       
267        var iq = $iq({from:this._connection.jid, to:this._service, type:'set', id:iqid})
268          .c('pubsub', { xmlns:Strophe.NS.PUBSUB })
269          .c('unsubscribe', {node:node, jid:this._connection.jid});
270        if (subid) iq.attrs({subid:subid});
271
272        this._connection.addHandler(call_back, null, 'iq', null, iqid, null);
273        this._connection.send(iq.tree());
274       
275        return iqid;
276    },
277
278    /** Function: getSubOptions
279     *  Get subscription options form.
280     * 
281     *  Parameters:
282     *    (String) node -  The name of the pubsub node.
283     *    (String) subid - The subscription id (optional).
284     *    (Function) call_back - Receives options form.
285     * 
286     *  Returns:
287     *    Iq id
288     */
289    getSubOptions: function(node, subid, call_back)
290    {
291        var iqid = this._connection.getUniqueId("suboptions");
292
293        var iq = $iq({from:this._connection.jid, to:this._service, type:'get', id:iqid})
294          .c('pubsub', {xmlns:Strophe.NS.PUBSUB})
295          .c('options', {node:node, jid:this._connection.jid});
296        if (subid) iq.attrs({subid:subid});
297
298        this._connection.addHandler(call_back, null, 'iq', null, iqid, null);
299        this._connection.send(iq.tree());
300
301        return iqid;
302    },
303
304    /** Function: publish
305     *  Publish a list of items to the given pubsub node.
306     * 
307     *  Parameters:
308     *    (String) node -  The name of the pubsub node.
309     *    (String) itemid - Optional value of item id param.
310     *    (Builder) builder - Strophe.Builder object.
311     *    (Function) call_back - Called on server response.
312     * 
313     *  Returns:
314     *    Iq id
315     */   
316    publish: function(node, itemid, builder, call_back)
317    {
318        var iqid = this._connection.getUniqueId("publishnode");
319       
320        var iq = $iq({from:this._connection.jid, to:this._service, type:'set', id:iqid})
321          .c('pubsub', { xmlns:Strophe.NS.PUBSUB })
322          .c('publish', { node:node })
323          .c('item', itemid ? {id:itemid} : null)
324          .cnode(builder.tree());
325       
326        this._connection.addHandler(call_back, null, 'iq', null, iqid, null);
327        this._connection.send(iq.tree());
328       
329        return iqid;
330    },
331
332    /** Function: publishAtomEntry
333     *  publish text in an Atom entry
334     */
335    publishAtomEntry: function(node, text, itemid, call_back)
336    {
337        var en = $build('entry', {xmlns: 'http://www.w3.org/2005/Atom'})
338          .t(text);
339        return this.publish(node, itemid, en, call_back);
340    },
341
342    /** Function: items
343     *  Retrieve the persistent items from the pubsub node.
344     * 
345     *  Parameters:
346     *    (String) node -  The name of the pubsub node.
347     *    (Function) call_back - Called on server response.
348     * 
349     *  Returns:
350     *    Iq id
351     */
352    items: function(node, call_back)
353    {
354        var iq = $iq({from:this._connection.jid, to:this._service, type:'get'})
355          .c('pubsub', { xmlns:Strophe.NS.PUBSUB })
356          .c('items', {node:node});
357       
358        return this._connection.sendIQ(iq.tree(), call_back, call_back);
359    }
360});

Download in other formats:

  • Original Format

Trac Powered

Powered by Trac 0.11.5
By Edgewall Software.

Visit the Trac open source project at
http://trac.edgewall.org/