setup.rst 11 KB

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