index.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. Pre-binding and Single 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. .. Note::
  80. If you want to enable single session support, make sure to pass **prebind: true**
  81. when you call **converse.initialize** (see ./main.js).
  82. When you authenticate to the XMPP server on your backend, you'll receive two
  83. tokens, RID (request ID) and SID (session ID).
  84. These tokens then need to be passed back to the javascript running in your
  85. browser, where you will need them attach to the existing session.
  86. You can embed the RID and SID tokens in your HTML markup or you can do an
  87. XMLHttpRequest call to you server and ask it to return them for you.
  88. Below is one example of how this could work. An Ajax call is made to the
  89. relative URL **/prebind** and it expects to receive JSON data back.
  90. ::
  91. $.getJSON('/prebind', function (data) {
  92. var connection = new Strophe.Connection(converse.bosh_service_url);
  93. connection.attach(data.jid, data.sid, data.rid, function (status) {
  94. if ((status === Strophe.Status.ATTACHED) || (status === Strophe.Status.CONNECTED)) {
  95. converse.onConnected(connection)
  96. }
  97. });
  98. }
  99. );
  100. **Here's what's happening:**
  101. The JSON data contains the user's JID (jabber ID), RID and SID. The URL to the
  102. BOSH connection manager is already set as a configuration setting on the
  103. *converse* object (see ./main.js), so we can reuse it from there.
  104. A new Strophe.Connection object is instantiated and then *attach* is called with
  105. the user's JID, the necessary tokens and a callback function.
  106. In the callback function, you call *converse.onConnected* together with the
  107. connection object.
  108. =========================================
  109. Quickstart (to get a demo up and running)
  110. =========================================
  111. When you download a specific release of *Converse.js*, say for example version 0.3,
  112. there will be two minified files inside the zip file.
  113. For version 0.3 they will be:
  114. * converse.0.3.min.js
  115. * converse.0.3.min.css
  116. You can include these two files in your website via the *script* and *link*
  117. tags:
  118. ::
  119. <link rel="stylesheet" type="text/css" media="screen" href="converse.0.3.min.css">
  120. <script src="converse.0.3.min.js"></script>
  121. The *index.html* file inside the Converse.js folder serves as a nice usable
  122. example of this.
  123. These minified files provide the same demo-like functionality as is available
  124. on the `conversejs.org`_ website. Useful for testing or demoing, but not very
  125. practical.
  126. You'll most likely want to implement some kind of single-signon solution for
  127. your website, where users authenticate once in your website and then stay
  128. logged into their XMPP session upon page reload.
  129. For more info on this, read `Session Support`_.
  130. You might also want to have more fine-grained control of what gets included in
  131. the minified Javascript file. Read `Configuration`_ and `Minification`_ for more info on how to do
  132. that.
  133. =============
  134. Configuration
  135. =============
  136. The included minified JS and CSS files can be used for demoing or testing, but
  137. you'll want to configure *Converse.js* to suit your needs before you deploy it
  138. on your website.
  139. *Converse.js* is passed its configuration settings when you call its
  140. *initialize* method.
  141. Please refer to the `Configuration variables`_ section below for info on
  142. all the available configuration settings.
  143. After you have configured *Converse.js*, you'll have to regenerate the minified
  144. JS file so that it will include the new settings. Please refer to the
  145. `Minification`_ section for more info on how to do this.
  146. Configuration variables
  147. =======================
  148. animate
  149. -------
  150. Default = True
  151. Show animations, for example when opening and closing chat boxes.
  152. auto_list_rooms
  153. ---------------
  154. Default = False
  155. If true, and the XMPP server on which the current user is logged in supports
  156. multi-user chat, then a list of rooms on that server will be fetched.
  157. Not recommended for servers with lots of chat rooms.
  158. For each room on the server a query is made to fetch further details (e.g.
  159. features, number of occupants etc.), so on servers with many rooms this
  160. option will create lots of extra connection traffic.
  161. auto_subscribe
  162. --------------
  163. Default = False
  164. If true, the user will automatically subscribe back to any contact requests.
  165. bosh_service_url
  166. ----------------
  167. Connections to an XMPP server depend on a BOSH connection manager which acts as
  168. a middle man between HTTP and XMPP.
  169. See `here`_ for more information.
  170. fullname
  171. --------
  172. If you are using prebinding, you need to specify the fullname of the currently
  173. logged in user.
  174. hide_muc_server
  175. ---------------
  176. Default = False
  177. Hide the ``server`` input field of the form inside the ``Room`` panel of the
  178. controlbox. Useful if you want to restrict users to a specific XMPP server of
  179. your choosing.
  180. prebind
  181. --------
  182. Default = False
  183. Use this option when you want to attach to an existing XMPP connection that was
  184. already authenticated (usually on the backend before page load).
  185. This is useful when you don't want to render the login form on the chat control
  186. box with each page load.
  187. When set to true, you'll need to make sure that the onConnected method is
  188. called, and passed to it a Strophe connection object.
  189. Besides requiring the back-end to authenticate you, you'll also
  190. have to write a Javascript snippet to attach to the set up connection::
  191. $.JSON({
  192. 'url': 'mysite.com/xmpp-authenticate',
  193. 'success': function (data) {
  194. connection = new Strophe.Connection(bosh_service_url);
  195. connection.attach(data.jid, data.sid, data.rid, converse.onConnected);
  196. }
  197. The backend must authenticate for you, and then return a SID (session ID) and
  198. RID (Request ID), which you use when you attach to the connection.
  199. show_controlbox_by_default
  200. --------------------------
  201. Default = False
  202. The "controlbox" refers to the special chatbox containing your contacts roster,
  203. status widget, chatrooms and other controls.
  204. By default this box is hidden and can be toggled by clicking on any element in
  205. the page with class *toggle-online-users*.
  206. If this options is set to true, the controlbox will by default be shown upon
  207. page load.
  208. xhr_user_search
  209. ---------------
  210. Default = False
  211. There are two ways to add users.
  212. * The user inputs a valid JID (Jabber ID), and the user is added as a pending contact.
  213. * 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.
  214. This setting enables the second mechanism, otherwise by default the first will
  215. be used.
  216. ============
  217. Minification
  218. ============
  219. Minifying Javascript
  220. ====================
  221. We use `require.js`_ to keep track of *Converse.js* and its dependencies and to
  222. to bundle them together in a single minified file fit for deployment to a
  223. production site.
  224. To use the require.js's optimization tool, you'll need Node and it's package
  225. manager, NPM.
  226. You can then install install require.js for Node like so:
  227. ::
  228. npm install requirejs
  229. The minified javascript file is then created like this:
  230. ::
  231. r.js -o build.js
  232. You should now have a new minified file (the name which is specified in build.js).
  233. You can `read more about require.js's optimizer here`_.
  234. Minifying CSS
  235. =============
  236. CSS can be minimized with Yahoo's yuicompressor tool:
  237. ::
  238. yui-compressor --type=css converse.css -o converse.min.css
  239. ============
  240. Translations
  241. ============
  242. The gettext POT file located in ./locale/converse.pot is the template
  243. containing all translations and from which for each language an individual PO
  244. file is generated.
  245. The POT file contains all translateable strings extracted from converse.js.
  246. To make a user facing string translateable, wrap it in the double underscore helper
  247. function like so:
  248. ::
  249. __('This string will be translated at runtime');
  250. After adding the string, you'll need to regenerate the POT file, like so:
  251. ::
  252. make pot
  253. You can then create or update the PO file for a specific language by doing the following:
  254. ::
  255. msgmerge ./locale/af/LC_MESSAGES/converse.po ./locale/converse.pot -U
  256. This PO file is then what gets translated.
  257. If you've created a new PO file, please make sure to add the following
  258. attributes at the top of the file (under *Content-Transfer-Encoding*). They are
  259. required as configuration settings for Jed, the Javascript translations library
  260. that we're using.
  261. ::
  262. "domain: converse\n"
  263. "lang: af\n"
  264. "plural_forms: nplurals=2; plural=(n != 1);\n"
  265. Unfortunately Jed cannot use the PO files directly. We have to generate from it
  266. a file in JSON format and then put that in a .js file for the specific
  267. language.
  268. To generate JSON from a PO file, you'll need po2json for node.js. Run the
  269. following command to install it (npm being the node.js package manager):
  270. ::
  271. npm install po2json
  272. You can then convert the translations into JSON format:
  273. ::
  274. po2json locale/af/LC_MESSAGES/converse.po locale/af/LC_MESSAGES/converse.json
  275. Now from converse.json paste the data as a value for the "locale_data" key in the
  276. object in the language's .js file.
  277. So, if you are for example translating into German (language code 'de'), you'll
  278. create or update the file ./locale/LC_MESSAGES/de.js with the following code:
  279. ::
  280. (function (root, factory) {
  281. define("af", ['jed'], function () {
  282. return factory(new Jed({
  283. "domain": "converse",
  284. "locale_data": {
  285. // Paste the JSON data from converse.json here
  286. }
  287. })
  288. }
  289. }(this, function (i18n) {
  290. return i18n;
  291. }));
  292. making sure to also paste the JSON data as value to the "locale_data" key.
  293. .. Note ::
  294. If you are adding translations for a new language that is not already supported,
  295. you'll have to make one more edit in ./locale/locales.js to make sure the
  296. language is loaded by require.js.
  297. Congratulations, you've now succesfully added your translations. Sorry for all
  298. those hoops you had to jump through.
  299. .. _`conversejs.org`: http://conversejs.org
  300. .. _`require.js`: http://requirejs.org
  301. .. _`read more about require.js's optimizer here`: http://requirejs.org/docs/optimization.html
  302. .. _`here`: http://metajack.im/2008/09/08/which-bosh-server-do-you-need/l
  303. .. _`HTTP`: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
  304. .. _`XMPP`: https://en.wikipedia.org/wiki/Xmpp
  305. .. _`Converse.js homepage`: http://conversejs.org
  306. .. _`CORS`: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
  307. .. _`Strophe.js plugin`: https://gist.github.com/1095825/6b4517276f26b66b01fa97b0a78c01275fdc6ff2
  308. .. _`xmpp.net`: http://xmpp.net
  309. .. _`xmpp.org`: http://xmpp.org/xmpp-software/servers/
  310. .. _`ejabberd`: http://www.ejabberd.im
  311. .. _`blogpost`: http://metajack.im/2008/10/03/getting-attached-to-strophe
  312. .. _`example Django application`: https://github.com/metajack/strophejs/tree/master/examples/attach