2
0

developer_api.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/theming.rst">Edit me on GitHub</a></div>
  3. The converse.js developer API
  4. =============================
  5. .. contents:: Table of Contents
  6. :depth: 2
  7. :local:
  8. .. note:: The API documented here is available in Converse.js 0.8.4 and higher.
  9. Earlier versions of Converse.js might have different API methods or none at all.
  10. In the Converse.js API, you traverse towards a logical grouping, from
  11. which you can then call certain standardised accessors and mutators, such as::
  12. .get
  13. .set
  14. .add
  15. .remove
  16. This is done to increase readability and to allow intuitive method chaining.
  17. For example, to get a contact, you would do the following::
  18. converse.contacts.get('jid@example.com');
  19. To get multiple contacts, just pass in an array of jids::
  20. converse.contacts.get(['jid1@example.com', 'jid2@example.com']);
  21. To get all contacts, simply call ``get`` without any jids::
  22. converse.contacts.get();
  23. **Here follows now a breakdown of all API groupings and methods**:
  24. initialize
  25. ----------
  26. .. note:: This method is the one exception of a method which is not logically grouped
  27. as explained above.
  28. Initializes converse.js. This method must always be called when using
  29. converse.js.
  30. The `initialize` method takes a map (also called a hash or dictionary) of :ref:`configuration-variables`.
  31. Example:
  32. .. code-block:: javascript
  33. converse.initialize({
  34. allow_otr: true,
  35. auto_list_rooms: false,
  36. auto_subscribe: false,
  37. bosh_service_url: 'https://bind.example.com',
  38. hide_muc_server: false,
  39. i18n: locales['en'],
  40. keepalive: true,
  41. play_sounds: true,
  42. prebind: false,
  43. show_controlbox_by_default: true,
  44. debug: false,
  45. roster_groups: true
  46. });
  47. send
  48. ----
  49. Allows you to send XML stanzas.
  50. For example, to send a message stanza:
  51. .. code-block:: javascript
  52. var msg = converse.env.$msg({
  53. from: 'juliet@example.com/balcony',
  54. to:'romeo@example.net',
  55. type:'chat'
  56. });
  57. converse.send(msg);
  58. The **archive** grouping
  59. ------------------------
  60. Converse.js supports the *Message Archive Management*
  61. (`XEP-0313 <https://xmpp.org/extensions/xep-0313.html>`_) protocol,
  62. through which it is able to query an XMPP server for archived messages.
  63. See also the **message_archiving** option in the :ref:`configuration-variables` section, which you'll usually
  64. want to in conjunction with this API.
  65. query
  66. ~~~~~
  67. The ``query`` method is used to query for archived messages.
  68. It accepts the following optional parameters:
  69. * **options** an object containing the query parameters. Valid query parameters
  70. are ``with``, ``start``, ``end``, ``first``, ``last``, ``after``, ``before``, ``index`` and ``count``.
  71. * **callback** is the callback method that will be called when all the messages
  72. have been received.
  73. * **errback** is the callback method to be called when an error is returned by
  74. the XMPP server, for example when it doesn't support message archiving.
  75. Examples
  76. ^^^^^^^^
  77. **Requesting all archived messages**
  78. The simplest query that can be made is to simply not pass in any parameters.
  79. Such a query will return all archived messages for the current user.
  80. Generally, you'll however always want to pass in a callback method, to receive
  81. the returned messages.
  82. .. code-block:: javascript
  83. var errback = function (iq) {
  84. // The query was not successful, perhaps inform the user?
  85. // The IQ stanza returned by the XMPP server is passed in, so that you
  86. // may inspect it and determine what the problem was.
  87. }
  88. var callback = function (messages) {
  89. // Do something with the messages, like showing them in your webpage.
  90. }
  91. converse.archive.query(callback, errback))
  92. **Waiting until server support has been determined**
  93. The query method will only work if converse.js has been able to determine that
  94. the server supports MAM queries, otherwise the following error will be raised:
  95. - *This server does not support XEP-0313, Message Archive Management*
  96. The very first time converse.js loads in a browser tab, if you call the query
  97. API too quickly, the above error might appear because service discovery has not
  98. yet been completed.
  99. To work solve this problem, you can first listen for the ``serviceDiscovered`` event,
  100. through which you can be informed once support for MAM has been determined.
  101. For example:
  102. .. code-block:: javascript
  103. converse.listen.on('serviceDiscovered', function (event, feature) {
  104. if (feature.get('var') === converse.env.Strophe.NS.MAM) {
  105. converse.archive.query()
  106. }
  107. });
  108. **Requesting all archived messages for a particular contact or room**
  109. To query for messages sent between the current user and another user or room,
  110. the query options need to contain the the JID (Jabber ID) of the user or
  111. room under the ``with`` key.
  112. .. code-block:: javascript
  113. // For a particular user
  114. converse.archive.query({'with': 'john@doe.net'}, callback, errback);)
  115. // For a particular room
  116. converse.archive.query({'with': 'discuss@conference.doglovers.net'}, callback, errback);)
  117. **Requesting all archived messages before or after a certain date**
  118. The ``start`` and ``end`` parameters are used to query for messages
  119. within a certain timeframe. The passed in date values may either be ISO8601
  120. formatted date strings, or Javascript Date objects.
  121. .. code-block:: javascript
  122. var options = {
  123. 'with': 'john@doe.net',
  124. 'start': '2010-06-07T00:00:00Z',
  125. 'end': '2010-07-07T13:23:54Z'
  126. };
  127. converse.archive.query(options, callback, errback);
  128. **Limiting the amount of messages returned**
  129. The amount of returned messages may be limited with the ``max`` parameter.
  130. By default, the messages are returned from oldest to newest.
  131. .. code-block:: javascript
  132. // Return maximum 10 archived messages
  133. converse.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  134. **Paging forwards through a set of archived messages**
  135. When limiting the amount of messages returned per query, you might want to
  136. repeatedly make a further query to fetch the next batch of messages.
  137. To simplify this usecase for you, the callback method receives not only an array
  138. with the returned archived messages, but also a special RSM (*Result Set
  139. Management*) object which contains the query parameters you passed in, as well
  140. as two utility methods ``next``, and ``previous``.
  141. When you call one of these utility methods on the returned RSM object, and then
  142. pass the result into a new query, you'll receive the next or previous batch of
  143. archived messages. Please note, when calling these methods, pass in an integer
  144. to limit your results.
  145. .. code-block:: javascript
  146. var callback = function (messages, rsm) {
  147. // Do something with the messages, like showing them in your webpage.
  148. // ...
  149. // You can now use the returned "rsm" object, to fetch the next batch of messages:
  150. converse.archive.query(rsm.next(10), callback, errback))
  151. }
  152. converse.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  153. **Paging backwards through a set of archived messages**
  154. To page backwards through the archive, you need to know the UID of the message
  155. which you'd like to page backwards from and then pass that as value for the
  156. ``before`` parameter. If you simply want to page backwards from the most recent
  157. message, pass in the ``before`` parameter with an empty string value ``''``.
  158. .. code-block:: javascript
  159. converse.archive.query({'before': '', 'max':5}, function (message, rsm) {
  160. // Do something with the messages, like showing them in your webpage.
  161. // ...
  162. // You can now use the returned "rsm" object, to fetch the previous batch of messages:
  163. rsm.previous(5); // Call previous method, to update the object's parameters,
  164. // passing in a limit value of 5.
  165. // Now we query again, to get the previous batch.
  166. converse.archive.query(rsm, callback, errback);
  167. }
  168. The **connection** grouping
  169. ---------------------------
  170. This grouping collects API functions related to the XMPP connection.
  171. connected
  172. ~~~~~~~~~
  173. A boolean attribute (i.e. not a callable) which is set to `true` or `false` depending
  174. on whether there is an established connection.
  175. disconnect
  176. ~~~~~~~~~~
  177. Terminates the connection.
  178. The **user** grouping
  179. ---------------------
  180. This grouping collects API functions related to the current logged in user.
  181. jid
  182. ~~~
  183. Return's the current user's full JID (Jabber ID).
  184. .. code-block:: javascript
  185. converse.user.jid()
  186. // Returns for example jc@opkode.com/conversejs-351236
  187. login
  188. ~~~~~
  189. Logs the user in. This method can accept a map with the credentials, like this:
  190. .. code-block:: javascript
  191. converse.user.login({
  192. 'jid': 'dummy@example.com',
  193. 'password': 'secret'
  194. });
  195. or it can be called without any parameters, in which case converse.js will try
  196. to log the user in by calling the `prebind_url` or `credentials_url` depending
  197. on whether prebinding is used or not.
  198. logout
  199. ~~~~~~
  200. Log the user out of the current XMPP session.
  201. .. code-block:: javascript
  202. converse.user.logout();
  203. The **status** sub-grouping
  204. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  205. Set and get the user's chat status, also called their *availability*.
  206. get
  207. ^^^
  208. Return the current user's availability status:
  209. .. code-block:: javascript
  210. converse.user.status.get(); // Returns for example "dnd"
  211. set
  212. ^^^
  213. The user's status can be set to one of the following values:
  214. * **away**
  215. * **dnd**
  216. * **offline**
  217. * **online**
  218. * **unavailable**
  219. * **xa**
  220. For example:
  221. .. code-block:: javascript
  222. converse.user.status.set('dnd');
  223. Because the user's availability is often set together with a custom status
  224. message, this method also allows you to pass in a status message as a
  225. second parameter:
  226. .. code-block:: javascript
  227. converse.user.status.set('dnd', 'In a meeting');
  228. The **message** sub-grouping
  229. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  230. The ``user.status.message`` sub-grouping exposes methods for setting and
  231. retrieving the user's custom status message.
  232. .. code-block:: javascript
  233. converse.user.status.message.set('In a meeting');
  234. converse.user.status.message.get(); // Returns "In a meeting"
  235. The **contacts** grouping
  236. -------------------------
  237. get
  238. ~~~
  239. This method is used to retrieve roster contacts.
  240. To get a single roster contact, call the method with the contact's JID (Jabber ID):
  241. .. code-block:: javascript
  242. converse.contacts.get('buddy@example.com')
  243. To get multiple contacts, pass in an array of JIDs:
  244. .. code-block:: javascript
  245. converse.contacts.get(['buddy1@example.com', 'buddy2@example.com'])
  246. To return all contacts, simply call ``get`` without any parameters:
  247. .. code-block:: javascript
  248. converse.contacts.get()
  249. The returned roster contact objects have these attributes:
  250. +----------------+-----------------------------------------------------------------------------------------------------------------+
  251. | Attribute | |
  252. +================+=================================================================================================================+
  253. | ask | If ask === 'subscribe', then we have asked this person to be our chat buddy. |
  254. +----------------+-----------------------------------------------------------------------------------------------------------------+
  255. | fullname | The person's full name. |
  256. +----------------+-----------------------------------------------------------------------------------------------------------------+
  257. | jid | The person's Jabber/XMPP username. |
  258. +----------------+-----------------------------------------------------------------------------------------------------------------+
  259. | requesting | If true, then this person is asking to be our chat buddy. |
  260. +----------------+-----------------------------------------------------------------------------------------------------------------+
  261. | subscription | The subscription state between the current user and this chat buddy. Can be `none`, `to`, `from` or `both`. |
  262. +----------------+-----------------------------------------------------------------------------------------------------------------+
  263. | id | A unique id, same as the jid. |
  264. +----------------+-----------------------------------------------------------------------------------------------------------------+
  265. | chat_status | The person's chat status. Can be `online`, `offline`, `busy`, `xa` (extended away) or `away`. |
  266. +----------------+-----------------------------------------------------------------------------------------------------------------+
  267. | user_id | The user id part of the JID (the part before the `@`). |
  268. +----------------+-----------------------------------------------------------------------------------------------------------------+
  269. | resources | The known resources for this chat buddy. Each resource denotes a separate and connected chat client. |
  270. +----------------+-----------------------------------------------------------------------------------------------------------------+
  271. | groups | The roster groups in which this chat buddy was placed. |
  272. +----------------+-----------------------------------------------------------------------------------------------------------------+
  273. | status | Their human readable custom status message. |
  274. +----------------+-----------------------------------------------------------------------------------------------------------------+
  275. | image_type | The image's file type. |
  276. +----------------+-----------------------------------------------------------------------------------------------------------------+
  277. | image | The Base64 encoded image data. |
  278. +----------------+-----------------------------------------------------------------------------------------------------------------+
  279. | url | The buddy's website URL, as specified in their VCard data. |
  280. +----------------+-----------------------------------------------------------------------------------------------------------------+
  281. | vcard_updated | When last the buddy's VCard was updated. |
  282. +----------------+-----------------------------------------------------------------------------------------------------------------+
  283. add
  284. ~~~
  285. Add a contact.
  286. Provide the JID of the contact you want to add:
  287. .. code-block:: javascript
  288. converse.contacts.add('buddy@example.com')
  289. You may also provide the fullname. If not present, we use the jid as fullname:
  290. .. code-block:: javascript
  291. converse.contacts.add('buddy@example.com', 'Buddy')
  292. The **chats** grouping
  293. ----------------------
  294. Note, for MUC chat rooms, you need to use the "rooms" grouping instead.
  295. get
  296. ~~~
  297. Returns an object representing a chat box.
  298. To return a single chat box, provide the JID of the contact you're chatting
  299. with in that chat box:
  300. .. code-block:: javascript
  301. converse.chats.get('buddy@example.com')
  302. To return an array of chat boxes, provide an array of JIDs:
  303. .. code-block:: javascript
  304. converse.chats.get(['buddy1@example.com', 'buddy2@example.com'])
  305. To return all open chat boxes, call the method without any JIDs::
  306. converse.chats.get()
  307. open
  308. ~~~~
  309. Opens a chat box and returns an object representing a chat box.
  310. To open a single chat box, provide the JID of the contact:
  311. .. code-block:: javascript
  312. converse.chats.open('buddy@example.com')
  313. To return an array of chat boxes, provide an array of JIDs:
  314. .. code-block:: javascript
  315. converse.chats.open(['buddy1@example.com', 'buddy2@example.com'])
  316. *The returned chat box object contains the following methods:*
  317. +-------------+------------------------------------------+
  318. | Method | Description |
  319. +=============+==========================================+
  320. | endOTR | End an OTR (Off-the-record) session. |
  321. +-------------+------------------------------------------+
  322. | get | Get an attribute (i.e. accessor). |
  323. +-------------+------------------------------------------+
  324. | initiateOTR | Start an OTR (off-the-record) session. |
  325. +-------------+------------------------------------------+
  326. | maximize | Minimize the chat box. |
  327. +-------------+------------------------------------------+
  328. | minimize | Maximize the chat box. |
  329. +-------------+------------------------------------------+
  330. | set | Set an attribute (i.e. mutator). |
  331. +-------------+------------------------------------------+
  332. | close | Close the chat box. |
  333. +-------------+------------------------------------------+
  334. | open | Opens the chat box. |
  335. +-------------+------------------------------------------+
  336. *The get and set methods can be used to retrieve and change the following attributes:*
  337. +-------------+-----------------------------------------------------+
  338. | Attribute | Description |
  339. +=============+=====================================================+
  340. | height | The height of the chat box. |
  341. +-------------+-----------------------------------------------------+
  342. | url | The URL of the chat box heading. |
  343. +-------------+-----------------------------------------------------+
  344. The **rooms** grouping
  345. ----------------------
  346. get
  347. ~~~
  348. Returns an object representing a multi user chat box (room).
  349. It takes 3 parameters:
  350. * the room JID (if not specified, all rooms will be returned).
  351. * a map (object) containing any extra room attributes For example, if you want
  352. to specify the nickname, use ``{'nick': 'bloodninja'}``. Previously (before
  353. version 1.0.7, the second parameter only accepted the nickname (as a string
  354. value). This is currently still accepted, but then you can't pass in any
  355. other room attributes. If the nickname is not specified then the node part of
  356. the user's JID will be used.
  357. * a boolean, indicating whether the room should be created if not found (default: `false`)
  358. .. code-block:: javascript
  359. var nick = 'dread-pirate-roberts';
  360. var create_if_not_found = true;
  361. converse.rooms.open('group@muc.example.com', {'nick': nick}, create_if_not_found)
  362. open
  363. ~~~~
  364. Opens a multi user chat box and returns an object representing it.
  365. Similar to the ``chats.get`` API.
  366. It takes 2 parameters:
  367. * The room JID or JIDs (if not specified, all currently open rooms will be returned).
  368. * A map (object) containing any extra room attributes. For example, if you want
  369. to specify the nickname, use ``{'nick': 'bloodninja'}``.
  370. To open a single multi user chat box, provide the JID of the room:
  371. .. code-block:: javascript
  372. converse.rooms.open('group@muc.example.com')
  373. To return an array of rooms, provide an array of room JIDs:
  374. .. code-block:: javascript
  375. converse.rooms.open(['group1@muc.example.com', 'group2@muc.example.com'])
  376. To setup a custom nickname when joining the room, provide the optional nick argument:
  377. .. code-block:: javascript
  378. converse.rooms.open('group@muc.example.com', {'nick': 'mycustomnick'})
  379. Room attributes that may be passed in:
  380. * *nick*: The nickname to be used
  381. * *auto_configure*: A boolean, indicating whether the room should be configured
  382. automatically or not. If set to ``true``, then it makes sense to pass in
  383. configuration settings.
  384. * *roomconfig*: A map of configuration settings to be used when the room gets
  385. configured automatically. Currently it doesn't make sense to specify
  386. ``roomconfig`` values if ``auto_configure`` is set to ``false``.
  387. For a list of configuration values that can be passed in, refer to these values
  388. in the `XEP-0045 MUC specification <http://xmpp.org/extensions/xep-0045.html#registrar-formtype-owner>`_.
  389. The values should be named without the ``muc#roomconfig_`` prefix.
  390. * *maximize*: A boolean, indicating whether minimized rooms should also be
  391. maximized, when opened. Set to ``false`` by default.
  392. For example, opening a room with a specific default configuration:
  393. .. code-block:: javascript
  394. converse.rooms.open(
  395. 'myroom@conference.example.org',
  396. { 'nick': 'coolguy69',
  397. 'auto_configure': true,
  398. 'roomconfig': {
  399. 'changesubject': false,
  400. 'membersonly': true,
  401. 'persistentroom': true,
  402. 'publicroom': true,
  403. 'roomdesc': 'Comfy room for hanging out',
  404. 'whois': 'anyone'
  405. }
  406. },
  407. true
  408. );
  409. .. note:: `multi-list` configuration values are not yet supported.
  410. close
  411. ~~~~~
  412. Lets you close open chat rooms. You can call this method without any arguments
  413. to close all open chat rooms, or you can specify a single JID or an array of
  414. JIDs.
  415. The **settings** grouping
  416. -------------------------
  417. This grouping allows you to get or set the configuration settings of converse.js.
  418. get(key)
  419. ~~~~~~~~
  420. Returns the value of a configuration settings. For example:
  421. .. code-block:: javascript
  422. converse.settings.get("play_sounds"); // default value returned would be false;
  423. set(key, value) or set(object)
  424. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  425. Set one or many configuration settings. For example:
  426. .. code-block:: javascript
  427. converse.settings.set("play_sounds", true);
  428. or :
  429. .. code-block:: javascript
  430. converse.settings.set({
  431. "play_sounds", true,
  432. "hide_offline_users" true
  433. });
  434. Note, this is not an alternative to calling ``converse.initialize``, which still needs
  435. to be called. Generally, you'd use this method after converse.js is already
  436. running and you want to change the configuration on-the-fly.
  437. The **tokens** grouping
  438. -----------------------
  439. get
  440. ~~~
  441. Returns a token, either the RID or SID token depending on what's asked for.
  442. Example:
  443. .. code-block:: javascript
  444. converse.tokens.get('rid')
  445. .. _`listen-grouping`:
  446. The **listen** grouping
  447. -----------------------
  448. Converse.js emits events to which you can subscribe from your own Javascript.
  449. Concerning events, the following methods are available under the "listen"
  450. grouping:
  451. * **on(eventName, callback, [context])**:
  452. Calling the ``on`` method allows you to subscribe to an event.
  453. Every time the event fires, the callback method specified by ``callback`` will be
  454. called.
  455. Parameters:
  456. * ``eventName`` is the event name as a string.
  457. * ``callback`` is the callback method to be called when the event is emitted.
  458. * ``context`` (optional), the value of the `this` parameter for the callback.
  459. For example:
  460. .. code-block:: javascript
  461. converse.listen.on('message', function (event, messageXML) { ... });
  462. * **once(eventName, callback, [context])**:
  463. Calling the ``once`` method allows you to listen to an event
  464. exactly once.
  465. Parameters:
  466. * ``eventName`` is the event name as a string.
  467. * ``callback`` is the callback method to be called when the event is emitted.
  468. * ``context`` (optional), the value of the `this` parameter for the callback.
  469. For example:
  470. .. code-block:: javascript
  471. converse.listen.once('message', function (event, messageXML) { ... });
  472. * **not(eventName, callback)**
  473. To stop listening to an event, you can use the ``not`` method.
  474. Parameters:
  475. * ``eventName`` is the event name as a string.
  476. * ``callback`` refers to the function that is to be no longer executed.
  477. For example:
  478. .. code-block:: javascript
  479. converse.listen.not('message', function (event, messageXML) { ... });