setup.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 with
  8. your own XMPP server and to better integrate it into your website.
  9. At the very least you'll need Converse and an :ref:`XMPP server` with
  10. :ref:`websocket-section` or :ref:`BOSH-section` enabled. That's definitely
  11. enough to simply demo Converse or to do development work on it.
  12. For end-to-end encryption via OMEMO, you'll need to load `libsignal-protocol.js
  13. <https://github.com/signalapp/libsignal-protocol-javascript>`_ separately in
  14. your page. Take a look at the section on :ref:`libsignal <dependency-libsignal>` and the
  15. :ref:`security considerations around OMEMO <feature-omemo>`.
  16. If you want to more fully integrate it into a website
  17. then you'll likely need to set up more services and components.
  18. The diagram below shows a fairly common setup for a website or intranet:
  19. * Converse runs in the web-browser on the user's device.
  20. * It communicates with the XMPP server via BOSH or websocket which is usually
  21. reverse-proxied by a web-server in order to overcome cross-site scripting
  22. restrictions in the browser.
  23. * Optionally the XMPP server is configured to use a SQL database for storing
  24. archived chat messages.
  25. * Optionally there is a user directory such as Active Directory or LDAP, which
  26. the XMPP server is configured to use, so that users can log in with those
  27. credentials.
  28. * Usually (but optionally) there is a backend web application which hosts a
  29. website in which Converse appears.
  30. .. figure:: images/diagram.png
  31. :align: center
  32. :alt: A diagram of a possible setup, consisting of Converse, a web server, a backend web application, an XMPP server, a user directory such as LDAP and an XMPP server.
  33. This diagram shows the various services in a fairly common setup (image generated with `draw.io <https://draw.io>`_).
  34. ----------------------
  35. The various components
  36. ----------------------
  37. .. _`XMPP server`:
  38. An XMPP server
  39. ==============
  40. Converse uses `XMPP <https://xmpp.org/about-xmpp/>`_ as its
  41. messaging protocol, and therefore needs to connect to an XMPP/Jabber
  42. server (Jabber® is an older and more user-friendly synonym for XMPP).
  43. You can connect to public XMPP servers like ``conversejs.org`` but if you want to
  44. integrate Converse into your own website and to use your website's
  45. authentication sessions to log in users to the XMPP server (i.e. :ref:`session support <session-support>`)
  46. then you'll have to set up your own XMPP server.
  47. You can find a list of public XMPP servers/providers on `compliance.conversations.im <http://compliance.conversations.im/>`_
  48. and a list of servers that you can set up yourself on `xmpp.org <https://xmpp.org/xmpp-software/servers/>`_.
  49. .. _`BOSH-section`:
  50. BOSH (XMPP-over-HTTP)
  51. =====================
  52. Web-browsers do not allow the persistent, direct TCP socket connections used by
  53. desktop XMPP clients to communicate with XMPP servers.
  54. Instead, we have HTTP and websocket as available protocols.
  55. `BOSH`_ can be seen as XMPP-over-HTTP. In other words, it allows for XMPP
  56. stanzas to be sent over an HTTP connection.
  57. HTTP connections are stateless and usually shortlived.
  58. XMPP connections on the other hand are stateful and usually last much longer.
  59. So to enable a web application like Converse to communicate with an XMPP
  60. server, we need a proxy which acts as a bridge between these two protocols.
  61. This is the job of a BOSH connection manager. BOSH (Bidirectional-streams Over
  62. Synchronous HTTP) is a protocol for allowing XMPP communication over HTTP. The
  63. protocol is defined in `XEP-0206: XMPP Over BOSH <https://xmpp.org/extensions/xep-0206.html>`_.
  64. Popular XMPP servers such as `Ejabberd <http://www.ejabberd.im>`_,
  65. Prosody `(mod_bosh) <http://prosody.im/doc/setting_up_bosh>`_ and
  66. `OpenFire <http://www.igniterealtime.org/projects/openfire/>`_ all include
  67. their own BOSH connection managers (but you usually have to enable them in the
  68. configuration).
  69. However, if you intend to support multiple different servers (like
  70. https://conversejs.org does), then you'll need a standalone connection manager.
  71. For a standalone manager, see for example `Punjab <https://github.com/twonds/punjab>`_
  72. and `node-xmpp-bosh <https://github.com/dhruvbird/node-xmpp-bosh>`_.
  73. The demo on the `Converse homepage <https://conversejs.org>`_ uses a connection
  74. manager located at https://conversejs.org/http-bind.
  75. This connection manager is available for testing purposes only, please don't
  76. use it in production.
  77. Refer to the :ref:`bosh-service-url` configuration setting for information on
  78. how to configure Converse to connect to a BOSH URL.
  79. Configuring your webserver for BOSH
  80. -----------------------------------
  81. Lets say the domain under which you host Converse is *example.org:80*,
  82. but the domain of your connection manager or the domain of
  83. your HTTP file server (for `XEP-0363 HTTP File Upload <https://xmpp.org/extensions/xep-0363.html>`_)
  84. is at a different domain, either a different port like *example.org:5280* or a
  85. different name like *elsehwere.org*.
  86. In such a situation the same-origin security policy of the browser comes into force.
  87. For security purposes a browser does not by default allow a website to
  88. make certain types of requests to other domains.
  89. There are two ways in which you can solve this problem.
  90. .. _CORS:
  91. 1. Cross-Origin Resource Sharing (CORS)
  92. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. CORS is a technique for overcoming browser restrictions related to the
  94. `same-origin security policy <https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy>`_.
  95. CORS is enabled by adding an ``Access-Control-Allow-Origin`` header. Where this
  96. is configured depends on what webserver is used for your file upload server.
  97. 2. Reverse-proxy
  98. ~~~~~~~~~~~~~~~~
  99. Another possible solution is to add a reverse proxy to a webserver such as Nginx or Apache to ensure that
  100. all services you use are hosted under the same domain name and port.
  101. Examples:
  102. *********
  103. Assuming your site is accessible on port ``80`` for the domain ``mysite.com``
  104. and your connection manager manager is running at ``someothersite.com/http-bind``.
  105. The *bosh_service_url* value you want to give Converse to overcome
  106. the cross-domain restriction is ``mysite.com/http-bind`` and not
  107. ``someothersite.com/http-bind``.
  108. Your ``nginx`` or ``apache`` configuration will look as follows:
  109. Nginx
  110. ^^^^^
  111. .. code-block:: nginx
  112. http {
  113. server {
  114. listen 80
  115. server_name mysite.com;
  116. location = / {
  117. root /path/to/converse.js/; # Properly set the path here
  118. index index.html;
  119. }
  120. location ~ ^/http-bind/ {
  121. proxy_pass http://someothersite.com;
  122. }
  123. # CORS
  124. location ~ .(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$ {
  125. add_header Access-Control-Allow-Origin "*"; # Decide here whether you want to allow all or only a particular domain
  126. root /path/to/converse.js/; # Properly set the path here
  127. }
  128. }
  129. }
  130. Apache
  131. ^^^^^^
  132. .. code-block:: apache
  133. <VirtualHost *:80>
  134. ServerName mysite.com
  135. RewriteEngine On
  136. RewriteRule ^/http-bind(.*) http://someothersite.com/http-bind$1 [P,L]
  137. </VirtualHost>
  138. .. note::
  139. If you're getting XML parsing errors for your BOSH endpoint, for
  140. example::
  141. XML Parsing Error: mismatched tag. Expected: </hr>.
  142. Location: https://example.org/http-bind/
  143. Line Number 6, Column 3: bosh-anon:6:3
  144. Also ERROR: request id 12.2 error 504 happened
  145. Then your BOSH proxy is returning an HTML error page (for a 504 error in
  146. the above example).
  147. This might be because your webserver and BOSH proxy have the same timeout
  148. for BOSH requests. Because the webserver receives the request slightly earlier,
  149. it gives up a few microseconds before the XMPP server’s empty result and thus returns a
  150. 504 error page containing HTML to browser, which then gets parsed as if its
  151. XML.
  152. To fix this, make sure that the webserver's timeout is slightly higher.
  153. In Nginx you can do this by adding ``proxy_read_timeout 61;``;
  154. From Converse 4.0.0 onwards the default ``wait`` time is set to 59 seconds, to avoid
  155. this problem.
  156. .. _`websocket-section`:
  157. Websocket
  158. =========
  159. Websockets provide an alternative means of connection to an XMPP server from
  160. your browser.
  161. Websockets provide long-lived, bidirectional connections which do not rely on
  162. HTTP. Therefore BOSH, which operates over HTTP, doesn't apply to websockets.
  163. `Prosody <http://prosody.im>`_ (from version 0.10) and `Ejabberd <http://www.ejabberd.im>`_ support websocket connections, as
  164. does the node-xmpp-bosh connection manager.
  165. Refer to the :ref:`websocket-url` configuration setting for information on how to
  166. configure Converse to connect to a websocket URL.
  167. Reverse-proxy for a websocket connection
  168. ----------------------------------------
  169. Assuming your website is accessible on port ``443`` on the domain ``mysite.com``
  170. and your XMPP server's websocket server is running at ``localhost:5280/xmpp-websocket``.
  171. You can then set up your webserver as an SSL enabled reverse proxy in front of
  172. your websocket endpoint.
  173. The :ref:`websocket-url` value you'll want to pass in to ``converse.initialize`` is ``wss://mysite.com/xmpp-websocket``.
  174. Your ``nginx`` will look as follows:
  175. .. code-block:: nginx
  176. http {
  177. server {
  178. listen 443
  179. server_name mysite.com;
  180. ssl on;
  181. ssl_certificate /path/to/fullchain.pem; # Properly set the path here
  182. ssl_certificate_key /path/to/privkey.pem; # Properly set the path here
  183. location = / {
  184. root /path/to/converse.js/; # Properly set the path here
  185. index index.html;
  186. }
  187. location /xmpp-websocket {
  188. proxy_http_version 1.1;
  189. proxy_pass http://127.0.0.1:5280;
  190. proxy_buffering off;
  191. proxy_set_header Host $host;
  192. proxy_set_header Upgrade $http_upgrade;
  193. proxy_set_header Connection "upgrade";
  194. proxy_read_timeout 86400;
  195. }
  196. # CORS
  197. location ~ .(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$ {
  198. add_header Access-Control-Allow-Origin "*"; # Decide here whether you want to allow all or only a particular domain
  199. root /path/to/converse.js/; # Properly set the path here
  200. }
  201. }
  202. }