developer_api.rst 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. =============================
  4. The converse.js developer API
  5. =============================
  6. .. contents:: Table of Contents
  7. :depth: 2
  8. :local:
  9. .. note:: The API documented here is available in Converse.js 0.8.4 and higher.
  10. Earlier versions of Converse.js might have different API methods or none at all.
  11. .. note:: From version 3.0.0 and onwards many API methods have been made
  12. private and available to plugins only. This means that if you want to
  13. use the API, you'll first need to create a plugin from which you can
  14. access it. This change is done to avoid leakage of sensitive data to
  15. malicious or non-whitelisted scripts.
  16. The Converse.js API is broken up into different logical "groupings" (for
  17. example ``converse.plugins`` or ``converse.contacts``).
  18. There are some exceptions to this, like ``converse.initialize``, which aren't
  19. groupings but single methods.
  20. The groupings logically group methods, such as standardised accessors and
  21. mutators::
  22. .get
  23. .set
  24. .add
  25. .remove
  26. So for example, to get a contact, you would do the following::
  27. _converse.api.contacts.get('jid@example.com');
  28. To get multiple contacts, just pass in an array of jids::
  29. _converse.api.contacts.get(['jid1@example.com', 'jid2@example.com']);
  30. To get all contacts, simply call ``get`` without any jids::
  31. _converse.api.contacts.get();
  32. Public API methods
  33. ==================
  34. Publich API methods are those methods that are accessible on the global
  35. ``window.converse`` object. They are public, because any Javascript in the page
  36. can call them. Public methods therefore don't expose any sensitive or closured
  37. data. To do that, you'll need to create a plugin, which has access to the
  38. private API method.
  39. initialize
  40. ----------
  41. .. note:: This method is the one exception of a method which is not logically grouped as explained above.
  42. Publich API method which initializes converse.js.
  43. This method must always be called when using converse.js.
  44. The `initialize` method takes a map of :ref:`configuration-variables`.
  45. Example:
  46. .. code-block:: javascript
  47. converse.initialize({
  48. allow_otr: true,
  49. auto_list_rooms: false,
  50. auto_subscribe: false,
  51. bosh_service_url: 'https://bind.example.com',
  52. hide_muc_server: false,
  53. i18n: locales['en'],
  54. keepalive: true,
  55. play_sounds: true,
  56. prebind: false,
  57. show_controlbox_by_default: true,
  58. debug: false,
  59. roster_groups: true
  60. });
  61. The **plugin** grouping
  62. ------------------------
  63. Exposes methods for adding and removing plugins. You'll need to write a plugin
  64. if you want to have access to the private API methods defined further down below.
  65. For more information on plugins, read the section :ref:`writing-a-plugin`.
  66. add
  67. ~~~
  68. Registers a new plugin.
  69. .. code-block:: javascript
  70. var plugin = {
  71. initialize: function () {
  72. // method on any plugin (if it exists) as soon as the plugin has
  73. // been loaded.
  74. // Inside this method, you have access to the closured
  75. // _converse object, which contains the core logic and data
  76. // structures of converse.js
  77. }
  78. }
  79. converse.plugins.add('myplugin', plugin);
  80. remove
  81. ~~~~~~
  82. Removes a plugin from the registry.
  83. .. code-block:: javascript
  84. converse.plugins.remove('myplugin');
  85. Private API methods
  86. ===================
  87. The private API methods are only accessible via the closured ``_converse``
  88. object, which is only available to plugins.
  89. These methods are kept private (i.e. not global) because they may return
  90. sensitive data which should be kept off-limits to other 3rd-party scripts
  91. that might be running in the page.
  92. .. note:: The example code snippets shown below are a bit contrived. I've added
  93. the minimum plugin boilerplace around the actual example, to show that
  94. these API methods can only be called inside a plugin where the
  95. ``_converse`` object is available. However, sometimes other considerations
  96. need to be made as well. For example, for certain API methods it is
  97. necessary to first wait until the data has been received from the XMPP
  98. server (or from the browser's sessionStorage cache). Due to
  99. time-constriaints these limitations are ignored in the examples below. For
  100. a fuller picture, refer to the section :ref:`events-API` as well.
  101. send
  102. ----
  103. Allows you to send XML stanzas.
  104. For example, to send a message stanza:
  105. .. code-block:: javascript
  106. converse.plugins.add('myplugin', {
  107. initialize: function () {
  108. var msg = converse.env.$msg({
  109. from: 'juliet@example.com/balcony',
  110. to:'romeo@example.net',
  111. type:'chat'
  112. });
  113. this._converse.api.send(msg);
  114. }
  115. });
  116. .. _`waituntil-grouping`:
  117. waitUntil
  118. ---------
  119. This method can be used to wait for promises. Promises are similar to events
  120. (for event handling, refer to the :ref:`listen-grouping`), but they differ in
  121. two important ways:
  122. * A promise gets resolved only once, whereas events can fire multiple times.
  123. * A handler registered for a promise, will still fire *after* the promise has
  124. been resolved, which is not the case with an event handler.
  125. Converse.js has the following promises:
  126. * cachedRoster
  127. * chatBoxesFetched
  128. * connected
  129. * pluginsInitialized
  130. * roster
  131. * rosterContactsFetched
  132. * rosterGroupsFetched
  133. * rosterInitialized
  134. * statusInitialized
  135. * roomsPanelRendered (only via the `converse-muc` plugin)
  136. Below is an example from `converse-muc.js <https://github.com/jcbrand/converse.js/blob/master/src/converse-muc.js>`_
  137. where the `rosterContactsFetched` promise is waited on. The method
  138. `this.initInviteWidget` will initialize the chatroom invitation widget.
  139. .. code-block:: javascript
  140. _converse.api.waitUntil('rosterContactsFetched').then(this.initInviteWidget.bind(this));
  141. The line above executes only once a chatroom has been opened and entered, so
  142. using an event handler here would not work, since the event might have fired
  143. already by that time.
  144. The **archive** grouping
  145. ------------------------
  146. Converse.js supports the *Message Archive Management*
  147. (`XEP-0313 <https://xmpp.org/extensions/xep-0313.html>`_) protocol,
  148. through which it is able to query an XMPP server for archived messages.
  149. See also the **message_archiving** option in the :ref:`configuration-variables` section, which you'll usually
  150. want to in conjunction with this API.
  151. query
  152. ~~~~~
  153. The ``query`` method is used to query for archived messages.
  154. It accepts the following optional parameters:
  155. * **options** an object containing the query parameters. Valid query parameters
  156. are ``with``, ``start``, ``end``, ``first``, ``last``, ``after``, ``before``, ``index`` and ``count``.
  157. * **callback** is the callback method that will be called when all the messages
  158. have been received.
  159. * **errback** is the callback method to be called when an error is returned by
  160. the XMPP server, for example when it doesn't support message archiving.
  161. Examples
  162. ^^^^^^^^
  163. **Requesting all archived messages**
  164. The simplest query that can be made is to simply not pass in any parameters.
  165. Such a query will return all archived messages for the current user.
  166. Generally, you'll however always want to pass in a callback method, to receive
  167. the returned messages.
  168. .. code-block:: javascript
  169. converse.plugins.add('myplugin', {
  170. initialize: function () {
  171. var errback = function (iq) {
  172. // The query was not successful, perhaps inform the user?
  173. // The IQ stanza returned by the XMPP server is passed in, so that you
  174. // may inspect it and determine what the problem was.
  175. }
  176. var callback = function (messages) {
  177. // Do something with the messages, like showing them in your webpage.
  178. }
  179. this._converse.api.archive.query(callback, errback))
  180. }
  181. });
  182. **Waiting until server support has been determined**
  183. The query method will only work if converse.js has been able to determine that
  184. the server supports MAM queries, otherwise the following error will be raised:
  185. - *This server does not support XEP-0313, Message Archive Management*
  186. The very first time converse.js loads in a browser tab, if you call the query
  187. API too quickly, the above error might appear because service discovery has not
  188. yet been completed.
  189. To work solve this problem, you can first listen for the ``serviceDiscovered`` event,
  190. through which you can be informed once support for MAM has been determined.
  191. For example:
  192. .. code-block:: javascript
  193. converse.plugins.add('myplugin', {
  194. initialize: function () {
  195. var _converse = this._converse;
  196. _converse.api.listen.on('serviceDiscovered', function (feature) {
  197. if (feature.get('var') === converse.env.Strophe.NS.MAM) {
  198. _converse.api.archive.query()
  199. }
  200. });
  201. converse.listen.on('serviceDiscovered', function (feature) {
  202. if (feature.get('var') === converse.env.Strophe.NS.MAM) {
  203. converse.archive.query()
  204. }
  205. });
  206. **Requesting all archived messages for a particular contact or room**
  207. To query for messages sent between the current user and another user or room,
  208. the query options need to contain the the JID (Jabber ID) of the user or
  209. room under the ``with`` key.
  210. .. code-block:: javascript
  211. converse.plugins.add('myplugin', {
  212. initialize: function () {
  213. // For a particular user
  214. this._converse.api.archive.query({'with': 'john@doe.net'}, callback, errback);)
  215. // For a particular room
  216. this._converse.api.archive.query({'with': 'discuss@conference.doglovers.net'}, callback, errback);)
  217. }
  218. });
  219. **Requesting all archived messages before or after a certain date**
  220. The ``start`` and ``end`` parameters are used to query for messages
  221. within a certain timeframe. The passed in date values may either be ISO8601
  222. formatted date strings, or Javascript Date objects.
  223. .. code-block:: javascript
  224. converse.plugins.add('myplugin', {
  225. initialize: function () {
  226. var options = {
  227. 'with': 'john@doe.net',
  228. 'start': '2010-06-07T00:00:00Z',
  229. 'end': '2010-07-07T13:23:54Z'
  230. };
  231. this._converse.api.archive.query(options, callback, errback);
  232. }
  233. });
  234. **Limiting the amount of messages returned**
  235. The amount of returned messages may be limited with the ``max`` parameter.
  236. By default, the messages are returned from oldest to newest.
  237. .. code-block:: javascript
  238. converse.plugins.add('myplugin', {
  239. initialize: function () {
  240. // Return maximum 10 archived messages
  241. this._converse.api.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  242. }
  243. });
  244. **Paging forwards through a set of archived messages**
  245. When limiting the amount of messages returned per query, you might want to
  246. repeatedly make a further query to fetch the next batch of messages.
  247. To simplify this usecase for you, the callback method receives not only an array
  248. with the returned archived messages, but also a special RSM (*Result Set
  249. Management*) object which contains the query parameters you passed in, as well
  250. as two utility methods ``next``, and ``previous``.
  251. When you call one of these utility methods on the returned RSM object, and then
  252. pass the result into a new query, you'll receive the next or previous batch of
  253. archived messages. Please note, when calling these methods, pass in an integer
  254. to limit your results.
  255. .. code-block:: javascript
  256. converse.plugins.add('myplugin', {
  257. initialize: function () {
  258. var _converse = this._converse;
  259. var callback = function (messages, rsm) {
  260. // Do something with the messages, like showing them in your webpage.
  261. // ...
  262. // You can now use the returned "rsm" object, to fetch the next batch of messages:
  263. _converse.api.archive.query(rsm.next(10), callback, errback))
  264. }
  265. _converse.api.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  266. }
  267. });
  268. **Paging backwards through a set of archived messages**
  269. To page backwards through the archive, you need to know the UID of the message
  270. which you'd like to page backwards from and then pass that as value for the
  271. ``before`` parameter. If you simply want to page backwards from the most recent
  272. message, pass in the ``before`` parameter with an empty string value ``''``.
  273. .. code-block:: javascript
  274. converse.plugins.add('myplugin', {
  275. initialize: function () {
  276. var _converse = this._converse;
  277. _converse.api.archive.query({'before': '', 'max':5}, function (message, rsm) {
  278. // Do something with the messages, like showing them in your webpage.
  279. // ...
  280. // You can now use the returned "rsm" object, to fetch the previous batch of messages:
  281. rsm.previous(5); // Call previous method, to update the object's parameters,
  282. // passing in a limit value of 5.
  283. // Now we query again, to get the previous batch.
  284. _converse.api.archive.query(rsm, callback, errback);
  285. }
  286. }
  287. });
  288. The **connection** grouping
  289. ---------------------------
  290. This grouping collects API functions related to the XMPP connection.
  291. connected
  292. ~~~~~~~~~
  293. A boolean attribute (i.e. not a callable) which is set to `true` or `false` depending
  294. on whether there is an established connection.
  295. disconnect
  296. ~~~~~~~~~~
  297. Terminates the connection.
  298. The **user** grouping
  299. ---------------------
  300. This grouping collects API functions related to the current logged in user.
  301. jid
  302. ~~~
  303. Return's the current user's full JID (Jabber ID).
  304. .. code-block:: javascript
  305. converse.plugins.add('myplugin', {
  306. initialize: function () {
  307. alert(this._converse.api.user.jid());
  308. }
  309. });
  310. login
  311. ~~~~~
  312. Logs the user in. This method can accept a map with the credentials, like this:
  313. .. code-block:: javascript
  314. converse.plugins.add('myplugin', {
  315. initialize: function () {
  316. this._converse.api.user.login({
  317. 'jid': 'dummy@example.com',
  318. 'password': 'secret'
  319. });
  320. }
  321. });
  322. or it can be called without any parameters, in which case converse.js will try
  323. to log the user in by calling the `prebind_url` or `credentials_url` depending
  324. on whether prebinding is used or not.
  325. logout
  326. ~~~~~~
  327. Log the user out of the current XMPP session.
  328. .. code-block:: javascript
  329. converse.plugins.add('myplugin', {
  330. initialize: function () {
  331. this._converse.api.user.logout();
  332. }
  333. });
  334. The **status** sub-grouping
  335. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  336. Set and get the user's chat status, also called their *availability*.
  337. get
  338. ^^^
  339. Return the current user's availability status:
  340. .. code-block:: javascript
  341. converse.plugins.add('myplugin', {
  342. initialize: function () {
  343. alert(this._converse.api.user.status.get()); // For example "dnd"
  344. }
  345. });
  346. set
  347. ^^^
  348. The user's status can be set to one of the following values:
  349. * **away**
  350. * **dnd**
  351. * **offline**
  352. * **online**
  353. * **unavailable**
  354. * **xa**
  355. For example:
  356. .. code-block:: javascript
  357. converse.plugins.add('myplugin', {
  358. initialize: function () {
  359. this._converse.api.user.status.set('dnd');
  360. }
  361. });
  362. Because the user's availability is often set together with a custom status
  363. message, this method also allows you to pass in a status message as a
  364. second parameter:
  365. .. code-block:: javascript
  366. converse.plugins.add('myplugin', {
  367. initialize: function () {
  368. this._converse.api.user.status.set('dnd', 'In a meeting');
  369. }
  370. });
  371. The **message** sub-grouping
  372. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  373. The ``user.status.message`` sub-grouping exposes methods for setting and
  374. retrieving the user's custom status message.
  375. .. code-block:: javascript
  376. converse.plugins.add('myplugin', {
  377. initialize: function () {
  378. this._converse.api.user.status.message.set('In a meeting');
  379. // Returns "In a meeting"
  380. return this._converse.api.user.status.message.get();
  381. }
  382. });
  383. The **contacts** grouping
  384. -------------------------
  385. get
  386. ~~~
  387. This method is used to retrieve roster contacts.
  388. To get a single roster contact, call the method with the contact's JID (Jabber ID):
  389. .. code-block:: javascript
  390. converse.plugins.add('myplugin', {
  391. initialize: function () {
  392. var _converse = this._converse;
  393. _converse.api.listen.on('rosterContactsFetched', function () {
  394. var contact = _converse.api.contacts.get('buddy@example.com')
  395. });
  396. }
  397. });
  398. To get multiple contacts, pass in an array of JIDs:
  399. .. code-block:: javascript
  400. converse.plugins.add('myplugin', {
  401. initialize: function () {
  402. var _converse = this._converse;
  403. _converse.api.listen.on('rosterContactsFetched', function () {
  404. var contacts = _converse.api.contacts.get(
  405. ['buddy1@example.com', 'buddy2@example.com']
  406. )
  407. });
  408. }
  409. });
  410. To return all contacts, simply call ``get`` without any parameters:
  411. .. code-block:: javascript
  412. converse.plugins.add('myplugin', {
  413. initialize: function () {
  414. var _converse = this._converse;
  415. _converse.api.listen.on('rosterContactsFetched', function () {
  416. var contacts = _converse.api.contacts.get();
  417. });
  418. }
  419. });
  420. The returned roster contact objects have these attributes:
  421. +----------------+-----------------------------------------------------------------------------------------------------------------+
  422. | Attribute | |
  423. +================+=================================================================================================================+
  424. | ask | If ask === 'subscribe', then we have asked this person to be our chat buddy. |
  425. +----------------+-----------------------------------------------------------------------------------------------------------------+
  426. | fullname | The person's full name. |
  427. +----------------+-----------------------------------------------------------------------------------------------------------------+
  428. | jid | The person's Jabber/XMPP username. |
  429. +----------------+-----------------------------------------------------------------------------------------------------------------+
  430. | requesting | If true, then this person is asking to be our chat buddy. |
  431. +----------------+-----------------------------------------------------------------------------------------------------------------+
  432. | subscription | The subscription state between the current user and this chat buddy. Can be `none`, `to`, `from` or `both`. |
  433. +----------------+-----------------------------------------------------------------------------------------------------------------+
  434. | id | A unique id, same as the jid. |
  435. +----------------+-----------------------------------------------------------------------------------------------------------------+
  436. | chat_status | The person's chat status. Can be `online`, `offline`, `busy`, `xa` (extended away) or `away`. |
  437. +----------------+-----------------------------------------------------------------------------------------------------------------+
  438. | user_id | The user id part of the JID (the part before the `@`). |
  439. +----------------+-----------------------------------------------------------------------------------------------------------------+
  440. | resources | The known resources for this chat buddy. Each resource denotes a separate and connected chat client. |
  441. +----------------+-----------------------------------------------------------------------------------------------------------------+
  442. | groups | The roster groups in which this chat buddy was placed. |
  443. +----------------+-----------------------------------------------------------------------------------------------------------------+
  444. | status | Their human readable custom status message. |
  445. +----------------+-----------------------------------------------------------------------------------------------------------------+
  446. | image_type | The image's file type. |
  447. +----------------+-----------------------------------------------------------------------------------------------------------------+
  448. | image | The Base64 encoded image data. |
  449. +----------------+-----------------------------------------------------------------------------------------------------------------+
  450. | url | The buddy's website URL, as specified in their VCard data. |
  451. +----------------+-----------------------------------------------------------------------------------------------------------------+
  452. | vcard_updated | When last the buddy's VCard was updated. |
  453. +----------------+-----------------------------------------------------------------------------------------------------------------+
  454. add
  455. ~~~
  456. Add a contact.
  457. Provide the JID of the contact you want to add:
  458. .. code-block:: javascript
  459. _converse.api.contacts.add('buddy@example.com')
  460. You may also provide the fullname. If not present, we use the jid as fullname:
  461. .. code-block:: javascript
  462. _converse.api.contacts.add('buddy@example.com', 'Buddy')
  463. The **chats** grouping
  464. ----------------------
  465. Note, for MUC chat rooms, you need to use the "rooms" grouping instead.
  466. get
  467. ~~~
  468. Returns an object representing a chat box.
  469. To return a single chat box, provide the JID of the contact you're chatting
  470. with in that chat box:
  471. .. code-block:: javascript
  472. _converse.api.chats.get('buddy@example.com')
  473. To return an array of chat boxes, provide an array of JIDs:
  474. .. code-block:: javascript
  475. _converse.api.chats.get(['buddy1@example.com', 'buddy2@example.com'])
  476. To return all open chat boxes, call the method without any JIDs::
  477. _converse.api.chats.get()
  478. open
  479. ~~~~
  480. Opens a chat box and returns a Backbone.View object representing a chat box.
  481. To open a single chat box, provide the JID of the contact:
  482. .. code-block:: javascript
  483. converse.plugins.add('myplugin', {
  484. initialize: function () {
  485. this._converse.api.chats.open('buddy@example.com')
  486. }
  487. });
  488. To return an array of chat boxes, provide an array of JIDs:
  489. .. code-block:: javascript
  490. converse.plugins.add('myplugin', {
  491. initialize: function () {
  492. this._converse.api.chats.open(['buddy1@example.com', 'buddy2@example.com'])
  493. }
  494. });
  495. *The returned chat box object contains the following methods:*
  496. +-------------------+------------------------------------------+
  497. | Method | Description |
  498. +===================+==========================================+
  499. | close | Close the chat box. |
  500. +-------------------+------------------------------------------+
  501. | focus | Focuses the chat box textarea |
  502. +-------------------+------------------------------------------+
  503. | model.endOTR | End an OTR (Off-the-record) session. |
  504. +-------------------+------------------------------------------+
  505. | model.get | Get an attribute (i.e. accessor). |
  506. +-------------------+------------------------------------------+
  507. | model.initiateOTR | Start an OTR (off-the-record) session. |
  508. +-------------------+------------------------------------------+
  509. | model.maximize | Minimize the chat box. |
  510. +-------------------+------------------------------------------+
  511. | model.minimize | Maximize the chat box. |
  512. +-------------------+------------------------------------------+
  513. | model.set | Set an attribute (i.e. mutator). |
  514. +-------------------+------------------------------------------+
  515. | show | Opens/shows the chat box. |
  516. +-------------------+------------------------------------------+
  517. *The get and set methods can be used to retrieve and change the following attributes:*
  518. +-------------+-----------------------------------------------------+
  519. | Attribute | Description |
  520. +=============+=====================================================+
  521. | height | The height of the chat box. |
  522. +-------------+-----------------------------------------------------+
  523. | url | The URL of the chat box heading. |
  524. +-------------+-----------------------------------------------------+
  525. The **rooms** grouping
  526. ----------------------
  527. get
  528. ~~~
  529. Returns an object representing a multi user chat box (room).
  530. It takes 3 parameters:
  531. * the room JID (if not specified, all rooms will be returned).
  532. * a map (object) containing any extra room attributes For example, if you want
  533. to specify the nickname, use ``{'nick': 'bloodninja'}``. Previously (before
  534. version 1.0.7, the second parameter only accepted the nickname (as a string
  535. value). This is currently still accepted, but then you can't pass in any
  536. other room attributes. If the nickname is not specified then the node part of
  537. the user's JID will be used.
  538. * a boolean, indicating whether the room should be created if not found (default: `false`)
  539. .. code-block:: javascript
  540. converse.plugins.add('myplugin', {
  541. initialize: function () {
  542. var nick = 'dread-pirate-roberts';
  543. var create_if_not_found = true;
  544. this._converse.api.rooms.open(
  545. 'group@muc.example.com',
  546. {'nick': nick},
  547. create_if_not_found
  548. )
  549. }
  550. });
  551. open
  552. ~~~~
  553. Opens a multi user chat box and returns an object representing it.
  554. Similar to the ``chats.get`` API.
  555. It takes 2 parameters:
  556. * The room JID or JIDs (if not specified, all currently open rooms will be returned).
  557. * A map (object) containing any extra room attributes. For example, if you want
  558. to specify the nickname, use ``{'nick': 'bloodninja'}``.
  559. To open a single multi user chat box, provide the JID of the room:
  560. .. code-block:: javascript
  561. converse.plugins.add('myplugin', {
  562. initialize: function () {
  563. this._converse.api.rooms.open('group@muc.example.com')
  564. }
  565. });
  566. To return an array of rooms, provide an array of room JIDs:
  567. .. code-block:: javascript
  568. converse.plugins.add('myplugin', {
  569. initialize: function () {
  570. this._converse.api.rooms.open(['group1@muc.example.com', 'group2@muc.example.com'])
  571. }
  572. });
  573. To setup a custom nickname when joining the room, provide the optional nick argument:
  574. .. code-block:: javascript
  575. converse.plugins.add('myplugin', {
  576. initialize: function () {
  577. this._converse.api.rooms.open('group@muc.example.com', {'nick': 'mycustomnick'})
  578. }
  579. });
  580. Room attributes that may be passed in:
  581. * *nick*: The nickname to be used
  582. * *auto_configure*: A boolean, indicating whether the room should be configured
  583. automatically or not. If set to ``true``, then it makes sense to pass in
  584. configuration settings.
  585. * *roomconfig*: A map of configuration settings to be used when the room gets
  586. configured automatically. Currently it doesn't make sense to specify
  587. ``roomconfig`` values if ``auto_configure`` is set to ``false``.
  588. For a list of configuration values that can be passed in, refer to these values
  589. in the `XEP-0045 MUC specification <http://xmpp.org/extensions/xep-0045.html#registrar-formtype-owner>`_.
  590. The values should be named without the ``muc#roomconfig_`` prefix.
  591. * *maximize*: A boolean, indicating whether minimized rooms should also be
  592. maximized, when opened. Set to ``false`` by default.
  593. For example, opening a room with a specific default configuration:
  594. .. code-block:: javascript
  595. converse.plugins.add('myplugin', {
  596. initialize: function () {
  597. this._converse.api.rooms.open(
  598. 'myroom@conference.example.org',
  599. { 'nick': 'coolguy69',
  600. 'auto_configure': true,
  601. 'roomconfig': {
  602. 'changesubject': false,
  603. 'membersonly': true,
  604. 'persistentroom': true,
  605. 'publicroom': true,
  606. 'roomdesc': 'Comfy room for hanging out',
  607. 'whois': 'anyone'
  608. }
  609. },
  610. true
  611. );
  612. }
  613. });
  614. .. note:: `multi-list` configuration values are not yet supported.
  615. close
  616. ~~~~~
  617. Lets you close open chat rooms. You can call this method without any arguments
  618. to close all open chat rooms, or you can specify a single JID or an array of
  619. JIDs.
  620. The **settings** grouping
  621. -------------------------
  622. This grouping allows you to get or set the configuration settings of converse.js.
  623. get(key)
  624. ~~~~~~~~
  625. Returns the value of a configuration settings. For example:
  626. .. code-block:: javascript
  627. converse.plugins.add('myplugin', {
  628. initialize: function () {
  629. // default value would be false;
  630. alert(this._converse.api.settings.get("play_sounds"));
  631. }
  632. });
  633. set(key, value) or set(object)
  634. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  635. Set one or many configuration settings. For example:
  636. .. code-block:: javascript
  637. converse.plugins.add('myplugin', {
  638. initialize: function () {
  639. this._converse.api.settings.set("play_sounds", true);
  640. }
  641. });
  642. or :
  643. .. code-block:: javascript
  644. converse.plugins.add('myplugin', {
  645. initialize: function () {
  646. this._converse.api.settings.set({
  647. "play_sounds", true,
  648. "hide_offline_users" true
  649. });
  650. }
  651. });
  652. Note, this is not an alternative to calling ``converse.initialize``, which still needs
  653. to be called. Generally, you'd use this method after converse.js is already
  654. running and you want to change the configuration on-the-fly.
  655. The **tokens** grouping
  656. -----------------------
  657. get
  658. ~~~
  659. Returns a token, either the RID or SID token depending on what's asked for.
  660. Example:
  661. .. code-block:: javascript
  662. converse.plugins.add('myplugin', {
  663. initialize: function () {
  664. alert(this._converse.api.tokens.get('rid'));
  665. }
  666. });
  667. .. _`listen-grouping`:
  668. The **listen** grouping
  669. -----------------------
  670. Converse.js emits events to which you can subscribe from your own Javascript.
  671. Concerning events, the following methods are available under the "listen"
  672. grouping:
  673. * **on(eventName, callback, [context])**:
  674. Calling the ``on`` method allows you to subscribe to an event.
  675. Every time the event fires, the callback method specified by ``callback`` will be
  676. called.
  677. Parameters:
  678. * ``eventName`` is the event name as a string.
  679. * ``callback`` is the callback method to be called when the event is emitted.
  680. * ``context`` (optional), the value of the `this` parameter for the callback.
  681. For example:
  682. .. code-block:: javascript
  683. _converse.api.listen.on('message', function (messageXML) { ... });
  684. * **once(eventName, callback, [context])**:
  685. Calling the ``once`` method allows you to listen to an event
  686. exactly once.
  687. Parameters:
  688. * ``eventName`` is the event name as a string.
  689. * ``callback`` is the callback method to be called when the event is emitted.
  690. * ``context`` (optional), the value of the `this` parameter for the callback.
  691. For example:
  692. .. code-block:: javascript
  693. _converse.api.listen.once('message', function (messageXML) { ... });
  694. * **not(eventName, callback)**
  695. To stop listening to an event, you can use the ``not`` method.
  696. Parameters:
  697. * ``eventName`` is the event name as a string.
  698. * ``callback`` refers to the function that is to be no longer executed.
  699. For example:
  700. .. code-block:: javascript
  701. _converse.api.listen.not('message', function (messageXML) { ... });