developer_api.rst 24 KB

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