2015-05-02 01:21:25 +02:00
.. raw :: html
<div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/setup.rst">Edit me on GitHub</a></div>
2017-10-30 10:25:30 +01:00
.. _what-you-will-need:
2014-12-07 00:53:17 +01:00
=====================
Setup and integration
=====================
2018-07-28 16:57:08 +02:00
This page documents what you'll need to do to be able to connect Converse with
2017-09-13 11:12:15 +02:00
your own XMPP server and to better integrate it into your website.
2015-03-08 14:17:21 +01:00
2018-07-28 16:57:08 +02:00
At the very least you'll need Converse and an :ref: `XMPP server` with
2017-09-13 11:12:15 +02:00
:ref: `websocket-section` or :ref: `BOSH-section` enabled. That's definitely
2018-07-28 16:57:08 +02:00
enough to simply demo Converse or to do development work on it.
2015-03-08 14:17:21 +01:00
2018-07-28 16:57:08 +02:00
However, if you want to more fully integrate it into a website
2017-09-13 11:12:15 +02:00
then you'll likely need to set up more services and components.
2015-03-08 14:17:21 +01:00
2017-09-13 11:12:15 +02:00
The diagram below shows a fairly common setup for a website or intranet:
2015-03-08 14:17:21 +01:00
2018-07-28 16:57:08 +02:00
* Converse runs in the web-browser on the user's device.
2017-09-04 16:18:13 +02:00
2017-09-13 11:12:15 +02:00
* It communicates with the XMPP server via BOSH or websocket which is usually
reverse-proxied by a web-server in order to overcome cross-site scripting
restrictions in the browser. For more info on that, read the section:
`Overcoming cross-domain request restrictions`_
2017-09-04 16:18:13 +02:00
2017-09-13 11:12:15 +02:00
* Optionally the XMPP server is configured to use a SQL database for storing
archived chat messages.
2017-09-04 16:18:13 +02:00
2017-09-13 11:12:15 +02:00
* Optionally there is a user directory such as Active Directory or LDAP, which
the XMPP server is configured to use, so that users can log in with those
credentials.
* Usually (but optionally) there is a backend web application which hosts a
2018-07-28 16:57:08 +02:00
website in which Converse appears.
2017-09-04 16:18:13 +02:00
.. figure :: images/diagram.png
:align: center
2018-07-28 16:57:08 +02:00
: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.
2017-09-04 16:18:13 +02:00
2017-09-13 11:12:15 +02:00
This diagram shows the various services in a fairly common setup (image generated with `draw.io <https://draw.io> `_ ).
2015-03-08 14:17:21 +01:00
2017-09-13 11:12:15 +02:00
----------------------
The various components
----------------------
2015-03-08 14:17:21 +01:00
.. _`XMPP server`:
2014-12-07 00:53:17 +01:00
An XMPP server
2017-03-03 16:16:41 +01:00
==============
2014-12-07 00:53:17 +01:00
2018-09-24 09:52:09 +02:00
Converse uses `XMPP <http://xmpp.org/about-xmpp/> `_ as its
2014-12-07 00:53:17 +01:00
messaging protocol, and therefore needs to connect to an XMPP/Jabber
2017-09-04 16:18:13 +02:00
server (Jabber® is an older and more user-friendly synonym for XMPP).
2014-12-07 00:53:17 +01:00
2018-07-28 16:57:08 +02:00
You can connect to public XMPP servers like `` conversejs.org `` but if you want to
2018-09-24 09:52:09 +02:00
integrate Converse into your own website and to use your website's
authentication sessions to log in users to the XMPP server (i.e. :ref: `session support <session-support>` )
then you'll have to set up your own XMPP server.
2014-12-07 00:53:17 +01:00
2018-09-24 09:52:09 +02:00
You can find a list of public XMPP servers/providers on `compliance.conversations.im <http://compliance.conversations.im/> `_
2017-09-04 16:18:13 +02:00
and a list of servers that you can set up yourself on `xmpp.org <http://xmpp.org/xmpp-software/servers/> `_ .
2014-12-07 00:53:17 +01:00
2017-09-13 11:12:15 +02:00
.. _`BOSH-section`:
BOSH
====
Web-browsers do not allow the persistent, direct TCP socket connections used by
desktop XMPP clients to communicate with XMPP servers.
2015-03-08 14:17:21 +01:00
2017-09-13 11:12:15 +02:00
Instead, we have HTTP and websocket as available protocols.
2014-12-07 00:53:17 +01:00
2017-09-13 11:12:15 +02:00
`BOSH`_ can be seen as XMPP-over-HTTP. In other words, it allows for XMPP
stanzas to be sent over an HTTP connection.
2014-12-07 00:53:17 +01:00
2017-09-13 11:12:15 +02:00
HTTP connections are stateless and usually shortlived.
XMPP connections on the other hand are stateful and usually last much longer.
2014-12-07 00:53:17 +01:00
2018-09-24 09:52:09 +02:00
So to enable a web application like Converse to communicate with an XMPP
2015-03-08 14:17:21 +01:00
server, we need a proxy which acts as a bridge between these two protocols.
This is the job of a BOSH connection manager. BOSH (Bidirectional-streams Over
Synchronous HTTP) is a protocol for allowing XMPP communication over HTTP. The
protocol is defined in `XEP-0206: XMPP Over BOSH <http://xmpp.org/extensions/xep-0206.html> `_ .
2017-04-04 22:43:25 +02:00
Popular XMPP servers such as `Ejabberd <http://www.ejabberd.im> `_ ,
2018-07-28 16:57:08 +02:00
Prosody `(mod_bosh) <http://prosody.im/doc/setting_up_bosh> `_ and
2017-04-04 22:43:25 +02:00
`OpenFire <http://www.igniterealtime.org/projects/openfire/> `_ all include
2017-09-13 11:12:15 +02:00
their own BOSH connection managers (but you usually have to enable them in the
2015-03-08 14:17:21 +01:00
configuration).
2014-12-07 00:53:17 +01:00
2015-03-08 14:17:21 +01:00
However, if you intend to support multiple different servers (like
https://conversejs.org does), then you'll need a standalone connection manager.
2014-12-07 00:53:17 +01:00
2015-03-08 14:17:21 +01:00
For a standalone manager, see for example `Punjab <https://github.com/twonds/punjab> `_
and `node-xmpp-bosh <https://github.com/dhruvbird/node-xmpp-bosh> `_ .
2014-12-07 00:53:17 +01:00
2018-07-28 16:57:08 +02:00
The demo on the `Converse homepage <http://conversejs.org> `_ uses a connection
2015-03-08 14:17:21 +01:00
manager located at https://conversejs.org/http-bind.
2014-12-07 00:53:17 +01:00
2015-03-08 14:17:21 +01:00
This connection manager is available for testing purposes only, please don't
use it in production.
2017-09-13 11:12:15 +02:00
Refer to the :ref: `bosh-service-url` configuration setting for information on
2018-07-28 16:57:08 +02:00
how to configure Converse to connect to a BOSH URL.
2017-09-13 11:12:15 +02:00
.. _`websocket-section`:
Websocket
=========
2015-03-08 14:17:21 +01:00
Websockets provide an alternative means of connection to an XMPP server from
your browser.
Websockets provide long-lived, bidirectional connections which do not rely on
HTTP. Therefore BOSH, which operates over HTTP, doesn't apply to websockets.
2018-04-26 14:46:03 +02:00
`Prosody <http://prosody.im> `_ (from version 0.10) and `Ejabberd <http://www.ejabberd.im> `_ support websocket connections, as
2015-03-08 14:17:21 +01:00
does the node-xmpp-bosh connection manager.
2014-12-07 00:53:17 +01:00
2017-09-13 11:12:15 +02:00
Refer to the :ref: `websocket-url` configuration setting for information on how to
2018-07-28 16:57:08 +02:00
configure Converse to connect to a websocket URL.
2017-09-13 11:12:15 +02:00
The Webserver
=============
2018-09-24 09:52:09 +02:00
Lets say the domain under which you host Converse is *example.org:80* ,
but the domain of your connection manager or the domain of
your HTTP file server (for `XEP-0363 HTTP File Upload <https://xmpp.org/extensions/xep-0363.html> `_ )
is at a different domain, either a different port like *example.org:5280* or a
different name like *elsehwere.org* .
2014-12-07 00:53:17 +01:00
2018-11-10 21:59:57 +01:00
In such a situation the same-origin security policy of the browser comes into force.
For security purposes a browser does not by default allow a website to
2018-09-24 09:52:09 +02:00
make certain types of requests to other domains.
2014-12-07 00:53:17 +01:00
2018-11-10 21:59:57 +01:00
There are two ways in which you can solve this problem.
.. _CORS:
1. Cross-Origin Resource Sharing (CORS)
---------------------------------------
CORS is a technique for overcoming browser restrictions related to the
`same-origin security policy <https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy> `_ .
2014-12-07 00:53:17 +01:00
2018-11-10 21:59:57 +01:00
CORS is enabled by adding an `` Access-Control-Allow-Origin `` header. Where this
is configured depends on what webserver is used for your file upload server.
2014-12-07 00:53:17 +01:00
2018-11-10 21:59:57 +01:00
2. Reverse-proxy
----------------
Another possible solution is to add a reverse proxy to a webserver such as Nginx or Apache to ensure that
all services you use are hosted under the same domain name and port.
2014-12-07 00:53:17 +01:00
2017-09-13 11:12:15 +02:00
Examples:
***** *** *
2014-12-07 00:53:17 +01:00
Assuming your site is accessible on port `` 80 `` for the domain `` mysite.com ``
and your connection manager manager is running at `` someothersite.com/http-bind `` .
2018-07-28 16:57:08 +02:00
The *bosh_service_url* value you want to give Converse to overcome
2014-12-07 00:53:17 +01:00
the cross-domain restriction is `` mysite.com/http-bind `` and not
`` someothersite.com/http-bind `` .
Your `` nginx `` or `` apache `` configuration will look as follows:
Nginx
2017-09-13 11:12:15 +02:00
~~~~~
2014-12-07 00:53:17 +01:00
2015-04-09 14:16:29 +02:00
.. code-block :: nginx
2014-12-07 00:53:17 +01:00
http {
server {
listen 80
server_name mysite.com;
2018-12-20 17:59:51 +01:00
location = / {
root /path/to/converse.js/; # Properly set the path here
index index.html;
}
2014-12-07 00:53:17 +01:00
location ~ ^/http-bind/ {
proxy_pass http://someothersite.com;
}
2018-12-20 17:59:51 +01:00
# CORS
location ~ .(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$ {
add_header Access-Control-Allow-Origin "*"; # Decide here whether you want to allow all or only a particular domain
root /path/to/converse.js/; # Properly set the path here
}
2014-12-07 00:53:17 +01:00
}
}
Apache
2017-09-13 11:12:15 +02:00
~~~~~~
2014-12-07 00:53:17 +01:00
.. code-block :: apache
<VirtualHost *:80>
ServerName mysite.com
RewriteEngine On
RewriteRule ^/http-bind(.*) http://someothersite.com/http-bind$1 [P,L]
</VirtualHost>
2018-07-28 16:57:08 +02:00
.. note ::
If you're getting XML parsing errors for your BOSH endpoint, for
example::
XML Parsing Error: mismatched tag. Expected: </hr>.
Location: https://example.org/http-bind/
Line Number 6, Column 3: bosh-anon:6:3
Also ERROR: request id 12.2 error 504 happened
2018-07-28 17:13:13 +02:00
Then your BOSH proxy is returning an HTML error page (for a 504 error in
the above example).
This might be because your webserver and BOSH proxy have the same timeout
2018-07-28 16:57:08 +02:00
for BOSH requests. Because the webserver receives the request slightly earlier,
it gives up a few microseconds before the XMPP server’ s empty result and thus returns a
504 error page containing HTML to browser, which then gets parsed as if its
XML.
To fix this, make sure that the webserver's timeout is slightly higher.
In Nginx you can do this by adding `` proxy_read_timeout 61; `` ;
2018-07-28 17:13:13 +02:00
From Converse 4.0.0 onwards the default `` wait `` time is set to 59 seconds, to avoid
this problem.
2018-07-28 16:57:08 +02:00
2014-12-07 00:53:17 +01:00
.. _`session-support`:
2014-12-18 14:39:03 +01:00
Single Session Support
2017-09-13 11:12:15 +02:00
======================
2014-12-18 14:39:03 +01:00
2017-03-03 16:16:41 +01:00
It's possible to enable shared sessions whereby users already
logged in to your website will also automatically be logged in on the XMPP server,
2015-03-08 14:17:21 +01:00
2017-03-03 16:16:41 +01:00
Once a user is logged in, the session will be kept alive across page loads by
way of the :ref: `keepalive` setting.
2015-03-08 14:17:21 +01:00
2017-03-03 16:16:41 +01:00
There are a few ways to let your users be automatically authenticated to an
XMPP server once they've logged in to your site.
2015-03-08 14:17:21 +01:00
2014-12-07 00:53:17 +01:00
2017-03-03 16:16:41 +01:00
Option 1). Server-side authentication via BOSH prebinding
2017-09-13 11:12:15 +02:00
---------------------------------------------------------
2017-03-03 16:16:41 +01:00
To **prebind** refers to a technique whereby your web application sets up an
authenticated BOSH session with the XMPP server or a standalone `BOSH <http://xmpp.org/about-xmpp/technology-overview/bosh/> `_
connection manager.
Once authenticated, it receives RID and SID tokens which need to be passed
2018-09-18 10:53:43 +02:00
on to Converse. Converse will then attach to that same session using
2017-03-03 16:16:41 +01:00
those tokens.
It's called "prebind" because you bind to the BOSH session beforehand, and then
later in the page you just attach to that session again.
The RID and SID tokens can be passed in manually when calling
2018-09-18 10:53:43 +02:00
`converse.initialize` , but a more convenient way is to pass Converse a :ref: `prebind_url`
2017-03-03 16:16:41 +01:00
which it will call when it needs the tokens. This way it will be able to
automatically reconnect whenever the connection drops, by simply calling that
URL again to fetch new tokens.
Prebinding reduces network traffic and also speeds up the startup time for
2018-09-18 10:53:43 +02:00
Converse. Additionally, because prebind works with tokens, it's not necessary
2017-03-03 16:16:41 +01:00
for the XMPP client to know or store users' passwords.
One potential drawback of using prebind is that in order to establish the
authenticated BOSH session server-side, you'll need to access and pass on the XMPP
credentials server-side, which, unless you're using tokens, means that you'll
need to store XMPP passwords in cleartext.
2014-12-07 00:53:17 +01:00
2017-03-03 16:16:41 +01:00
This is however not the case if you for example use LDAP or Active Directory as
your authentication backend, since you could then configure your XMPP server to
use that as well.
2014-12-07 00:53:17 +01:00
2018-09-18 10:53:43 +02:00
To prebind you will require a BOSH-enabled XMPP server for Converse to connect to
2017-07-05 11:03:13 +02:00
(see the :ref: `bosh-service-url` under :ref: `configuration-settings` )
2015-03-08 14:17:21 +01:00
as well as a BOSH client in your web application (written for example in
Python, Ruby or PHP) that will set up an authenticated BOSH session, which
2018-09-18 10:53:43 +02:00
Converse can then attach to.
2014-12-07 00:53:17 +01:00
.. note ::
A BOSH server acts as a bridge between HTTP, the protocol of the web, and
XMPP, the instant messaging protocol.
2017-03-03 16:16:41 +01:00
2018-07-28 16:57:08 +02:00
Converse can only communicate via HTTP (or websocket, in which case BOSH can't be used).
2017-03-03 16:16:41 +01:00
It cannot open TCP sockets to communicate to an XMPP server directly.
So the BOSH server acts as a middle man, translating our HTTP requests into XMPP stanzas and vice versa.
2014-12-07 00:53:17 +01:00
Jack Moffitt has a great `blogpost <http://metajack.im/2008/10/03/getting-attached-to-strophe> `_
about this and even provides an
`example Django application <https://github.com/metajack/strophejs/tree/master/examples/attach> `_
to demonstrate it.
When you authenticate to the XMPP server on your backend application (for
example via a BOSH client in Django), you'll receive two tokens, RID (request ID) and SID (session ID).
The **Session ID (SID)** is a unique identifier for the current *session* . This
number stays constant for the entire session.
The **Request ID (RID)** is a unique identifier for the current *request* (i.e.
page load). Each page load is a new request which requires a new unique RID.
The best way to achieve this is to simply increment the RID with each page
load.
2018-09-18 10:53:43 +02:00
You'll need to configure Converse with the `` prebind `` , :ref: `keepalive` and
2015-03-05 23:11:07 +01:00
:ref: `prebind_url` settings.
2015-02-08 19:35:58 +01:00
2015-03-05 23:11:07 +01:00
Please read the documentation on those settings for a fuller picture of what
needs to be done.
2014-12-07 00:53:17 +01:00
Example code for server-side prebinding
2017-09-13 11:12:15 +02:00
***** ***** ***** ***** ***** ***** ***** *** *
2014-12-07 00:53:17 +01:00
* PHP:
See `xmpp-prebind-php <https://github.com/candy-chat/xmpp-prebind-php> `_ by
Michael Weibel and the folks from Candy chat.
* Python:
See this `example Django application`_ by Jack Moffitt.
2015-03-08 14:17:21 +01:00
2017-03-03 16:16:41 +01:00
Option 2). Delegated authentication, also called external authentication
2017-09-13 11:12:15 +02:00
------------------------------------------------------------------------
2017-03-03 16:16:41 +01:00
2017-09-13 11:12:15 +02:00
Delegated authentication refers to the usecase where the XMPP server delegates
authentication to some other service.
2017-03-03 16:16:41 +01:00
2017-09-13 11:12:15 +02:00
This could be to LDAP or Active Directory (as shown in the diagram at the top
of the page), or it could be to an OAuth provider, a SQL server to a specific
website.
The Prosody webserver has various user-contributed modules which delegate
authentication to external services. They are listed in the `Prosody community modules
page <https://modules.prosody.im/>`_. Other XMPP servers have similar plugin modules.
If your web-application has access to the same credentials, it can send those
2018-07-28 16:57:08 +02:00
credentials to Converse so that user's are automatically logged in when the
2017-09-13 11:12:15 +02:00
page loads.
2017-09-13 11:14:58 +02:00
This is can be done by setting :ref: `auto_login` to true and configuring the
the :ref: `credentials_url` setting.
2017-09-13 11:12:15 +02:00
Option 3). Temporary authentication tokens
------------------------------------------
The first option has the drawback that your web-application needs to know the
XMPP credentials of your users and that they need to be stored in the clear.
The second option has that same drawback and it also needs to pass those
2018-07-28 16:57:08 +02:00
credentials to Converse.
2017-09-13 11:12:15 +02:00
To avoid these drawbacks, you can instead let your backend web application
generate temporary authentication tokens which are then sent to the XMPP server
which in turn delegates authentication to an external authentication provider
(generally the same web-app that generated the tokens).
This can be combined with prebind or with the :ref: `credentials_url` setting.
2017-03-03 16:16:41 +01:00
2017-09-13 11:12:15 +02:00
Option 4). Cryptographically signed tokens
------------------------------------------
2017-03-03 16:16:41 +01:00
A third potential option is to generate cryptographically signed tokens (e.g.
HMAC tokens) which the XMPP server could authenticate by checking that they're
signed with the right key and that they conform to some kind of pre-arranged
format.
In this case, you would also use the :ref: `credentials_url` setting, to specify a
2018-09-18 10:53:43 +02:00
URL from which Converse should fetch the username and token.