events.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. .. _`events-API`:
  4. Events and promises
  5. ===================
  6. Converse and its plugins emit various events which you can listen to via the
  7. :ref:`listen-grouping`.
  8. Some of these events are also available as `ES2015 Promises <http://es6-features.org/#PromiseUsage>`_,
  9. although not all of them could logically act as promises, since some events
  10. might be fired multpile times whereas promises are to be resolved (or
  11. rejected) only once.
  12. The core events, which are also promises are:
  13. * `cachedRoster`_
  14. * `chatBoxesFetched`_
  15. * `connectionInitialized`_
  16. * `controlboxInitialized`_ (only via the `converse-controlbox` plugin)
  17. * `pluginsInitialized`_
  18. * `roomsPanelRendered`_ (only via the `converse-muc` plugin)
  19. * `rosterContactsFetched`_
  20. * `rosterGroupsFetched`_
  21. * `rosterInitialized`_
  22. * `roster`_
  23. * `statusInitialized`_
  24. For more info on how to use (or add promises), you can read the
  25. :ref:`promises-grouping` in the API documentation.
  26. Below we will now list all events and also specify whether they are available
  27. as promises.
  28. Global events
  29. -------------
  30. With global events, we mean events triggered in the global context, i.e. on the
  31. `window` object in browsers.
  32. converse-loaded
  33. ---------------
  34. Once Converse.js has loaded, it'll dispatch a custom event with the name
  35. ``converse-loaded``.
  36. You can listen for this event in your scripts and thereby be informed as soon
  37. as converse.js has been loaded, which would mean it's safe to call
  38. ``converse.initialize``.
  39. For example:
  40. .. code-block:: javascript
  41. window.addEventListener('converse-loaded', () => {
  42. converse.initialize();
  43. });
  44. List protected of events (and promises)
  45. ----------------------------------------
  46. Hooking into events that Converse.js emits is a great way to extend or
  47. customize its functionality.
  48. From version 3.0.0 and up, it's only possible to register event handlers inside
  49. a plugin, by using the closured ``_converse`` object. When writing a plugin,
  50. remember that it will also have to be whitelisted, before it will be loaded.
  51. Refer to the :ref:`whitelisted_plugins` setting.
  52. Here follows the different events that are emitted:
  53. afterMessagesFetched
  54. ~~~~~~~~~~~~~~~~~~~~
  55. Emitted whenever a chatbox has fetched its messages from ``sessionStorage`` and
  56. **NOT** from the server.
  57. This event is listened to by the ``converse-mam`` plugin to know when it can
  58. fetch archived messages from the server.
  59. The event handler is passed the ``Backbone.View`` instance of the relevant chat
  60. box.
  61. ``_converse.api.listen.on('afterMessagesFetched', function (chatboxview) { ... });``
  62. .. _`cachedRoster`:
  63. cachedRoster
  64. ~~~~~~~~~~~~
  65. The contacts roster has been retrieved from the local cache (`sessionStorage`).
  66. ``_converse.api.listen.on('cachedRoster', function (items) { ... });``
  67. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  68. .. code-block:: javascript
  69. _converse.api.waitUntil('cachedRoster').then(function () {
  70. // Your code here...
  71. });
  72. See also the `roster`_ event further down.
  73. callButtonClicked
  74. ~~~~~~~~~~~~~~~~~
  75. When a call button (i.e. with class .toggle-call) on a chatbox has been clicked.
  76. ``_converse.api.listen.on('callButtonClicked', function (connection, model) { ... });``
  77. .. _`chatBoxesFetched`:
  78. chatBoxesFetched
  79. ~~~~~~~~~~~~~~~~
  80. Any open chatboxes (from this current session) has been retrieved from the local cache (`sessionStorage`).
  81. You should wait for this event or promise before attempting to do things
  82. related to open chatboxes.
  83. ``_converse.api.listen.on('chatBoxesFetched', function (items) { ... });``
  84. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  85. .. code-block:: javascript
  86. _converse.api.waitUntil('chatBoxesFetched').then(function () {
  87. // Your code here...
  88. });
  89. chatBoxInitialized
  90. ~~~~~~~~~~~~~~~~~~
  91. When a chatbox has been initialized. Relevant to converse-chatview.js plugin.
  92. ``_converse.api.listen.on('chatBoxInitialized', function (chatbox) { ... });``
  93. chatBoxOpened
  94. ~~~~~~~~~~~~~
  95. When a chatbox has been opened. Relevant to converse-chatview.js plugin.
  96. ``_converse.api.listen.on('chatBoxOpened', function (chatbox) { ... });``
  97. chatRoomOpened
  98. ~~~~~~~~~~~~~~
  99. When a chatroom has been opened. Relevant to converse-chatview.js plugin.
  100. ``_converse.api.listen.on('chatRoomOpened', function (chatbox) { ... });``
  101. chatBoxClosed
  102. ~~~~~~~~~~~~~
  103. When a chatbox has been closed. Relevant to converse-chatview.js plugin.
  104. ``_converse.api.listen.on('chatBoxClosed', function (chatbox) { ... });``
  105. chatBoxFocused
  106. ~~~~~~~~~~~~~~
  107. When the focus has been moved to a chatbox. Relevant to converse-chatview.js plugin.
  108. ``_converse.api.listen.on('chatBoxFocused', function (chatbox) { ... });``
  109. chatBoxToggled
  110. ~~~~~~~~~~~~~~
  111. When a chatbox has been minimized or maximized. Relevant to converse-chatview.js plugin.
  112. ``_converse.api.listen.on('chatBoxToggled', function (chatbox) { ... });``
  113. clearSession
  114. ~~~~~~~~~~~~
  115. Called when the user is logging out and provides the opportunity to remove session data.
  116. connected
  117. ~~~~~~~~~
  118. After connection has been established and converse.js has got all its ducks in a row.
  119. ``_converse.api.listen.on('connected', function () { ... });``
  120. connectionInitialized
  121. ~~~~~~~~~~~~~~~~~~~~~
  122. Called once the ``Strophe.Connection`` constructor has been initialized, which
  123. will be responsible for managing the connection to the XMPP server.
  124. contactRequest
  125. ~~~~~~~~~~~~~~
  126. Someone has requested to subscribe to your presence (i.e. to be your contact).
  127. The `Backbone.Model <http://backbonejs.org/#Model>`_ instance representing the
  128. roster contact is passed to the event listener.
  129. ``_converse.api.listen.on('contactRequest', function (contact) { ... });``
  130. contactRemoved
  131. ~~~~~~~~~~~~~~
  132. The user has removed a contact.
  133. ``_converse.api.listen.on('contactRemoved', function (data) { ... });``
  134. contactPresenceChanged
  135. ~~~~~~~~~~~~~~~~~~~~~~
  136. When a chat buddy's presence status has changed.
  137. The presence status is either `online`, `offline`, `dnd`, `away` or `xa`.
  138. ``_converse.api.listen.on('contactPresenceChanged', function (presence) { ... });``
  139. contactStatusMessageChanged
  140. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  141. When a chat buddy's custom status message has changed.
  142. ``_converse.api.listen.on('contactStatusMessageChanged', function (data) { ... });``
  143. controlboxInitialized
  144. ~~~~~~~~~~~~~~~~~~~~~
  145. Called when the controlbox has been initialized and therefore exists.
  146. The controlbox contains the login and register forms when
  147. the user is logged out and a list of the user's contacts and group chats when
  148. logged in.
  149. ``_converse.api.listen.on('controlboxInitialized', function () { ... });``
  150. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  151. .. code-block:: javascript
  152. _converse.api.waitUntil('controlboxInitialized').then(function () {
  153. // Your code here...
  154. });
  155. discoInitialized
  156. ~~~~~~~~~~~~~~~~
  157. Emitted once the ``converse-disco`` plugin has been initialized and the
  158. ``_converse.disco_entities`` collection will be available and populated with at
  159. least the service discovery features of the user's own server.
  160. ``_converse.api.listen.on('discoInitialized', function () { ... });``
  161. disconnected
  162. ~~~~~~~~~~~~
  163. After converse.js has disconnected from the XMPP server.
  164. ``_converse.api.listen.on('disconnected', function () { ... });``
  165. initialized
  166. ~~~~~~~~~~~
  167. Once converse.js has been initialized.
  168. ``_converse.api.listen.on('initialized', function () { ... });``
  169. See also `pluginsInitialized`_.
  170. logout
  171. ~~~~~~
  172. The user has logged out.
  173. ``_converse.api.listen.on('logout', function () { ... });``
  174. messageAdded
  175. ~~~~~~~~~~~~
  176. Once a message has been added to a chatbox. The passed in data object contains
  177. a `chatbox` attribute, referring to the chatbox receiving the message, as well
  178. as a `message` attribute which refers to the Message model.
  179. .. code-block:: javascript
  180. _converse.api.listen.on('messageAdded', function (data) {
  181. // The message is at `data.message`
  182. // The original chatbox is at `data.chatbox`.
  183. });
  184. messageNotification
  185. ~~~~~~~~~~~~~~~~~~~
  186. Emitted just before an HTML5 message notification will be sent out.
  187. .. code-block:: javascript
  188. _converse.api.listen.on('messageNotification', stanza => {
  189. const body = sizzle(`encrypted[xmlns="${Strophe.NS.OMEMO}"]`, message).length ?
  190. __('OMEMO Message received') :
  191. _.get(message.querySelector('body'), 'textContent');
  192. alert(body);
  193. });
  194. messageSend
  195. ~~~~~~~~~~~
  196. When a message will be sent out.
  197. ``_converse.api.listen.on('messageSend', function (messageText) { ... });``
  198. noResumeableSession
  199. ~~~~~~~~~~~~~~~~~~~
  200. When keepalive=true but there aren't any stored prebind tokens.
  201. ``_converse.api.listen.on('noResumeableSession', function () { ... });``
  202. .. _`pluginsInitialized`:
  203. pluginsInitialized
  204. ~~~~~~~~~~~~~~~~~~
  205. Emitted once all plugins have been initialized. This is a useful event if you want to
  206. register event handlers but would like your own handlers to be overridable by
  207. plugins. In that case, you need to first wait until all plugins have been
  208. initialized, so that their overrides are active. One example where this is used
  209. is in `converse-notifications.js <https://github.com/jcbrand/converse.js/blob/master/src/converse-notification.js>`.
  210. ``_converse.api.listen.on('pluginsInitialized', function () { ... });``
  211. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  212. .. code-block:: javascript
  213. _converse.api.waitUntil('pluginsInitialized').then(function () {
  214. // Your code here...
  215. });
  216. privateChatsAutoJoined
  217. ~~~~~~~~~~~~~~~~~~~~~~
  218. Emitted once any private chats have been automatically joined as specified by
  219. the _`auto_join_private_chats` settings.
  220. .. code-block:: javascript
  221. _converse.api.listen.on('privateChatsAutoJoined', function () { ... });
  222. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_.
  223. .. code-block:: javascript
  224. _converse.api.waitUntil('privateChatsAutoJoined').then(function () {
  225. // Your code here...
  226. });
  227. reconnecting
  228. ~~~~~~~~~~~~
  229. Fired once converse.js has determined that it will attempt to reconnect (and
  230. each subsequent time, if it attempts repeatedly).
  231. reconnected
  232. ~~~~~~~~~~~
  233. After the connection has dropped and converse.js has reconnected.
  234. Any Strophe stanza handlers (as registered via `converse.listen.stanza`) will
  235. have to be registered anew.
  236. .. code-block:: javascript
  237. _converse.api.listen.on('reconnected', function () { ... });
  238. registeredGlobalEventHandlers
  239. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  240. Called once Converse has registered its global event handlers (for events such
  241. as window resize or unload).
  242. Plugins can listen to this event as cue to register their own global event
  243. handlers.
  244. roomsAutoJoined
  245. ~~~~~~~~~~~~~~~
  246. Emitted once any rooms that have been configured to be automatically joined,
  247. specified via the _`auto_join_rooms` setting, have been entered.
  248. .. code-block:: javascript
  249. _converse.api.listen.on('roomsAutoJoined', function () { ... });
  250. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  251. .. code-block:: javascript
  252. _converse.api.waitUntil('roomsAutoJoined').then(function () {
  253. // Your code here...
  254. });
  255. roomInviteSent
  256. ~~~~~~~~~~~~~~
  257. After the user has sent out a direct invitation, to a roster contact, asking them to join a room.
  258. ``_converse.api.listen.on('roomInvite', function (data) { ... });``
  259. roomInviteReceived
  260. ~~~~~~~~~~~~~~~~~~
  261. After the user has sent out a direct invitation, to a roster contact, asking them to join a room.
  262. ``_converse.api.listen.on('roomInvite', function (data) { ... });``
  263. .. _`roomsPanelRendered`:
  264. roomsPanelRendered
  265. ~~~~~~~~~~~~~~~~~~
  266. Emitted once the "Rooms" panel in the control box has been rendered.
  267. Used by `converse-bookmarks` and `converse-roomslist` to know when they can
  268. render themselves in that panel.
  269. ``_converse.api.listen.on('roomsPanelRendered', function (data) { ... });``
  270. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  271. .. code-block:: javascript
  272. _converse.api.waitUntil('roomsPanelRendered').then(function () {
  273. // Your code here...
  274. });
  275. .. _`roster`:
  276. roster
  277. ~~~~~~
  278. When the roster has been received from the XMPP server.
  279. ``_converse.api.listen.on('roster', function (items) { ... });``
  280. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  281. .. code-block:: javascript
  282. _converse.api.waitUntil('roster').then(function () {
  283. // Your code here...
  284. });
  285. See also the `cachedRoster` event further up, which gets called instead of
  286. `roster` if its already in `sessionStorage`.
  287. .. _`rosterContactsFetched`:
  288. rosterContactsFetched
  289. ~~~~~~~~~~~~~~~~~~~~~
  290. Triggered once roster contacts have been fetched. Used by the
  291. `converse-rosterview.js` plugin to know when it can start to show the roster.
  292. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  293. .. code-block:: javascript
  294. _converse.api.waitUntil('rosterContactsFetched').then(function () {
  295. // Your code here...
  296. });
  297. .. _`rosterGroupsFetched`:
  298. rosterGroupsFetched
  299. ~~~~~~~~~~~~~~~~~~~
  300. Triggered once roster groups have been fetched. Used by the
  301. `converse-rosterview.js` plugin to know when it can start alphabetically
  302. position roster groups.
  303. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  304. .. code-block:: javascript
  305. _converse.api.waitUntil('rosterGroupsFetched').then(function () {
  306. // Your code here...
  307. });
  308. .. _`rosterInitialized`:
  309. rosterInitialized
  310. ~~~~~~~~~~~~~~~~~
  311. The Backbone collections `RosterContacts` and `RosterGroups` have been created,
  312. but not yet populated with data.
  313. This event is useful when you want to create views for these collections.
  314. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  315. .. code-block:: javascript
  316. _converse.api.waitUntil('rosterInitialized').then(function () {
  317. // Your code here...
  318. });
  319. rosterPush
  320. ~~~~~~~~~~
  321. When the roster receives a push event from server. (i.e. New entry in your buddy list)
  322. ``_converse.api.listen.on('rosterPush', function (items) { ... });``
  323. rosterReadyAfterReconnection
  324. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  325. Similar to `rosterInitialized`, but instead pertaining to reconnection. This
  326. event indicates that the Backbone collections representing the roster and its
  327. groups are now again available after converse.js has reconnected.
  328. serviceDiscovered
  329. ~~~~~~~~~~~~~~~~~
  330. When converse.js has learned of a service provided by the XMPP server. See XEP-0030.
  331. ``_converse.api.listen.on('serviceDiscovered', function (service) { ... });``
  332. .. _`statusInitialized`:
  333. statusInitialized
  334. ~~~~~~~~~~~~~~~~~
  335. When the user's own chat status has been initialized.
  336. ``_converse.api.listen.on('statusInitialized', function (status) { ... });``
  337. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  338. .. code-block:: javascript
  339. _converse.api.waitUntil('statusInitialized').then(function () {
  340. // Your code here...
  341. });
  342. statusChanged
  343. ~~~~~~~~~~~~~
  344. When own chat status has changed.
  345. ``_converse.api.listen.on('statusChanged', function (status) { ... });``
  346. statusMessageChanged
  347. ~~~~~~~~~~~~~~~~~~~~
  348. When own custom status message has changed.
  349. ``_converse.api.listen.on('statusMessageChanged', function (message) { ... });``
  350. streamFeaturesAdded
  351. ~~~~~~~~~~~~~~~~~~~
  352. Emitted as soon as Converse has processed the stream features as advertised by
  353. the server. If you want to check whether a stream feature is supported before
  354. proceeding, then you'll first want to wait for this event.
  355. windowStateChanged
  356. ~~~~~~~~~~~~~~~~~~
  357. When window state has changed. Used to determine when a user left the page and when came back.
  358. ``_converse.api.listen.on('windowStateChanged', function (data) { ... });``
  359. List of events on the ChatRoom Backbone.Model
  360. ---------------------------------------------
  361. configurationNeeded
  362. ~~~~~~~~~~~~~~~~~~~
  363. Triggered when a new room has been created which first needs to be configured
  364. and when `auto_configure` is set to `false`.
  365. Used by the core `ChatRoomView` view in order to know when to render the
  366. configuration form for a new room.