2
0

index.rst 17 KB

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