developer_api.rst 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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.contacts.get('jid@example.com');
  28. To get multiple contacts, just pass in an array of jids::
  29. _converse.contacts.get(['jid1@example.com', 'jid2@example.com']);
  30. To get all contacts, simply call ``get`` without any jids::
  31. _converse.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.send(msg);
  114. }
  115. });
  116. The **archive** grouping
  117. ------------------------
  118. Converse.js supports the *Message Archive Management*
  119. (`XEP-0313 <https://xmpp.org/extensions/xep-0313.html>`_) protocol,
  120. through which it is able to query an XMPP server for archived messages.
  121. See also the **message_archiving** option in the :ref:`configuration-variables` section, which you'll usually
  122. want to in conjunction with this API.
  123. query
  124. ~~~~~
  125. The ``query`` method is used to query for archived messages.
  126. It accepts the following optional parameters:
  127. * **options** an object containing the query parameters. Valid query parameters
  128. are ``with``, ``start``, ``end``, ``first``, ``last``, ``after``, ``before``, ``index`` and ``count``.
  129. * **callback** is the callback method that will be called when all the messages
  130. have been received.
  131. * **errback** is the callback method to be called when an error is returned by
  132. the XMPP server, for example when it doesn't support message archiving.
  133. Examples
  134. ^^^^^^^^
  135. **Requesting all archived messages**
  136. The simplest query that can be made is to simply not pass in any parameters.
  137. Such a query will return all archived messages for the current user.
  138. Generally, you'll however always want to pass in a callback method, to receive
  139. the returned messages.
  140. .. code-block:: javascript
  141. converse.plugins.add('myplugin', {
  142. initialize: function () {
  143. var errback = function (iq) {
  144. // The query was not successful, perhaps inform the user?
  145. // The IQ stanza returned by the XMPP server is passed in, so that you
  146. // may inspect it and determine what the problem was.
  147. }
  148. var callback = function (messages) {
  149. // Do something with the messages, like showing them in your webpage.
  150. }
  151. this._converse.archive.query(callback, errback))
  152. }
  153. });
  154. **Waiting until server support has been determined**
  155. The query method will only work if converse.js has been able to determine that
  156. the server supports MAM queries, otherwise the following error will be raised:
  157. - *This server does not support XEP-0313, Message Archive Management*
  158. The very first time converse.js loads in a browser tab, if you call the query
  159. API too quickly, the above error might appear because service discovery has not
  160. yet been completed.
  161. To work solve this problem, you can first listen for the ``serviceDiscovered`` event,
  162. through which you can be informed once support for MAM has been determined.
  163. For example:
  164. .. code-block:: javascript
  165. converse.plugins.add('myplugin', {
  166. initialize: function () {
  167. var _converse = this._converse;
  168. _converse.listen.on('serviceDiscovered', function (feature) {
  169. if (feature.get('var') === converse.env.Strophe.NS.MAM) {
  170. _converse.archive.query()
  171. }
  172. });
  173. converse.listen.on('serviceDiscovered', function (feature) {
  174. if (feature.get('var') === converse.env.Strophe.NS.MAM) {
  175. converse.archive.query()
  176. }
  177. });
  178. **Requesting all archived messages for a particular contact or room**
  179. To query for messages sent between the current user and another user or room,
  180. the query options need to contain the the JID (Jabber ID) of the user or
  181. room under the ``with`` key.
  182. .. code-block:: javascript
  183. converse.plugins.add('myplugin', {
  184. initialize: function () {
  185. // For a particular user
  186. this._converse.archive.query({'with': 'john@doe.net'}, callback, errback);)
  187. // For a particular room
  188. this._converse.archive.query({'with': 'discuss@conference.doglovers.net'}, callback, errback);)
  189. }
  190. });
  191. **Requesting all archived messages before or after a certain date**
  192. The ``start`` and ``end`` parameters are used to query for messages
  193. within a certain timeframe. The passed in date values may either be ISO8601
  194. formatted date strings, or Javascript Date objects.
  195. .. code-block:: javascript
  196. converse.plugins.add('myplugin', {
  197. initialize: function () {
  198. var options = {
  199. 'with': 'john@doe.net',
  200. 'start': '2010-06-07T00:00:00Z',
  201. 'end': '2010-07-07T13:23:54Z'
  202. };
  203. this._converse.archive.query(options, callback, errback);
  204. }
  205. });
  206. **Limiting the amount of messages returned**
  207. The amount of returned messages may be limited with the ``max`` parameter.
  208. By default, the messages are returned from oldest to newest.
  209. .. code-block:: javascript
  210. converse.plugins.add('myplugin', {
  211. initialize: function () {
  212. // Return maximum 10 archived messages
  213. this._converse.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  214. }
  215. });
  216. **Paging forwards through a set of archived messages**
  217. When limiting the amount of messages returned per query, you might want to
  218. repeatedly make a further query to fetch the next batch of messages.
  219. To simplify this usecase for you, the callback method receives not only an array
  220. with the returned archived messages, but also a special RSM (*Result Set
  221. Management*) object which contains the query parameters you passed in, as well
  222. as two utility methods ``next``, and ``previous``.
  223. When you call one of these utility methods on the returned RSM object, and then
  224. pass the result into a new query, you'll receive the next or previous batch of
  225. archived messages. Please note, when calling these methods, pass in an integer
  226. to limit your results.
  227. .. code-block:: javascript
  228. converse.plugins.add('myplugin', {
  229. initialize: function () {
  230. var _converse = this._converse;
  231. var callback = function (messages, rsm) {
  232. // Do something with the messages, like showing them in your webpage.
  233. // ...
  234. // You can now use the returned "rsm" object, to fetch the next batch of messages:
  235. _converse.archive.query(rsm.next(10), callback, errback))
  236. }
  237. _converse.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  238. }
  239. });
  240. **Paging backwards through a set of archived messages**
  241. To page backwards through the archive, you need to know the UID of the message
  242. which you'd like to page backwards from and then pass that as value for the
  243. ``before`` parameter. If you simply want to page backwards from the most recent
  244. message, pass in the ``before`` parameter with an empty string value ``''``.
  245. .. code-block:: javascript
  246. converse.plugins.add('myplugin', {
  247. initialize: function () {
  248. var _converse = this._converse;
  249. _converse.archive.query({'before': '', 'max':5}, function (message, rsm) {
  250. // Do something with the messages, like showing them in your webpage.
  251. // ...
  252. // You can now use the returned "rsm" object, to fetch the previous batch of messages:
  253. rsm.previous(5); // Call previous method, to update the object's parameters,
  254. // passing in a limit value of 5.
  255. // Now we query again, to get the previous batch.
  256. _converse.archive.query(rsm, callback, errback);
  257. }
  258. }
  259. });
  260. The **connection** grouping
  261. ---------------------------
  262. This grouping collects API functions related to the XMPP connection.
  263. connected
  264. ~~~~~~~~~
  265. A boolean attribute (i.e. not a callable) which is set to `true` or `false` depending
  266. on whether there is an established connection.
  267. disconnect
  268. ~~~~~~~~~~
  269. Terminates the connection.
  270. The **user** grouping
  271. ---------------------
  272. This grouping collects API functions related to the current logged in user.
  273. jid
  274. ~~~
  275. Return's the current user's full JID (Jabber ID).
  276. .. code-block:: javascript
  277. converse.plugins.add('myplugin', {
  278. initialize: function () {
  279. alert(this._converse.user.jid());
  280. }
  281. });
  282. login
  283. ~~~~~
  284. Logs the user in. This method can accept a map with the credentials, like this:
  285. .. code-block:: javascript
  286. converse.plugins.add('myplugin', {
  287. initialize: function () {
  288. this._converse.user.login({
  289. 'jid': 'dummy@example.com',
  290. 'password': 'secret'
  291. });
  292. }
  293. });
  294. or it can be called without any parameters, in which case converse.js will try
  295. to log the user in by calling the `prebind_url` or `credentials_url` depending
  296. on whether prebinding is used or not.
  297. logout
  298. ~~~~~~
  299. Log the user out of the current XMPP session.
  300. .. code-block:: javascript
  301. converse.plugins.add('myplugin', {
  302. initialize: function () {
  303. this._converse.user.logout();
  304. }
  305. });
  306. The **status** sub-grouping
  307. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  308. Set and get the user's chat status, also called their *availability*.
  309. get
  310. ^^^
  311. Return the current user's availability status:
  312. .. code-block:: javascript
  313. converse.plugins.add('myplugin', {
  314. initialize: function () {
  315. alert(this._converse.user.status.get()); // For example "dnd"
  316. }
  317. });
  318. set
  319. ^^^
  320. The user's status can be set to one of the following values:
  321. * **away**
  322. * **dnd**
  323. * **offline**
  324. * **online**
  325. * **unavailable**
  326. * **xa**
  327. For example:
  328. .. code-block:: javascript
  329. converse.plugins.add('myplugin', {
  330. initialize: function () {
  331. this._converse.user.status.set('dnd');
  332. }
  333. });
  334. Because the user's availability is often set together with a custom status
  335. message, this method also allows you to pass in a status message as a
  336. second parameter:
  337. .. code-block:: javascript
  338. converse.plugins.add('myplugin', {
  339. initialize: function () {
  340. this._converse.user.status.set('dnd', 'In a meeting');
  341. }
  342. });
  343. The **message** sub-grouping
  344. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  345. The ``user.status.message`` sub-grouping exposes methods for setting and
  346. retrieving the user's custom status message.
  347. .. code-block:: javascript
  348. converse.plugins.add('myplugin', {
  349. initialize: function () {
  350. this._converse.user.status.message.set('In a meeting');
  351. // Returns "In a meeting"
  352. return this._converse.user.status.message.get();
  353. }
  354. });
  355. The **contacts** grouping
  356. -------------------------
  357. get
  358. ~~~
  359. This method is used to retrieve roster contacts.
  360. To get a single roster contact, call the method with the contact's JID (Jabber ID):
  361. .. code-block:: javascript
  362. converse.plugins.add('myplugin', {
  363. initialize: function () {
  364. var _converse = this._converse;
  365. _converse.on('rosterContactsFetched', function () {
  366. var contact = _converse.contacts.get('buddy@example.com')
  367. });
  368. }
  369. });
  370. To get multiple contacts, pass in an array of JIDs:
  371. .. code-block:: javascript
  372. converse.plugins.add('myplugin', {
  373. initialize: function () {
  374. var _converse = this._converse;
  375. _converse.on('rosterContactsFetched', function () {
  376. var contacts = _converse.contacts.get(
  377. ['buddy1@example.com', 'buddy2@example.com']
  378. )
  379. });
  380. }
  381. });
  382. To return all contacts, simply call ``get`` without any parameters:
  383. .. code-block:: javascript
  384. converse.plugins.add('myplugin', {
  385. initialize: function () {
  386. var _converse = this._converse;
  387. _converse.on('rosterContactsFetched', function () {
  388. var contacts = _converse.contacts.get();
  389. });
  390. }
  391. });
  392. The returned roster contact objects have these attributes:
  393. +----------------+-----------------------------------------------------------------------------------------------------------------+
  394. | Attribute | |
  395. +================+=================================================================================================================+
  396. | ask | If ask === 'subscribe', then we have asked this person to be our chat buddy. |
  397. +----------------+-----------------------------------------------------------------------------------------------------------------+
  398. | fullname | The person's full name. |
  399. +----------------+-----------------------------------------------------------------------------------------------------------------+
  400. | jid | The person's Jabber/XMPP username. |
  401. +----------------+-----------------------------------------------------------------------------------------------------------------+
  402. | requesting | If true, then this person is asking to be our chat buddy. |
  403. +----------------+-----------------------------------------------------------------------------------------------------------------+
  404. | subscription | The subscription state between the current user and this chat buddy. Can be `none`, `to`, `from` or `both`. |
  405. +----------------+-----------------------------------------------------------------------------------------------------------------+
  406. | id | A unique id, same as the jid. |
  407. +----------------+-----------------------------------------------------------------------------------------------------------------+
  408. | chat_status | The person's chat status. Can be `online`, `offline`, `busy`, `xa` (extended away) or `away`. |
  409. +----------------+-----------------------------------------------------------------------------------------------------------------+
  410. | user_id | The user id part of the JID (the part before the `@`). |
  411. +----------------+-----------------------------------------------------------------------------------------------------------------+
  412. | resources | The known resources for this chat buddy. Each resource denotes a separate and connected chat client. |
  413. +----------------+-----------------------------------------------------------------------------------------------------------------+
  414. | groups | The roster groups in which this chat buddy was placed. |
  415. +----------------+-----------------------------------------------------------------------------------------------------------------+
  416. | status | Their human readable custom status message. |
  417. +----------------+-----------------------------------------------------------------------------------------------------------------+
  418. | image_type | The image's file type. |
  419. +----------------+-----------------------------------------------------------------------------------------------------------------+
  420. | image | The Base64 encoded image data. |
  421. +----------------+-----------------------------------------------------------------------------------------------------------------+
  422. | url | The buddy's website URL, as specified in their VCard data. |
  423. +----------------+-----------------------------------------------------------------------------------------------------------------+
  424. | vcard_updated | When last the buddy's VCard was updated. |
  425. +----------------+-----------------------------------------------------------------------------------------------------------------+
  426. add
  427. ~~~
  428. Add a contact.
  429. Provide the JID of the contact you want to add:
  430. .. code-block:: javascript
  431. _converse.contacts.add('buddy@example.com')
  432. You may also provide the fullname. If not present, we use the jid as fullname:
  433. .. code-block:: javascript
  434. _converse.contacts.add('buddy@example.com', 'Buddy')
  435. The **chats** grouping
  436. ----------------------
  437. Note, for MUC chat rooms, you need to use the "rooms" grouping instead.
  438. get
  439. ~~~
  440. Returns an object representing a chat box.
  441. To return a single chat box, provide the JID of the contact you're chatting
  442. with in that chat box:
  443. .. code-block:: javascript
  444. _converse.chats.get('buddy@example.com')
  445. To return an array of chat boxes, provide an array of JIDs:
  446. .. code-block:: javascript
  447. _converse.chats.get(['buddy1@example.com', 'buddy2@example.com'])
  448. To return all open chat boxes, call the method without any JIDs::
  449. _converse.chats.get()
  450. open
  451. ~~~~
  452. Opens a chat box and returns an object representing a chat box.
  453. To open a single chat box, provide the JID of the contact:
  454. .. code-block:: javascript
  455. converse.plugins.add('myplugin', {
  456. initialize: function () {
  457. this._converse.chats.open('buddy@example.com')
  458. }
  459. });
  460. To return an array of chat boxes, provide an array of JIDs:
  461. .. code-block:: javascript
  462. converse.plugins.add('myplugin', {
  463. initialize: function () {
  464. this._converse.chats.open(['buddy1@example.com', 'buddy2@example.com'])
  465. }
  466. });
  467. *The returned chat box object contains the following methods:*
  468. +-------------+------------------------------------------+
  469. | Method | Description |
  470. +=============+==========================================+
  471. | endOTR | End an OTR (Off-the-record) session. |
  472. +-------------+------------------------------------------+
  473. | get | Get an attribute (i.e. accessor). |
  474. +-------------+------------------------------------------+
  475. | initiateOTR | Start an OTR (off-the-record) session. |
  476. +-------------+------------------------------------------+
  477. | maximize | Minimize the chat box. |
  478. +-------------+------------------------------------------+
  479. | minimize | Maximize the chat box. |
  480. +-------------+------------------------------------------+
  481. | set | Set an attribute (i.e. mutator). |
  482. +-------------+------------------------------------------+
  483. | close | Close the chat box. |
  484. +-------------+------------------------------------------+
  485. | open | Opens the chat box. |
  486. +-------------+------------------------------------------+
  487. *The get and set methods can be used to retrieve and change the following attributes:*
  488. +-------------+-----------------------------------------------------+
  489. | Attribute | Description |
  490. +=============+=====================================================+
  491. | height | The height of the chat box. |
  492. +-------------+-----------------------------------------------------+
  493. | url | The URL of the chat box heading. |
  494. +-------------+-----------------------------------------------------+
  495. The **rooms** grouping
  496. ----------------------
  497. get
  498. ~~~
  499. Returns an object representing a multi user chat box (room).
  500. It takes 3 parameters:
  501. * the room JID (if not specified, all rooms will be returned).
  502. * a map (object) containing any extra room attributes For example, if you want
  503. to specify the nickname, use ``{'nick': 'bloodninja'}``. Previously (before
  504. version 1.0.7, the second parameter only accepted the nickname (as a string
  505. value). This is currently still accepted, but then you can't pass in any
  506. other room attributes. If the nickname is not specified then the node part of
  507. the user's JID will be used.
  508. * a boolean, indicating whether the room should be created if not found (default: `false`)
  509. .. code-block:: javascript
  510. converse.plugins.add('myplugin', {
  511. initialize: function () {
  512. var nick = 'dread-pirate-roberts';
  513. var create_if_not_found = true;
  514. this._converse.rooms.open(
  515. 'group@muc.example.com',
  516. {'nick': nick},
  517. create_if_not_found
  518. )
  519. }
  520. });
  521. open
  522. ~~~~
  523. Opens a multi user chat box and returns an object representing it.
  524. Similar to the ``chats.get`` API.
  525. It takes 2 parameters:
  526. * The room JID or JIDs (if not specified, all currently open rooms will be returned).
  527. * A map (object) containing any extra room attributes. For example, if you want
  528. to specify the nickname, use ``{'nick': 'bloodninja'}``.
  529. To open a single multi user chat box, provide the JID of the room:
  530. .. code-block:: javascript
  531. converse.plugins.add('myplugin', {
  532. initialize: function () {
  533. this._converse.rooms.open('group@muc.example.com')
  534. }
  535. });
  536. To return an array of rooms, provide an array of room JIDs:
  537. .. code-block:: javascript
  538. converse.plugins.add('myplugin', {
  539. initialize: function () {
  540. this._converse.rooms.open(['group1@muc.example.com', 'group2@muc.example.com'])
  541. }
  542. });
  543. To setup a custom nickname when joining the room, provide the optional nick argument:
  544. .. code-block:: javascript
  545. converse.plugins.add('myplugin', {
  546. initialize: function () {
  547. this._converse.rooms.open('group@muc.example.com', {'nick': 'mycustomnick'})
  548. }
  549. });
  550. Room attributes that may be passed in:
  551. * *nick*: The nickname to be used
  552. * *auto_configure*: A boolean, indicating whether the room should be configured
  553. automatically or not. If set to ``true``, then it makes sense to pass in
  554. configuration settings.
  555. * *roomconfig*: A map of configuration settings to be used when the room gets
  556. configured automatically. Currently it doesn't make sense to specify
  557. ``roomconfig`` values if ``auto_configure`` is set to ``false``.
  558. For a list of configuration values that can be passed in, refer to these values
  559. in the `XEP-0045 MUC specification <http://xmpp.org/extensions/xep-0045.html#registrar-formtype-owner>`_.
  560. The values should be named without the ``muc#roomconfig_`` prefix.
  561. * *maximize*: A boolean, indicating whether minimized rooms should also be
  562. maximized, when opened. Set to ``false`` by default.
  563. For example, opening a room with a specific default configuration:
  564. .. code-block:: javascript
  565. converse.plugins.add('myplugin', {
  566. initialize: function () {
  567. this._converse.rooms.open(
  568. 'myroom@conference.example.org',
  569. { 'nick': 'coolguy69',
  570. 'auto_configure': true,
  571. 'roomconfig': {
  572. 'changesubject': false,
  573. 'membersonly': true,
  574. 'persistentroom': true,
  575. 'publicroom': true,
  576. 'roomdesc': 'Comfy room for hanging out',
  577. 'whois': 'anyone'
  578. }
  579. },
  580. true
  581. );
  582. }
  583. });
  584. .. note:: `multi-list` configuration values are not yet supported.
  585. close
  586. ~~~~~
  587. Lets you close open chat rooms. You can call this method without any arguments
  588. to close all open chat rooms, or you can specify a single JID or an array of
  589. JIDs.
  590. The **settings** grouping
  591. -------------------------
  592. This grouping allows you to get or set the configuration settings of converse.js.
  593. get(key)
  594. ~~~~~~~~
  595. Returns the value of a configuration settings. For example:
  596. .. code-block:: javascript
  597. converse.plugins.add('myplugin', {
  598. initialize: function () {
  599. // default value would be false;
  600. alert(this._converse.settings.get("play_sounds"));
  601. }
  602. });
  603. set(key, value) or set(object)
  604. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  605. Set one or many configuration settings. For example:
  606. .. code-block:: javascript
  607. converse.plugins.add('myplugin', {
  608. initialize: function () {
  609. this._converse.settings.set("play_sounds", true);
  610. }
  611. });
  612. or :
  613. .. code-block:: javascript
  614. converse.plugins.add('myplugin', {
  615. initialize: function () {
  616. this._converse.settings.set({
  617. "play_sounds", true,
  618. "hide_offline_users" true
  619. });
  620. }
  621. });
  622. Note, this is not an alternative to calling ``converse.initialize``, which still needs
  623. to be called. Generally, you'd use this method after converse.js is already
  624. running and you want to change the configuration on-the-fly.
  625. The **tokens** grouping
  626. -----------------------
  627. get
  628. ~~~
  629. Returns a token, either the RID or SID token depending on what's asked for.
  630. Example:
  631. .. code-block:: javascript
  632. converse.plugins.add('myplugin', {
  633. initialize: function () {
  634. alert(this._converse.tokens.get('rid'));
  635. }
  636. });
  637. .. _`listen-grouping`:
  638. The **listen** grouping
  639. -----------------------
  640. Converse.js emits events to which you can subscribe from your own Javascript.
  641. Concerning events, the following methods are available under the "listen"
  642. grouping:
  643. * **on(eventName, callback, [context])**:
  644. Calling the ``on`` method allows you to subscribe to an event.
  645. Every time the event fires, the callback method specified by ``callback`` will be
  646. called.
  647. Parameters:
  648. * ``eventName`` is the event name as a string.
  649. * ``callback`` is the callback method to be called when the event is emitted.
  650. * ``context`` (optional), the value of the `this` parameter for the callback.
  651. For example:
  652. .. code-block:: javascript
  653. <<<<<<< 3699a0af5c53a1ba4f8fd030421e4f976368b404
  654. _converse.listen.on('message', function (messageXML) { ... });
  655. =======
  656. converse.listen.on('message', function (messageXML) { ... });
  657. >>>>>>> Replace jQuery-based event emitter with Backbone.Events
  658. * **once(eventName, callback, [context])**:
  659. Calling the ``once`` method allows you to listen to an event
  660. exactly once.
  661. Parameters:
  662. * ``eventName`` is the event name as a string.
  663. * ``callback`` is the callback method to be called when the event is emitted.
  664. * ``context`` (optional), the value of the `this` parameter for the callback.
  665. For example:
  666. .. code-block:: javascript
  667. <<<<<<< 3699a0af5c53a1ba4f8fd030421e4f976368b404
  668. _converse.listen.once('message', function (messageXML) { ... });
  669. =======
  670. converse.listen.once('message', function (messageXML) { ... });
  671. >>>>>>> Replace jQuery-based event emitter with Backbone.Events
  672. * **not(eventName, callback)**
  673. To stop listening to an event, you can use the ``not`` method.
  674. Parameters:
  675. * ``eventName`` is the event name as a string.
  676. * ``callback`` refers to the function that is to be no longer executed.
  677. For example:
  678. .. code-block:: javascript
  679. <<<<<<< 3699a0af5c53a1ba4f8fd030421e4f976368b404
  680. _converse.listen.not('message', function (messageXML) { ... });
  681. =======
  682. converse.listen.not('message', function (messageXML) { ... });
  683. >>>>>>> Replace jQuery-based event emitter with Backbone.Events