events.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.js 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. * `controlboxInitialized`_ (only via the `converse-controlbox` plugin)
  16. * `pluginsInitialized`_
  17. * `roomsPanelRendered`_ (only via the `converse-muc` plugin)
  18. * `rosterContactsFetched`_
  19. * `rosterGroupsFetched`_
  20. * `rosterInitialized`_
  21. * `roster`_
  22. * `statusInitialized`_
  23. For more info on how to use (or add promises), you can read the
  24. :ref:`promises-grouping` in the API documentation.
  25. Below we will now list all events and also specify whether they are available
  26. as promises.
  27. List of Events (and promises)
  28. -----------------------------
  29. Hooking into events that Converse.js emits is a great way to extend or
  30. customize its functionality.
  31. From version 3.0.0 and up, it's only possible to register event handlers inside
  32. a plugin, by using the closured ``_converse`` object. When writing a plugin,
  33. remember that it will also have to be whitelisted, before it will be loaded.
  34. Refer to the :ref:`whitelisted_plugins` setting.
  35. Here follows the different events that are emitted:
  36. afterMessagesFetched
  37. ~~~~~~~~~~~~~~~~~~~~
  38. Emitted whenever a chatbox has fetched its messages from ``sessionStorage`` and
  39. **NOT** from the server.
  40. This event is listened to by the ``converse-mam`` plugin to know when it can
  41. fetch archived messages from the server.
  42. The event handler is passed the ``Backbone.View`` instance of the relevant chat
  43. box.
  44. ``_converse.on('afterMessagesFetched', function (chatboxview) { ... });``
  45. .. _`cachedRoster`:
  46. cachedRoster
  47. ~~~~~~~~~~~~
  48. The contacts roster has been retrieved from the local cache (`sessionStorage`).
  49. ``_converse.on('cachedRoster', function (items) { ... });``
  50. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  51. .. code-block:: javascript
  52. _converse.api.waitUntil('cachedRoster').then(function () {
  53. // Your code here...
  54. });
  55. See also the `roster`_ event further down.
  56. callButtonClicked
  57. ~~~~~~~~~~~~~~~~~
  58. When a call button (i.e. with class .toggle-call) on a chatbox has been clicked.
  59. ``_converse.on('callButtonClicked', function (connection, model) { ... });``
  60. .. _`chatBoxesFetched`:
  61. chatBoxesFetched
  62. ~~~~~~~~~~~~~~~~
  63. Any open chatboxes (from this current session) has been retrieved from the local cache (`sessionStorage`).
  64. You should wait for this event or promise before attempting to do things
  65. related to open chatboxes.
  66. ``_converse.on('chatBoxesFetched', function (items) { ... });``
  67. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  68. .. code-block:: javascript
  69. _converse.api.waitUntil('chatBoxesFetched').then(function () {
  70. // Your code here...
  71. });
  72. chatBoxInitialized
  73. ~~~~~~~~~~~~~~~~~~
  74. When a chatbox has been initialized. Relevant to converse-chatview.js plugin.
  75. ``_converse.on('chatBoxInitialized', function (chatbox) { ... });``
  76. chatBoxOpened
  77. ~~~~~~~~~~~~~
  78. When a chatbox has been opened. Relevant to converse-chatview.js plugin.
  79. ``_converse.on('chatBoxOpened', function (chatbox) { ... });``
  80. chatRoomOpened
  81. ~~~~~~~~~~~~~~
  82. When a chatroom has been opened. Relevant to converse-chatview.js plugin.
  83. ``_converse.on('chatRoomOpened', function (chatbox) { ... });``
  84. chatBoxClosed
  85. ~~~~~~~~~~~~~
  86. When a chatbox has been closed. Relevant to converse-chatview.js plugin.
  87. ``_converse.on('chatBoxClosed', function (chatbox) { ... });``
  88. chatBoxFocused
  89. ~~~~~~~~~~~~~~
  90. When the focus has been moved to a chatbox. Relevant to converse-chatview.js plugin.
  91. ``_converse.on('chatBoxFocused', function (chatbox) { ... });``
  92. chatBoxToggled
  93. ~~~~~~~~~~~~~~
  94. When a chatbox has been minimized or maximized. Relevant to converse-chatview.js plugin.
  95. ``_converse.on('chatBoxToggled', function (chatbox) { ... });``
  96. connected
  97. ~~~~~~~~~
  98. After connection has been established and converse.js has got all its ducks in a row.
  99. ``_converse.on('connected', function () { ... });``
  100. contactRequest
  101. ~~~~~~~~~~~~~~
  102. Someone has requested to subscribe to your presence (i.e. to be your contact).
  103. ``_converse.on('contactRequest', function (user_data) { ... });``
  104. contactRemoved
  105. ~~~~~~~~~~~~~~
  106. The user has removed a contact.
  107. ``_converse.on('contactRemoved', function (data) { ... });``
  108. contactStatusChanged
  109. ~~~~~~~~~~~~~~~~~~~~
  110. When a chat buddy's chat status has changed.
  111. ``_converse.on('contactStatusChanged', function (buddy) { ... });``
  112. contactStatusMessageChanged
  113. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. When a chat buddy's custom status message has changed.
  115. ``_converse.on('contactStatusMessageChanged', function (data) { ... });``
  116. controlboxInitialized
  117. ~~~~~~~~~~~~~~~~~~~~~
  118. Called when the controlbox has been initialized and therefore exists.
  119. The controlbox contains the login and register forms when
  120. the user is logged out and a list of the user's contacts and group chats when
  121. logged in.
  122. ``_converse.on('controlboxInitialized', function () { ... });``
  123. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  124. .. code-block:: javascript
  125. _converse.api.waitUntil('controlboxInitialized').then(function () {
  126. // Your code here...
  127. });
  128. discoInitialized
  129. ~~~~~~~~~~~~~~~~
  130. Emitted once the ``converse-disco`` plugin has been initialized and the
  131. ``_converse.disco_entities`` collection will be available and populated with at
  132. least the service discovery features of the user's own server.
  133. ``_converse.on('discoInitialized', function () { ... });``
  134. disconnected
  135. ~~~~~~~~~~~~
  136. After converse.js has disconnected from the XMPP server.
  137. ``_converse.on('disconnected', function () { ... });``
  138. initialized
  139. ~~~~~~~~~~~
  140. Once converse.js has been initialized.
  141. ``_converse.on('initialized', function () { ... });``
  142. See also `pluginsInitialized`_.
  143. logout
  144. ~~~~~~
  145. The user has logged out.
  146. ``_converse.on('logout', function () { ... });``
  147. messageAdded
  148. ~~~~~~~~~~~~
  149. Once a message has been added to a chatbox. The passed in data object contains
  150. a `chatbox` attribute, referring to the chatbox receiving the message, as well
  151. as a `message` attribute which refers to the Message model.
  152. .. code-block:: javascript
  153. _converse.on('messageAdded', function (data) {
  154. // The message is at `data.message`
  155. // The original chatbox is at `data.chatbox`.
  156. });
  157. messageSend
  158. ~~~~~~~~~~~
  159. When a message will be sent out.
  160. ``_converse.on('messageSend', function (messageText) { ... });``
  161. noResumeableSession
  162. ~~~~~~~~~~~~~~~~~~~
  163. When keepalive=true but there aren't any stored prebind tokens.
  164. ``_converse.on('noResumeableSession', function () { ... });``
  165. .. _`pluginsInitialized`:
  166. pluginsInitialized
  167. ~~~~~~~~~~~~~~~~~~
  168. Emitted once all plugins have been initialized. This is a useful event if you want to
  169. register event handlers but would like your own handlers to be overridable by
  170. plugins. In that case, you need to first wait until all plugins have been
  171. initialized, so that their overrides are active. One example where this is used
  172. is in `converse-notifications.js <https://github.com/jcbrand/converse.js/blob/master/src/converse-notification.js>`.
  173. ``_converse.on('pluginsInitialized', function () { ... });``
  174. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  175. .. code-block:: javascript
  176. _converse.api.waitUntil('pluginsInitialized').then(function () {
  177. // Your code here...
  178. });
  179. reconnecting
  180. ~~~~~~~~~~~~
  181. Fired once converse.js has determined that it will attempt to reconnect (and
  182. each subsequent time, if it attempts repeatedly).
  183. reconnected
  184. ~~~~~~~~~~~
  185. After the connection has dropped and converse.js has reconnected.
  186. Any Strophe stanza handlers (as registered via `converse.listen.stanza`) will
  187. have to be registered anew.
  188. ``_converse.on('reconnected', function () { ... });``
  189. roomsAutoJoined
  190. ---------------
  191. Emitted once any rooms that have been configured to be automatically joined,
  192. specified via the _`auto_join_rooms` setting, have been entered.
  193. ``_converse.on('roomsAutoJoined', function () { ... });``
  194. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  195. .. code-block:: javascript
  196. _converse.api.waitUntil('roomsAutoJoined').then(function () {
  197. // Your code here...
  198. });
  199. roomInviteSent
  200. ~~~~~~~~~~~~~~
  201. After the user has sent out a direct invitation, to a roster contact, asking them to join a room.
  202. ``_converse.on('roomInvite', function (data) { ... });``
  203. roomInviteReceived
  204. ~~~~~~~~~~~~~~~~~~
  205. After the user has sent out a direct invitation, to a roster contact, asking them to join a room.
  206. ``_converse.on('roomInvite', function (data) { ... });``
  207. .. _`roomsPanelRendered`:
  208. roomsPanelRendered
  209. ~~~~~~~~~~~~~~~~~~
  210. Emitted once the "Rooms" panel in the control box has been rendered.
  211. Used by `converse-bookmarks` and `converse-roomslist` to know when they can
  212. render themselves in that panel.
  213. ``_converse.on('roomsPanelRendered', function (data) { ... });``
  214. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  215. .. code-block:: javascript
  216. _converse.api.waitUntil('roomsPanelRendered').then(function () {
  217. // Your code here...
  218. });
  219. .. _`roster`:
  220. roster
  221. ~~~~~~
  222. When the roster has been received from the XMPP server.
  223. ``_converse.on('roster', function (items) { ... });``
  224. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  225. .. code-block:: javascript
  226. _converse.api.waitUntil('roster').then(function () {
  227. // Your code here...
  228. });
  229. See also the `cachedRoster` event further up, which gets called instead of
  230. `roster` if its already in `sessionStorage`.
  231. .. _`rosterContactsFetched`:
  232. rosterContactsFetched
  233. ~~~~~~~~~~~~~~~~~~~~~
  234. Triggered once roster contacts have been fetched. Used by the
  235. `converse-rosterview.js` plugin to know when it can start to show the roster.
  236. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  237. .. code-block:: javascript
  238. _converse.api.waitUntil('rosterContactsFetched').then(function () {
  239. // Your code here...
  240. });
  241. .. _`rosterGroupsFetched`:
  242. rosterGroupsFetched
  243. ~~~~~~~~~~~~~~~~~~~
  244. Triggered once roster groups have been fetched. Used by the
  245. `converse-rosterview.js` plugin to know when it can start alphabetically
  246. position roster groups.
  247. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  248. .. code-block:: javascript
  249. _converse.api.waitUntil('rosterGroupsFetched').then(function () {
  250. // Your code here...
  251. });
  252. .. _`rosterInitialized`:
  253. rosterInitialized
  254. ~~~~~~~~~~~~~~~~~
  255. The Backbone collections `RosterContacts` and `RosterGroups` have been created,
  256. but not yet populated with data.
  257. This event is useful when you want to create views for these collections.
  258. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  259. .. code-block:: javascript
  260. _converse.api.waitUntil('rosterInitialized').then(function () {
  261. // Your code here...
  262. });
  263. rosterPush
  264. ~~~~~~~~~~
  265. When the roster receives a push event from server. (i.e. New entry in your buddy list)
  266. ``_converse.on('rosterPush', function (items) { ... });``
  267. rosterReadyAfterReconnection
  268. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  269. Similar to `rosterInitialized`, but instead pertaining to reconnection. This
  270. event indicates that the Backbone collections representing the roster and its
  271. groups are now again available after converse.js has reconnected.
  272. .. _`statusInitialized`:
  273. statusInitialized
  274. ~~~~~~~~~~~~~~~~~
  275. When the user's own chat status has been initialized.
  276. ``_converse.on('statusInitialized', function (status) { ... });``
  277. Also available as an `ES2015 Promise <http://es6-features.org/#PromiseUsage>`_:
  278. .. code-block:: javascript
  279. _converse.api.waitUntil('statusInitialized').then(function () {
  280. // Your code here...
  281. });
  282. statusChanged
  283. ~~~~~~~~~~~~~
  284. When own chat status has changed.
  285. ``_converse.on('statusChanged', function (status) { ... });``
  286. statusMessageChanged
  287. ~~~~~~~~~~~~~~~~~~~~
  288. When own custom status message has changed.
  289. ``_converse.on('statusMessageChanged', function (message) { ... });``
  290. serviceDiscovered
  291. ~~~~~~~~~~~~~~~~~
  292. When converse.js has learned of a service provided by the XMPP server. See XEP-0030.
  293. ``_converse.on('serviceDiscovered', function (service) { ... });``
  294. windowStateChanged
  295. ~~~~~~~~~~~~~~~~~~
  296. When window state has changed. Used to determine when a user left the page and when came back.
  297. ``_converse.on('windowStateChanged', function (data) { ... });``