setup.rst 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. =====================
  2. Setup and integration
  3. =====================
  4. .. contents:: Table of Contents
  5. :depth: 2
  6. :local:
  7. .. _what-you-will-need:
  8. ------------------
  9. What you will need
  10. ------------------
  11. If you'd like to host your own version of converse.js or you would like to
  12. integrate it into your website, then you'll need to set up and configure some
  13. more server components.
  14. For example, if you want to allow chat accounts under your own domain (for
  15. example, the same domain as your website), then you will need to set up your
  16. own :ref:`XMPP server`.
  17. Besides an XMPP server, you also need a way for converse.js (which uses HTTP), to
  18. communicate with XMPP servers (which use XMPP).
  19. For this, you'll need :ref:`BOSH Connection Manager`.
  20. Lastly, if you want to maintain a single chat session for your website's users,
  21. you'll need to set up a BOSH session on your server, which converse.js can then
  22. connect to once the page loads. Please see the section: :ref:`session-support`.
  23. .. _`XMPP server`:
  24. --------------
  25. An XMPP server
  26. --------------
  27. *Converse.js* implements `XMPP <http://xmpp.org/about-xmpp/>`_ as its
  28. messaging protocol, and therefore needs to connect to an XMPP/Jabber
  29. server (Jabber is really just a synonym for XMPP).
  30. You can connect to public XMPP servers like ``jabber.org`` but if you want to
  31. have :ref:`session support <session-support>` you'll have to set up your own XMPP server.
  32. You can find a list of public XMPP servers/providers on `xmpp.net <http://xmpp.net>`_ and a list of
  33. servers that you can set up yourself on `xmpp.org <http://xmpp.org/xmpp-software/servers/>`_.
  34. .. _`BOSH connection manager`:
  35. -------------------------
  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. Websockets
  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 <prosody.im>`_ (from version 0.10) supports websocket connections, as
  67. does the node-xmpp-bosh connection manager.
  68. Overcoming cross-domain request restrictions
  69. ============================================
  70. Lets say your domain is *example.org*, but the domain of your connection
  71. manager is *example.com*.
  72. HTTP requests are made by *Converse.js* to the connection manager via XmlHttpRequests (XHR).
  73. Until recently, it was not possible to make such requests to a different domain
  74. than the one currently being served (to prevent XSS attacks).
  75. Luckily there is now a standard called
  76. `CORS <https://en.wikipedia.org/wiki/Cross-origin_resource_sharing>`_
  77. (Cross-origin resource sharing), which enables exactly that.
  78. Modern browsers support CORS, but there are problems with Internet Explorer < 10.
  79. IE 8 and 9 partially support CORS via a proprietary implementation called
  80. XDomainRequest. There is a `Strophe.js plugin <https://gist.github.com/1095825/6b4517276f26b66b01fa97b0a78c01275fdc6ff2>`_
  81. which you can use to enable support for XDomainRequest when it is present.
  82. In IE < 8, there is no support for CORS.
  83. Instead of using CORS, you can add a reverse proxy in
  84. Apache/Nginx which serves the connection manager under the same domain as your
  85. website. This will remove the need for any cross-domain XHR support.
  86. For example:
  87. ------------
  88. Assuming your site is accessible on port ``80`` for the domain ``mysite.com``
  89. and your connection manager manager is running at ``someothersite.com/http-bind``.
  90. The *bosh_service_url* value you want to give Converse.js to overcome
  91. the cross-domain restriction is ``mysite.com/http-bind`` and not
  92. ``someothersite.com/http-bind``.
  93. Your ``nginx`` or ``apache`` configuration will look as follows:
  94. Nginx
  95. ~~~~~
  96. .. code-block:: nginx
  97. http {
  98. server {
  99. listen 80
  100. server_name mysite.com;
  101. location ~ ^/http-bind/ {
  102. proxy_pass http://someothersite.com;
  103. }
  104. }
  105. }
  106. Apache
  107. ~~~~~~
  108. .. code-block:: apache
  109. <VirtualHost *:80>
  110. ServerName mysite.com
  111. RewriteEngine On
  112. RewriteRule ^/http-bind(.*) http://someothersite.com/http-bind$1 [P,L]
  113. </VirtualHost>
  114. .. _`session-support`:
  115. ----------------------
  116. Single Session Support
  117. ----------------------
  118. .. note::
  119. What is prebinding?
  120. ~~~~~~~~~~~~~~~~~~~
  121. **Prebind** refers to a technique whereby your web application sets up an
  122. authenticated BOSH session with a BOSH connection manager (which could be your
  123. XMPP server). Then later, in the browser, converse.js attaches to that
  124. session that was previously set up.
  125. This reduces network traffic and also speeds up loading times for
  126. converse.js. Additionally, because prebinding works with tokens, it's not necessary
  127. for the XMPP client to store users' passwords).
  128. Server-side authentication (prebind)
  129. ====================================
  130. It's possible to enable shared sessions whereby users already
  131. authenticated in your website will also automatically be logged in on the XMPP server,
  132. This session can also be made to persist across page loads. In other words, we want
  133. a user to automatically be logged in to chat when they log in to the website,
  134. and we want their chat session to persist across page loads.
  135. To do this you will require a `BOSH server <http://xmpp.org/about-xmpp/technology-overview/bosh/>`_
  136. for converse.js to connect to (see the :ref:`bosh-service-url` under :ref:`configuration-variables`)
  137. as well as a BOSH client in your web application (written for example in
  138. Python, Ruby or PHP) that will set up an authenticated BOSH session, which
  139. converse.js can then attach to.
  140. .. note::
  141. A BOSH server acts as a bridge between HTTP, the protocol of the web, and
  142. XMPP, the instant messaging protocol.
  143. Converse.js can only communicate via HTTP, but we need to communicate with
  144. an XMPP server in order to chat. So the BOSH server acts as a middle man,
  145. translating our HTTP requests into XMPP stanzas and vice versa.
  146. Jack Moffitt has a great `blogpost <http://metajack.im/2008/10/03/getting-attached-to-strophe>`_
  147. about this and even provides an
  148. `example Django application <https://github.com/metajack/strophejs/tree/master/examples/attach>`_
  149. to demonstrate it.
  150. When you authenticate to the XMPP server on your backend application (for
  151. example via a BOSH client in Django), you'll receive two tokens, RID (request ID) and SID (session ID).
  152. The **Session ID (SID)** is a unique identifier for the current *session*. This
  153. number stays constant for the entire session.
  154. The **Request ID (RID)** is a unique identifier for the current *request* (i.e.
  155. page load). Each page load is a new request which requires a new unique RID.
  156. The best way to achieve this is to simply increment the RID with each page
  157. load.
  158. You'll need to configure converse.js with the :ref:`prebind`, :ref:`keepalive` and
  159. :ref:`prebind_url` settings.
  160. Please read the documentation on those settings for a fuller picture of what
  161. needs to be done.
  162. Example code for server-side prebinding
  163. =======================================
  164. * PHP:
  165. See `xmpp-prebind-php <https://github.com/candy-chat/xmpp-prebind-php>`_ by
  166. Michael Weibel and the folks from Candy chat.
  167. * Python:
  168. See this `example Django application`_ by Jack Moffitt.