index.txt 19 KB

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