developer_api.rst 35 KB

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