setup.rst 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. =====================
  2. Setup and integration
  3. =====================
  4. .. contents:: Table of Contents
  5. :depth: 2
  6. :local:
  7. .. _what-you-will-need:
  8. --------------
  9. An XMPP server
  10. --------------
  11. *Converse.js* implements `XMPP <http://xmpp.org/about-xmpp/>`_ as its
  12. messaging protocol, and therefore needs to connect to an XMPP/Jabber
  13. server (Jabber is really just a synonym for XMPP).
  14. You can connect to public XMPP servers like ``jabber.org`` but if you want to
  15. have :ref:`session support <session-support>` you'll have to set up your own XMPP server.
  16. You can find a list of public XMPP servers/providers on `xmpp.net <http://xmpp.net>`_ and a list of
  17. servers that you can set up yourself on `xmpp.org <http://xmpp.org/xmpp-software/servers/>`_.
  18. -------------------------
  19. A BOSH Connection Manager
  20. -------------------------
  21. Your website and *Converse.js* use `HTTP <https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol>`_
  22. as protocol to communicate with the webserver. HTTP connections are stateless and usually shortlived.
  23. `XMPP <https://en.wikipedia.org/wiki/Xmpp>`_ on the other hand, is the protocol that enables instant messaging, and
  24. its connections are stateful and usually longer.
  25. To enable a web application like *Converse.js* to communicate with an XMPP
  26. server, we need a proxy in the middle that can act as a bridge between the two
  27. protocols.
  28. The `index.html <https://github.com/jcbrand/converse.js/blob/master/index.html>`_ file inside the
  29. This is the job of a connection manager. A connection manager can be either a
  30. standalone application or part of an XMPP server. Popular XMPP servers such as
  31. `ejabberd <http://www.ejabberd.im>`_, `prosody <http://prosody.im/doc/setting_up_bosh>`_ and
  32. `openfire <http://www.igniterealtime.org/projects/openfire/>`_ all include their own connection managers
  33. (but you usually have to enable them in the configuration).
  34. Standalone connection managers also exist, see for example `Punjab <https://github.com/twonds/punjab>`_.
  35. The demo on the `Converse.js homepage <http://conversejs.org>`_ uses a connection manager located at https://bind.conversejs.org.
  36. This connection manager is available for testing purposes only, please don't use it in production.
  37. Overcoming cross-domain request restrictions
  38. ============================================
  39. Lets say your domain is *example.org*, but the domain of your connection
  40. manager is *example.com*.
  41. HTTP requests are made by *Converse.js* to the connection manager via XmlHttpRequests (XHR).
  42. Until recently, it was not possible to make such requests to a different domain
  43. than the one currently being served (to prevent XSS attacks).
  44. Luckily there is now a standard called
  45. `CORS <https://en.wikipedia.org/wiki/Cross-origin_resource_sharing>`_
  46. (Cross-origin resource sharing), which enables exactly that.
  47. Modern browsers support CORS, but there are problems with Internet Explorer < 10.
  48. IE 8 and 9 partially support CORS via a proprietary implementation called
  49. XDomainRequest. There is a `Strophe.js plugin <https://gist.github.com/1095825/6b4517276f26b66b01fa97b0a78c01275fdc6ff2>`_
  50. which you can use to enable support for XDomainRequest when it is present.
  51. In IE < 8, there is no support for CORS.
  52. Instead of using CORS, you can add a reverse proxy in
  53. Apache/Nginx which serves the connection manager under the same domain as your
  54. website. This will remove the need for any cross-domain XHR support.
  55. For example:
  56. ------------
  57. Assuming your site is accessible on port ``80`` for the domain ``mysite.com``
  58. and your connection manager manager is running at ``someothersite.com/http-bind``.
  59. The *bosh_service_url* value you want to give Converse.js to overcome
  60. the cross-domain restriction is ``mysite.com/http-bind`` and not
  61. ``someothersite.com/http-bind``.
  62. Your ``nginx`` or ``apache`` configuration will look as follows:
  63. Nginx
  64. -----
  65. .. code-block:: nginx
  66. http {
  67. server {
  68. listen 80
  69. server_name mysite.com;
  70. location ~ ^/http-bind/ {
  71. proxy_pass http://someothersite.com;
  72. }
  73. }
  74. }
  75. Apache
  76. ------
  77. .. code-block:: apache
  78. <VirtualHost *:80>
  79. ServerName mysite.com
  80. RewriteEngine On
  81. RewriteRule ^/http-bind(.*) http://someothersite.com/http-bind$1 [P,L]
  82. </VirtualHost>
  83. .. _`session-support`:
  84. ----------------------
  85. Single Session Support
  86. ----------------------
  87. Server-side authentication
  88. ==========================
  89. It's possible to enable single-site login, whereby users already
  90. authenticated in your website will also automatically be logged in on the chat server,
  91. This session should also persist across page loads. In other words, we don't
  92. want the user to have to give their chat credentials every time they reload the
  93. page.
  94. To do this you will require a `BOSH server <http://xmpp.org/about-xmpp/technology-overview/bosh/>`_
  95. for converse.js to connect to (see the :ref:`bosh-service-url` under :ref:`configuration-variables`)
  96. as well as a BOSH client on your own server (written for example in Python, Ruby or PHP) that will
  97. do the pre-authentication before the web page loads.
  98. .. note::
  99. A BOSH server acts as a bridge between HTTP, the protocol of the web, and
  100. XMPP, the instant messaging protocol.
  101. Converse.js can only communicate via HTTP, but we need to communicate with
  102. an XMPP server in order to chat. So the BOSH server acts as a middle man,
  103. translating our HTTP requests into XMPP stanzas and vice versa.
  104. Jack Moffitt has a great `blogpost <http://metajack.im/2008/10/03/getting-attached-to-strophe>`_
  105. about this and even provides an
  106. `example Django application <https://github.com/metajack/strophejs/tree/master/examples/attach>`_
  107. to demonstrate it.
  108. When you authenticate to the XMPP server on your backend application (for
  109. example via a BOSH client in Django), you'll receive two tokens, RID (request ID) and SID (session ID).
  110. The **Session ID (SID)** is a unique identifier for the current *session*. This
  111. number stays constant for the entire session.
  112. The **Request ID (RID)** is a unique identifier for the current *request* (i.e.
  113. page load). Each page load is a new request which requires a new unique RID.
  114. The best way to achieve this is to simply increment the RID with each page
  115. load.
  116. When you initialize converse.js in your browser, you need to pass it these two
  117. tokens. Converse.js will then use them to attach to the session you just
  118. created.
  119. You can embed the RID and SID tokens in your HTML markup or you can do an
  120. XMLHttpRequest call to your server and ask it to return them for you.
  121. Below is one example of how this could work. An Ajax call is made to the
  122. relative URL **/prebind** and it expects to receive JSON data back.
  123. .. code-block:: javascript
  124. $.getJSON('/prebind', function (data) {
  125. converse.initialize({
  126. prebind: true,
  127. bosh_service_url: data.bosh_service_url,
  128. jid: data.jid,
  129. sid: data.sid,
  130. rid: data.rid
  131. });
  132. );
  133. **Here's what's happening:**
  134. The JSON data returned from the Ajax call to example.com/prebind contains the user's JID (jabber ID), RID, SID and the URL to the
  135. BOSH server (also called a *connection manager*).
  136. These values are then passed to converse.js's ``initialize`` method.
  137. .. note::
  138. If you want to enable single session support, you need to set **prebind: true**
  139. when calling **converse.initialize** (see ./index.html).
  140. Additionally you need to pass in valid **jid**, **sid**, **rid** and
  141. **bosh_service_url** values.
  142. Example code for server-side prebinding
  143. =======================================
  144. * PHP:
  145. See `xmpp-prebind-php <https://github.com/candy-chat/xmpp-prebind-php>`_ by
  146. Michael Weibel and the folks from Candy chat.
  147. * Python:
  148. See this `example Django application`_ by Jack Moffitt.