setup.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/setup.rst">Edit me on GitHub</a></div>
  3. .. _what-you-will-need:
  4. =====================
  5. Setup and integration
  6. =====================
  7. This page documents what you'll need to do to be able to connect Converse.js with
  8. your own XMPP server and to better integrate it into your website.
  9. At the very least you'll need Converse.js and an :ref:`XMPP server` with
  10. :ref:`websocket-section` or :ref:`BOSH-section` enabled. That's definitely
  11. enough to simply demo Converse.js or to do development work on it.
  12. However, if you want to more fully integrate it into a website or intranet,
  13. then you'll likely need to set up more services and components.
  14. The diagram below shows a fairly common setup for a website or intranet:
  15. * Converse.js runs in the web-browser on the user's device.
  16. * It communicates with the XMPP server via BOSH or websocket which is usually
  17. reverse-proxied by a web-server in order to overcome cross-site scripting
  18. restrictions in the browser. For more info on that, read the section:
  19. `Overcoming cross-domain request restrictions`_
  20. * Optionally the XMPP server is configured to use a SQL database for storing
  21. archived chat messages.
  22. * Optionally there is a user directory such as Active Directory or LDAP, which
  23. the XMPP server is configured to use, so that users can log in with those
  24. credentials.
  25. * Usually (but optionally) there is a backend web application which hosts a
  26. website in which Converse.js appears.
  27. .. figure:: images/diagram.png
  28. :align: center
  29. :alt: A diagram of a possible setup, consisting of Converse.js, a web server, a backend web application, an XMPP server, a user directory such as LDAP and an XMPP server.
  30. This diagram shows the various services in a fairly common setup (image generated with `draw.io <https://draw.io>`_).
  31. ----------------------
  32. The various components
  33. ----------------------
  34. .. _`XMPP server`:
  35. An XMPP server
  36. ==============
  37. *Converse.js* implements `XMPP <http://xmpp.org/about-xmpp/>`_ as its
  38. messaging protocol, and therefore needs to connect to an XMPP/Jabber
  39. server (Jabber® is an older and more user-friendly synonym for XMPP).
  40. You can connect to public XMPP servers like ``jabber.org`` but if you want to
  41. have :ref:`session support <session-support>` you'll have to set up your own XMPP server.
  42. You can find a list of public XMPP servers/providers on `xmpp.net <https://list.jabber.at>`_
  43. and a list of servers that you can set up yourself on `xmpp.org <http://xmpp.org/xmpp-software/servers/>`_.
  44. .. _`BOSH-section`:
  45. BOSH
  46. ====
  47. Web-browsers do not allow the persistent, direct TCP socket connections used by
  48. desktop XMPP clients to communicate with XMPP servers.
  49. Instead, we have HTTP and websocket as available protocols.
  50. `BOSH`_ can be seen as XMPP-over-HTTP. In other words, it allows for XMPP
  51. stanzas to be sent over an HTTP connection.
  52. HTTP connections are stateless and usually shortlived.
  53. XMPP connections on the other hand are stateful and usually last much longer.
  54. So to enable a web application like *Converse.js* to communicate with an XMPP
  55. server, we need a proxy which acts as a bridge between these two protocols.
  56. This is the job of a BOSH connection manager. BOSH (Bidirectional-streams Over
  57. Synchronous HTTP) is a protocol for allowing XMPP communication over HTTP. The
  58. protocol is defined in `XEP-0206: XMPP Over BOSH <http://xmpp.org/extensions/xep-0206.html>`_.
  59. Popular XMPP servers such as `Ejabberd <http://www.ejabberd.im>`_,
  60. prosody `(mod_bosh) <http://prosody.im/doc/setting_up_bosh>`_ and
  61. `OpenFire <http://www.igniterealtime.org/projects/openfire/>`_ all include
  62. their own BOSH connection managers (but you usually have to enable them in the
  63. configuration).
  64. However, if you intend to support multiple different servers (like
  65. https://conversejs.org does), then you'll need a standalone connection manager.
  66. For a standalone manager, see for example `Punjab <https://github.com/twonds/punjab>`_
  67. and `node-xmpp-bosh <https://github.com/dhruvbird/node-xmpp-bosh>`_.
  68. The demo on the `Converse.js homepage <http://conversejs.org>`_ uses a connection
  69. manager located at https://conversejs.org/http-bind.
  70. This connection manager is available for testing purposes only, please don't
  71. use it in production.
  72. Refer to the :ref:`bosh-service-url` configuration setting for information on
  73. how to configure Converse.js to connect to a BOSH URL.
  74. .. _`websocket-section`:
  75. Websocket
  76. =========
  77. Websockets provide an alternative means of connection to an XMPP server from
  78. your browser.
  79. Websockets provide long-lived, bidirectional connections which do not rely on
  80. HTTP. Therefore BOSH, which operates over HTTP, doesn't apply to websockets.
  81. `Prosody <http://prosody.im>`_ (from version 0.10) supports websocket connections, as
  82. does the node-xmpp-bosh connection manager.
  83. Refer to the :ref:`websocket-url` configuration setting for information on how to
  84. configure Converse.js to connect to a websocket URL.
  85. The Webserver
  86. =============
  87. Overcoming cross-domain request restrictions
  88. --------------------------------------------
  89. Lets say your domain is *example.org*, but the domain of your connection
  90. manager is *example.com*.
  91. HTTP requests are made by *Converse.js* to the BOSH connection manager via
  92. XmlHttpRequests (XHR). Until recently, it was not possible to make such
  93. requests to a different domain than the one currently being served
  94. (to prevent XSS attacks).
  95. Luckily there is now a standard called
  96. `CORS <https://en.wikipedia.org/wiki/Cross-origin_resource_sharing>`_
  97. (Cross-origin resource sharing), which enables exactly that.
  98. Modern browsers support CORS, but there are problems with Internet Explorer < 10.
  99. IE 8 and 9 partially support CORS via a proprietary implementation called
  100. XDomainRequest. There is a `Strophe.js plugin <https://gist.github.com/1095825/6b4517276f26b66b01fa97b0a78c01275fdc6ff2>`_
  101. which you can use to enable support for XDomainRequest when it is present.
  102. In IE < 8, there is no support for CORS.
  103. Instead of using CORS, you can add a reverse proxy in
  104. Apache/Nginx which serves the connection manager under the same domain as your
  105. website. This will remove the need for any cross-domain XHR support.
  106. Examples:
  107. *********
  108. Assuming your site is accessible on port ``80`` for the domain ``mysite.com``
  109. and your connection manager manager is running at ``someothersite.com/http-bind``.
  110. The *bosh_service_url* value you want to give Converse.js to overcome
  111. the cross-domain restriction is ``mysite.com/http-bind`` and not
  112. ``someothersite.com/http-bind``.
  113. Your ``nginx`` or ``apache`` configuration will look as follows:
  114. Nginx
  115. ~~~~~
  116. .. code-block:: nginx
  117. http {
  118. server {
  119. listen 80
  120. server_name mysite.com;
  121. location ~ ^/http-bind/ {
  122. proxy_pass http://someothersite.com;
  123. }
  124. }
  125. }
  126. Apache
  127. ~~~~~~
  128. .. code-block:: apache
  129. <VirtualHost *:80>
  130. ServerName mysite.com
  131. RewriteEngine On
  132. RewriteRule ^/http-bind(.*) http://someothersite.com/http-bind$1 [P,L]
  133. </VirtualHost>
  134. .. _`session-support`:
  135. Single Session Support
  136. ======================
  137. It's possible to enable shared sessions whereby users already
  138. logged in to your website will also automatically be logged in on the XMPP server,
  139. Once a user is logged in, the session will be kept alive across page loads by
  140. way of the :ref:`keepalive` setting.
  141. There are a few ways to let your users be automatically authenticated to an
  142. XMPP server once they've logged in to your site.
  143. Option 1). Server-side authentication via BOSH prebinding
  144. ---------------------------------------------------------
  145. To **prebind** refers to a technique whereby your web application sets up an
  146. authenticated BOSH session with the XMPP server or a standalone `BOSH <http://xmpp.org/about-xmpp/technology-overview/bosh/>`_
  147. connection manager.
  148. Once authenticated, it receives RID and SID tokens which need to be passed
  149. on to converse.js upon pa. Converse.js will then attach to that same session using
  150. those tokens.
  151. It's called "prebind" because you bind to the BOSH session beforehand, and then
  152. later in the page you just attach to that session again.
  153. The RID and SID tokens can be passed in manually when calling
  154. `converse.initialize`, but a more convenient way is to pass converse.js a :ref:`prebind_url`
  155. which it will call when it needs the tokens. This way it will be able to
  156. automatically reconnect whenever the connection drops, by simply calling that
  157. URL again to fetch new tokens.
  158. Prebinding reduces network traffic and also speeds up the startup time for
  159. converse.js. Additionally, because prebind works with tokens, it's not necessary
  160. for the XMPP client to know or store users' passwords.
  161. One potential drawback of using prebind is that in order to establish the
  162. authenticated BOSH session server-side, you'll need to access and pass on the XMPP
  163. credentials server-side, which, unless you're using tokens, means that you'll
  164. need to store XMPP passwords in cleartext.
  165. This is however not the case if you for example use LDAP or Active Directory as
  166. your authentication backend, since you could then configure your XMPP server to
  167. use that as well.
  168. To prebind you will require a BOSH-enabled XMPP server for converse.js to connect to
  169. (see the :ref:`bosh-service-url` under :ref:`configuration-settings`)
  170. as well as a BOSH client in your web application (written for example in
  171. Python, Ruby or PHP) that will set up an authenticated BOSH session, which
  172. converse.js can then attach to.
  173. .. note::
  174. A BOSH server acts as a bridge between HTTP, the protocol of the web, and
  175. XMPP, the instant messaging protocol.
  176. Converse.js can only communicate via HTTP (or websocket, in which case BOSH can't be used).
  177. It cannot open TCP sockets to communicate to an XMPP server directly.
  178. So the BOSH server acts as a middle man, translating our HTTP requests into XMPP stanzas and vice versa.
  179. Jack Moffitt has a great `blogpost <http://metajack.im/2008/10/03/getting-attached-to-strophe>`_
  180. about this and even provides an
  181. `example Django application <https://github.com/metajack/strophejs/tree/master/examples/attach>`_
  182. to demonstrate it.
  183. When you authenticate to the XMPP server on your backend application (for
  184. example via a BOSH client in Django), you'll receive two tokens, RID (request ID) and SID (session ID).
  185. The **Session ID (SID)** is a unique identifier for the current *session*. This
  186. number stays constant for the entire session.
  187. The **Request ID (RID)** is a unique identifier for the current *request* (i.e.
  188. page load). Each page load is a new request which requires a new unique RID.
  189. The best way to achieve this is to simply increment the RID with each page
  190. load.
  191. You'll need to configure converse.js with the ``prebind``, :ref:`keepalive` and
  192. :ref:`prebind_url` settings.
  193. Please read the documentation on those settings for a fuller picture of what
  194. needs to be done.
  195. Example code for server-side prebinding
  196. ***************************************
  197. * PHP:
  198. See `xmpp-prebind-php <https://github.com/candy-chat/xmpp-prebind-php>`_ by
  199. Michael Weibel and the folks from Candy chat.
  200. * Python:
  201. See this `example Django application`_ by Jack Moffitt.
  202. Option 2). Delegated authentication, also called external authentication
  203. ------------------------------------------------------------------------
  204. Delegated authentication refers to the usecase where the XMPP server delegates
  205. authentication to some other service.
  206. This could be to LDAP or Active Directory (as shown in the diagram at the top
  207. of the page), or it could be to an OAuth provider, a SQL server to a specific
  208. website.
  209. The Prosody webserver has various user-contributed modules which delegate
  210. authentication to external services. They are listed in the `Prosody community modules
  211. page <https://modules.prosody.im/>`_. Other XMPP servers have similar plugin modules.
  212. If your web-application has access to the same credentials, it can send those
  213. credentials to Converse.js so that user's are automatically logged in when the
  214. page loads.
  215. This is can be done by setting :ref:`auto_login` to true and configuring the
  216. the :ref:`credentials_url` setting.
  217. Option 3). Temporary authentication tokens
  218. ------------------------------------------
  219. The first option has the drawback that your web-application needs to know the
  220. XMPP credentials of your users and that they need to be stored in the clear.
  221. The second option has that same drawback and it also needs to pass those
  222. credentials to Converse.js.
  223. To avoid these drawbacks, you can instead let your backend web application
  224. generate temporary authentication tokens which are then sent to the XMPP server
  225. which in turn delegates authentication to an external authentication provider
  226. (generally the same web-app that generated the tokens).
  227. This can be combined with prebind or with the :ref:`credentials_url` setting.
  228. Option 4). Cryptographically signed tokens
  229. ------------------------------------------
  230. A third potential option is to generate cryptographically signed tokens (e.g.
  231. HMAC tokens) which the XMPP server could authenticate by checking that they're
  232. signed with the right key and that they conform to some kind of pre-arranged
  233. format.
  234. In this case, you would also use the :ref:`credentials_url` setting, to specify a
  235. URL from which converse.js should fetch the username and token.