strophe.pubsub.js
7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
Copyright 2008, Stanziq Inc.
*/
Strophe.addConnectionPlugin('pubsub', {
/*
Extend connection object to have plugin name 'pubsub'.
*/
_connection: null,
//The plugin must have the init function.
init: function(conn) {
this._connection = conn;
/*
Function used to setup plugin.
*/
/* extend name space
* NS.PUBSUB - XMPP Publish Subscribe namespace
* from XEP 60.
*
* NS.PUBSUB_SUBSCRIBE_OPTIONS - XMPP pubsub
* options namespace from XEP 60.
*/
Strophe.addNamespace('PUBSUB',"http://jabber.org/protocol/pubsub");
Strophe.addNamespace('PUBSUB_SUBSCRIBE_OPTIONS',
Strophe.NS.PUBSUB+"#subscribe_options");
Strophe.addNamespace('PUBSUB_ERRORS',Strophe.NS.PUBSUB+"#errors");
Strophe.addNamespace('PUBSUB_EVENT',Strophe.NS.PUBSUB+"#event");
Strophe.addNamespace('PUBSUB_OWNER',Strophe.NS.PUBSUB+"#owner");
Strophe.addNamespace('PUBSUB_AUTO_CREATE',
Strophe.NS.PUBSUB+"#auto-create");
Strophe.addNamespace('PUBSUB_PUBLISH_OPTIONS',
Strophe.NS.PUBSUB+"#publish-options");
Strophe.addNamespace('PUBSUB_NODE_CONFIG',
Strophe.NS.PUBSUB+"#node_config");
Strophe.addNamespace('PUBSUB_CREATE_AND_CONFIGURE',
Strophe.NS.PUBSUB+"#create-and-configure");
Strophe.addNamespace('PUBSUB_SUBSCRIBE_AUTHORIZATION',
Strophe.NS.PUBSUB+"#subscribe_authorization");
Strophe.addNamespace('PUBSUB_GET_PENDING',
Strophe.NS.PUBSUB+"#get-pending");
Strophe.addNamespace('PUBSUB_MANAGE_SUBSCRIPTIONS',
Strophe.NS.PUBSUB+"#manage-subscriptions");
Strophe.addNamespace('PUBSUB_META_DATA',
Strophe.NS.PUBSUB+"#meta-data");
},
/***Function
Create a pubsub node on the given service with the given node
name.
Parameters:
(String) jid - The node owner's jid.
(String) service - The name of the pubsub service.
(String) node - The name of the pubsub node.
(Dictionary) options - The configuration options for the node.
(Function) call_back - Used to determine if node
creation was sucessful.
Returns:
Iq id used to send subscription.
*/
createNode: function(jid,service,node,options, call_back) {
var iqid = this._connection.getUniqueId("pubsubcreatenode");
var iq = $iq({from:jid, to:service, type:'set', id:iqid});
var c_options = Strophe.xmlElement("configure",[]);
var x = Strophe.xmlElement("x",[["xmlns","jabber:x:data"]]);
var form_field = Strophe.xmlElement("field",[["var","FORM_TYPE"],
["type","hidden"]]);
var value = Strophe.xmlElement("value",[]);
var text = Strophe.xmlTextNode(Strophe.NS.PUBSUB+"#node_config");
value.appendChild(text);
form_field.appendChild(value);
x.appendChild(form_field);
for (var i in options)
{
var val = options[i];
x.appendChild(val);
}
if(options.length && options.length != 0)
{
c_options.appendChild(x);
}
iq.c('pubsub',
{xmlns:Strophe.NS.PUBSUB}).c('create',
{node:node}).up().cnode(c_options);
this._connection.addHandler(call_back,
null,
'iq',
null,
iqid,
null);
this._connection.send(iq.tree());
return iqid;
},
/***Function
Subscribe to a node in order to receive event items.
Parameters:
(String) jid - The node owner's jid.
(String) service - The name of the pubsub service.
(String) node - The name of the pubsub node.
(Array) options - The configuration options for the node.
(Function) event_cb - Used to recieve subscription events.
(Function) call_back - Used to determine if node
creation was sucessful.
Returns:
Iq id used to send subscription.
*/
subscribe: function(jid,service,node,options, event_cb, call_back) {
var subid = this._connection.getUniqueId("subscribenode");
//create subscription options
var sub_options = Strophe.xmlElement("options",[]);
var x = Strophe.xmlElement("x",[["xmlns","jabber:x:data"]]);
var form_field = Strophe.xmlElement("field",[["var","FORM_TYPE"],
["type","hidden"]]);
var value = Strophe.xmlElement("value",[]);
var text = Strophe.xmlTextNode(Strophe.NS.PUBSUB_SUBSCRIBE_OPTIONS);
value.appendChild(text);
form_field.appendChild(value);
x.appendChild(form_field);
var sub = $iq({from:jid, to:service, type:'set', id:subid})
if(options && options.length && options.length !== 0)
{
for (var i = 0; i < options.length; i++)
{
var val = options[i];
x.appendChild(val);
}
sub_options.appendChild(x);
sub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).c('subscribe',
{node:node,jid:jid}).up().cnode(sub_options);
}
else
{
sub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).c('subscribe',
{node:node,jid:jid});
}
this._connection.addHandler(call_back,
null,
'iq',
null,
subid,
null);
//add the event handler to receive items
this._connection.addHandler(event_cb,
null,
'message',
null,
null,
null);
this._connection.send(sub.tree());
return subid;
},
/***Function
Unsubscribe from a node.
Parameters:
(String) jid - The node owner's jid.
(String) service - The name of the pubsub service.
(String) node - The name of the pubsub node.
(Function) call_back - Used to determine if node
creation was sucessful.
*/
unsubscribe: function(jid,service,node, call_back) {
var subid = this._connection.getUniqueId("unsubscribenode");
var sub = $iq({from:jid, to:service, type:'set', id:subid})
sub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).c('unsubscribe',
{node:node,jid:jid});
this._connection.addHandler(call_back,
null,
'iq',
null,
subid,
null);
this._connection.send(sub.tree());
return subid;
},
/***Function
Publish and item to the given pubsub node.
Parameters:
(String) jid - The node owner's jid.
(String) service - The name of the pubsub service.
(String) node - The name of the pubsub node.
(Array) items - The list of items to be published.
(Function) call_back - Used to determine if node
creation was sucessful.
*/
publish: function(jid, service, node, items, call_back) {
var pubid = this._connection.getUniqueId("publishnode");
var publish_elem = Strophe.xmlElement("publish",
[["node",
node],
["jid",
jid]]);
for (var i in items)
{
var item = Strophe.xmlElement("item",[]);
var entry = Strophe.xmlElement("entry",[]);
var t = Strophe.xmlTextNode(items[i]);
entry.appendChild(t);
item.appendChild(entry);
publish_elem.appendChild(item);
}
var pub = $iq({from:jid, to:service, type:'set', id:pubid})
pub.c('pubsub', { xmlns:Strophe.NS.PUBSUB }).cnode(publish_elem);
this._connection.addHandler(call_back,
null,
'iq',
null,
pubid,
null);
this._connection.send(pub.tree());
return pubid;
},
/*Function: items
Used to retrieve the persistent items from the pubsub node.
*/
items: function(jid,service,node,ok_callback,error_back) {
var pub = $iq({from:jid, to:service, type:'get'})
//ask for all items
pub.c('pubsub',
{ xmlns:Strophe.NS.PUBSUB }).c('items',{node:node});
return this._connection.sendIQ(pub.tree(),ok_callback,error_back);
}
});