index.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. .. Converse.js documentation master file, created by
  2. sphinx-quickstart on Fri Apr 26 20:48:03 2013.
  3. You can adapt this file completely to your liking, but it should at least
  4. contain the root `toctree` directive.
  5. .. toctree::
  6. :maxdepth: 2
  7. .. contents:: Table of Contents
  8. :depth: 3
  9. :local:
  10. ============
  11. Introduction
  12. ============
  13. Even though you can connect to public XMPP servers on the `conversejs.org`_
  14. website, *Converse.js* is not really meant to be a "Software-as-a-service" (SaaS)
  15. webchat.
  16. Instead, its goal is to provide the means for website owners to add a tightly
  17. integrated instant messaging service to their own sites.
  18. As a website owner, you are expected to host *Converse.js* yourself, and to do some legwork to
  19. properly configure and integrate it into your site.
  20. The benefit in doing this, is that your users have a much more streamlined and integrated
  21. webchat experience and that you have control over the data. The latter being a
  22. requirement for many sites dealing with sensitive information.
  23. You'll need to set up your own XMPP server and in order to have
  24. `Session Support`_ (i.e. single-signon functionality whereby users are authenticated once and stay
  25. logged in to XMPP upon page reload) you will also have to add some server-side
  26. code.
  27. The `What you will need`_ section has more information on all these
  28. requirements.
  29. ==================
  30. What you will need
  31. ==================
  32. An XMPP/Jabber server
  33. =====================
  34. *Converse.js* implements `XMPP`_ as its messaging protocol, and therefore needs
  35. to connect to an XMPP/Jabber server (Jabber is really just a synonym for XMPP).
  36. You can connect to public XMPP servers like ``jabber.org`` but if you want to
  37. have `Session Support`_ you'll have to set up your own XMPP server.
  38. You can find a list of public XMPP servers/providers on `xmpp.net`_ and a list of
  39. servers that you can set up yourself on `xmpp.org`_.
  40. Connection Manager
  41. ==================
  42. Your website and *Converse.js* use `HTTP`_ as protocol to communicate with
  43. the webserver. HTTP connections are stateless and usually shortlived.
  44. `XMPP`_ on the other hand, is the protocol that enables instant messaging, and
  45. its connections are stateful and usually longer.
  46. To enable a web application like *Converse.js* to communicate with an XMPP
  47. server, we need a proxy in the middle that can act as a bridge between the two
  48. protocols.
  49. This is the job of a connection manager. A connection manager can be either a
  50. standalone application or part of an XMPP server. `ejabberd`_ for example,
  51. includes a connection manager (but you have to enable it).
  52. The demo on the `Converse.js homepage`_ uses a a connection manager located at https://bind.opkode.im.
  53. This connection manager is for testing purposes only, please don't use it in
  54. production.
  55. Overcoming cross-domain request restrictions
  56. --------------------------------------------
  57. The domain of the *Converse.js* demo is *conversejs.org*, but the domain of the connection manager is *opkode.im*.
  58. HTTP requests are made by *Converse.js* to the connection manager via XmlHttpRequests (XHR).
  59. Until recently, it was not possible to make such requests to a different domain
  60. than the one currently being served (to prevent XSS attacks).
  61. Luckily there is now a standard called `CORS`_ (Cross-origin resource sharing), which enables exactly that.
  62. Modern browsers support CORS, but there are problems with Internet Explorer <
  63. 10.
  64. IE 8 and 9 partially support CORS via a proprietary implementation called
  65. XDomainRequest. There is a `Strophe.js plugin`_ which you can use to enable
  66. support for XDomainRequest when it is present.
  67. In IE < 8, there is no support for CORS.
  68. If you need to support these browsers, you can add a front-end proxy in
  69. Apache/Nginx which serves the connection manager under the same domain as your
  70. website. This will remove the need for any cross-domain XHR support.
  71. Server-side authentication
  72. ==========================
  73. Session Support
  74. ---------------
  75. It's possible to enable single-site login, whereby users already
  76. authenticated in your website will also automatically be logged in on the chat server,
  77. but this will require custom code on your server.
  78. Jack Moffitt has a great `blogpost`_ about this and even provides an `example Django application`_ to demonstrate it.
  79. When you authenticate to the XMPP server on your backend, you'll receive two
  80. tokens, RID (request ID) and SID (session ID).
  81. These tokens then need to be passed back to the javascript running in your
  82. browser, where you will need them attach to the existing session.
  83. You can embed the RID and SID tokens in your HTML markup or you can do an
  84. XMLHttpRequest call to you server and ask it to return them for you.
  85. Below is one example of how this could work. An Ajax call is made to the
  86. relative URL **/prebind** and it expects to receive JSON data back.
  87. ::
  88. $.getJSON('/prebind', function (data) {
  89. var connection = new Strophe.Connection(converse.bosh_service_url);
  90. connection.attach(data.jid, data.sid, data.rid, function (status) {
  91. if ((status === Strophe.Status.ATTACHED) || (status === Strophe.Status.CONNECTED)) {
  92. converse.onConnected(connection)
  93. }
  94. });
  95. }
  96. );
  97. **Here's what's happening:**
  98. The JSON data contains the user's JID (jabber ID), RID and SID. The URL to the
  99. BOSH connection manager is already set as a configuration setting on the
  100. *converse* object (see ./main.js), so we can reuse it from there.
  101. A new Strophe.Connection object is instantiated and then *attach* is called with
  102. the user's JID, the necessary tokens and a callback function.
  103. In the callback function, you call *converse.onConnected* together with the
  104. connection object.
  105. =========================================
  106. Quickstart (to get a demo up and running)
  107. =========================================
  108. When you download a specific release of *Converse.js*, say for example version 0.3,
  109. there will be two minified files inside the zip file.
  110. For version 0.3 they will be:
  111. * converse.0.3.min.js
  112. * converse.0.3.min.css
  113. You can include these two files in your website via the *script* and *link*
  114. tags:
  115. ::
  116. <link rel="stylesheet" type="text/css" media="screen" href="converse.0.3.min.css">
  117. <script src="converse.0.3.min.js"></script>
  118. The *index.html* file inside the Converse.js folder serves as a nice usable
  119. example of this.
  120. These minified files provide the same demo-like functionality as is available
  121. on the `conversejs.org`_ website. Useful for testing or demoing, but not very
  122. practical.
  123. You'll most likely want to implement some kind of single-signon solution for
  124. your website, where users authenticate once in your website and then stay
  125. logged into their XMPP session upon page reload.
  126. For more info on this, read `Session Support`_.
  127. You might also want to have more fine-grained control of what gets included in
  128. the minified Javascript file. Read `Configuration`_ and `Minification`_ for more info on how to do
  129. that.
  130. =============
  131. Configuration
  132. =============
  133. The included minified JS and CSS files can be used for demoing or testing, but
  134. you'll want to configure *Converse.js* to suit your needs before you deploy it
  135. on your website.
  136. *Converse.js* is passed its configuration settings when you call its
  137. *initialize* method.
  138. Please refer to the `Configuration variables`_ section below for info on
  139. all the available configuration settings.
  140. After you have configured *Converse.js*, you'll have to regenerate the minified
  141. JS file so that it will include the new settings. Please refer to the
  142. `Minification`_ section for more info on how to do this.
  143. Configuration variables
  144. =======================
  145. animate
  146. -------
  147. Default = True
  148. Show animations, for example when opening and closing chat boxes.
  149. auto_list_rooms
  150. ---------------
  151. Default = False
  152. If true, and the XMPP server on which the current user is logged in supports
  153. multi-user chat, then a list of rooms on that server will be fetched.
  154. Not recommended for servers with lots of chat rooms.
  155. For each room on the server a query is made to fetch further details (e.g.
  156. features, number of occupants etc.), so on servers with many rooms this
  157. option will create lots of extra connection traffic.
  158. auto_subscribe
  159. --------------
  160. Default = False
  161. If true, the user will automatically subscribe back to any contact requests.
  162. bosh_service_url
  163. ----------------
  164. Connections to an XMPP server depend on a BOSH connection manager which acts as
  165. a middle man between HTTP and XMPP.
  166. See `here`_ for more information.
  167. fullname
  168. --------
  169. If you are using prebinding, you need to specify the fullname of the currently
  170. logged in user.
  171. hide_muc_server
  172. ---------------
  173. Default = False
  174. Hide the ``server`` input field of the form inside the ``Room`` panel of the
  175. controlbox. Useful if you want to restrict users to a specific XMPP server of
  176. your choosing.
  177. prebind
  178. --------
  179. Default = False
  180. Use this option when you want to attach to an existing XMPP connection that was
  181. already authenticated (usually on the backend before page load).
  182. This is useful when you don't want to render the login form on the chat control
  183. box with each page load.
  184. When set to true, you'll need to make sure that the onConnected method is
  185. called, and passed to it a Strophe connection object.
  186. Besides requiring the back-end to authenticate you, you'll also
  187. have to write a Javascript snippet to attach to the set up connection::
  188. $.JSON({
  189. 'url': 'mysite.com/xmpp-authenticate',
  190. 'success': function (data) {
  191. connection = new Strophe.Connection(bosh_service_url);
  192. connection.attach(data.jid, data.sid, data.rid, converse.onConnected);
  193. }
  194. The backend must authenticate for you, and then return a SID (session ID) and
  195. RID (Request ID), which you use when you attach to the connection.
  196. show_controlbox_by_default
  197. --------------------------
  198. Default = False
  199. The "controlbox" refers to the special chatbox containing your contacts roster,
  200. status widget, chatrooms and other controls.
  201. By default this box is hidden and can be toggled by clicking on any element in
  202. the page with class *toggle-online-users*.
  203. If this options is set to true, the controlbox will by default be shown upon
  204. page load.
  205. xhr_user_search
  206. ---------------
  207. Default = False
  208. There are two ways to add users.
  209. * The user inputs a valid JID (Jabber ID), and the user is added as a pending contact.
  210. * The user inputs some text (for example part of a firstname or lastname), an XHR will be made to a backend, and a list of matches are returned. The user can then choose one of the matches to add as a contact.
  211. This setting enables the second mechanism, otherwise by default the first will
  212. be used.
  213. ============
  214. Minification
  215. ============
  216. Minifying Javascript
  217. ====================
  218. We use `require.js`_ to keep track of *Converse.js* and its dependencies and to
  219. to bundle them together in a single minified file fit for deployment to a
  220. production site.
  221. To use the require.js's optimization tool, you'll need Node and it's package
  222. manager, NPM.
  223. You can then install install require.js for Node like so:
  224. ::
  225. npm install requirejs
  226. The minified javascript file is then created like this:
  227. ::
  228. r.js -o build.js
  229. You should now have a new minified file (the name which is specified in build.js).
  230. You can `read more about require.js's optimizer here`_.
  231. Minifying CSS
  232. =============
  233. CSS can be minimized with Yahoo's yuicompressor tool:
  234. ::
  235. yui-compressor --type=css converse.css -o converse.min.css
  236. ============
  237. Translations
  238. ============
  239. The gettext POT file located in ./locale/converse.pot is the template
  240. containing all translations and from which for each language an individual PO
  241. file is generated.
  242. The POT file contains all translateable strings extracted from converse.js.
  243. To make a user facing string translateable, wrap it in the double underscore helper
  244. function like so:
  245. ::
  246. __('This string will be translated at runtime');
  247. After adding the string, you'll need to regenerate the POT file, like so:
  248. ::
  249. make pot
  250. You can then create or update the PO file for a specific language by doing the following:
  251. ::
  252. msgmerge ./locale/af/LC_MESSAGES/converse.po ./locale/converse.pot -U
  253. This PO file is then what gets translated.
  254. If you've created a new PO file, please make sure to add the following
  255. attributes at the top of the file (under *Content-Transfer-Encoding*). They are
  256. required as configuration settings for Jed, the Javascript translations library
  257. that we're using.
  258. ::
  259. "domain: converse\n"
  260. "lang: af\n"
  261. "plural_forms: nplurals=2; plural=(n != 1);\n"
  262. Unfortunately Jed cannot use the PO files directly. We have to generate from it
  263. a file in JSON format and then put that in a .js file for the specific
  264. language.
  265. To generate JSON from a PO file, you'll need po2json for node.js. Run the
  266. following command to install it (npm being the node.js package manager):
  267. ::
  268. npm install po2json
  269. You can then convert the translations into JSON format:
  270. ::
  271. po2json locale/af/LC_MESSAGES/converse.po locale/af/LC_MESSAGES/converse.json
  272. Now from converse.json paste the data as a value for the "locale_data" key in the
  273. object in the language's .js file.
  274. So, if you are for example translating into German (language code 'de'), you'll
  275. create or update the file ./locale/LC_MESSAGES/de.js with the following code:
  276. ::
  277. (function (root, factory) {
  278. define("af", ['jed'], function () {
  279. return factory(new Jed({
  280. "domain": "converse",
  281. "locale_data": {
  282. // Paste the JSON data from converse.json here
  283. }
  284. })
  285. }
  286. }(this, function (i18n) {
  287. return i18n;
  288. }));
  289. making sure to also paste the JSON data as value to the "locale_data" key.
  290. .. Note ::
  291. If you are adding translations for a new language that is not already supported,
  292. you'll have to make one more edit in ./locale/locales.js to make sure the
  293. language is loaded by require.js.
  294. Congratulations, you've now succesfully added your translations. Sorry for all
  295. those hoops you had to jump through.
  296. .. _`conversejs.org`: http://conversejs.org
  297. .. _`require.js`: http://requirejs.org
  298. .. _`read more about require.js's optimizer here`: http://requirejs.org/docs/optimization.html
  299. .. _`here`: http://metajack.im/2008/09/08/which-bosh-server-do-you-need/l
  300. .. _`HTTP`: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
  301. .. _`XMPP`: https://en.wikipedia.org/wiki/Xmpp
  302. .. _`Converse.js homepage`: http://conversejs.org
  303. .. _`CORS`: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
  304. .. _`Strophe.js plugin`: https://gist.github.com/1095825/6b4517276f26b66b01fa97b0a78c01275fdc6ff2
  305. .. _`xmpp.net`: http://xmpp.net
  306. .. _`xmpp.org`: http://xmpp.org/xmpp-software/servers/
  307. .. _`ejabberd`: http://www.ejabberd.im
  308. .. _`blogpost`: http://metajack.im/2008/10/03/getting-attached-to-strophe
  309. .. _`example Django application`: https://github.com/metajack/strophejs/tree/master/examples/attach