README.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. .. contents::
  2. Introduction
  3. ============
  4. Matrix is an ambitious new ecosystem for open federated Instant Messaging and
  5. VoIP. The basics you need to know to get up and running are:
  6. - Everything in Matrix happens in a room. Rooms are distributed and do not
  7. exist on any single server. Rooms can be located using convenience aliases
  8. like ``#matrix:matrix.org`` or ``#test:localhost:8448``.
  9. - Matrix user IDs look like ``@matthew:matrix.org`` (although in the future
  10. you will normally refer to yourself and others using a 3PID: email
  11. address, phone number, etc rather than manipulating Matrix user IDs)
  12. The overall architecture is::
  13. client <----> homeserver <=====================> homeserver <----> client
  14. https://somewhere.org/_matrix https://elsewhere.net/_matrix
  15. ``#matrix:matrix.org`` is the official support room for Matrix, and can be
  16. accessed by the web client at http://matrix.org/beta or via an IRC bridge at
  17. irc://irc.freenode.net/matrix.
  18. Synapse is currently in rapid development, but as of version 0.5 we believe it
  19. is sufficiently stable to be run as an internet-facing service for real usage!
  20. About Matrix
  21. ============
  22. Matrix specifies a set of pragmatic RESTful HTTP JSON APIs as an open standard,
  23. which handle:
  24. - Creating and managing fully distributed chat rooms with no
  25. single points of control or failure
  26. - Eventually-consistent cryptographically secure synchronisation of room
  27. state across a global open network of federated servers and services
  28. - Sending and receiving extensible messages in a room with (optional)
  29. end-to-end encryption[1]
  30. - Inviting, joining, leaving, kicking, banning room members
  31. - Managing user accounts (registration, login, logout)
  32. - Using 3rd Party IDs (3PIDs) such as email addresses, phone numbers,
  33. Facebook accounts to authenticate, identify and discover users on Matrix.
  34. - Placing 1:1 VoIP and Video calls
  35. These APIs are intended to be implemented on a wide range of servers, services
  36. and clients, letting developers build messaging and VoIP functionality on top
  37. of the entirely open Matrix ecosystem rather than using closed or proprietary
  38. solutions. The hope is for Matrix to act as the building blocks for a new
  39. generation of fully open and interoperable messaging and VoIP apps for the
  40. internet.
  41. Synapse is a reference "homeserver" implementation of Matrix from the core
  42. development team at matrix.org, written in Python/Twisted for clarity and
  43. simplicity. It is intended to showcase the concept of Matrix and let folks see
  44. the spec in the context of a codebase and let you run your own homeserver and
  45. generally help bootstrap the ecosystem.
  46. In Matrix, every user runs one or more Matrix clients, which connect through to
  47. a Matrix homeserver which stores all their personal chat history and user
  48. account information - much as a mail client connects through to an IMAP/SMTP
  49. server. Just like email, you can either run your own Matrix homeserver and
  50. control and own your own communications and history or use one hosted by
  51. someone else (e.g. matrix.org) - there is no single point of control or
  52. mandatory service provider in Matrix, unlike WhatsApp, Facebook, Hangouts, etc.
  53. Synapse ships with two basic demo Matrix clients: webclient (a basic group chat
  54. web client demo implemented in AngularJS) and cmdclient (a basic Python
  55. command line utility which lets you easily see what the JSON APIs are up to).
  56. Meanwhile, iOS and Android SDKs and clients are available from:
  57. - https://github.com/matrix-org/matrix-ios-sdk
  58. - https://github.com/matrix-org/matrix-ios-kit
  59. - https://github.com/matrix-org/matrix-ios-console
  60. - https://github.com/matrix-org/matrix-android-sdk
  61. We'd like to invite you to join #matrix:matrix.org (via
  62. https://matrix.org/beta), run a homeserver, take a look at the Matrix spec at
  63. https://matrix.org/docs/spec and API docs at https://matrix.org/docs/api,
  64. experiment with the APIs and the demo clients, and report any bugs via
  65. https://matrix.org/jira.
  66. Thanks for using Matrix!
  67. [1] End-to-end encryption is currently in development
  68. Synapse Installation
  69. ====================
  70. Synapse is the reference python/twisted Matrix homeserver implementation.
  71. System requirements:
  72. - POSIX-compliant system (tested on Linux & OS X)
  73. - Python 2.7
  74. - At least 512 MB RAM.
  75. Synapse is written in python but some of the libraries is uses are written in
  76. C. So before we can install synapse itself we need a working C compiler and the
  77. header files for python C extensions.
  78. Installing prerequisites on Ubuntu or Debian::
  79. sudo apt-get install build-essential python2.7-dev libffi-dev \
  80. python-pip python-setuptools sqlite3 \
  81. libssl-dev python-virtualenv libjpeg-dev
  82. Installing prerequisites on ArchLinux::
  83. sudo pacman -S base-devel python2 python-pip \
  84. python-setuptools python-virtualenv sqlite3
  85. Installing prerequisites on Mac OS X::
  86. xcode-select --install
  87. sudo easy_install pip
  88. sudo pip install virtualenv
  89. To install the synapse homeserver run::
  90. virtualenv -p python2.7 ~/.synapse
  91. source ~/.synapse/bin/activate
  92. pip install --upgrade setuptools
  93. pip install --process-dependency-links https://github.com/matrix-org/synapse/tarball/master
  94. This installs synapse, along with the libraries it uses, into a virtual
  95. environment under ``~/.synapse``. Feel free to pick a different directory
  96. if you prefer.
  97. In case of problems, please see the _Troubleshooting section below.
  98. Alternatively, Silvio Fricke has contributed a Dockerfile to automate the
  99. above in Docker at https://registry.hub.docker.com/u/silviof/docker-matrix/.
  100. To set up your homeserver, run (in your virtualenv, as before)::
  101. cd ~/.synapse
  102. python -m synapse.app.homeserver \
  103. --server-name machine.my.domain.name \
  104. --config-path homeserver.yaml \
  105. --generate-config
  106. Substituting your host and domain name as appropriate.
  107. This will generate you a config file that you can then customise, but it will
  108. also generate a set of keys for you. These keys will allow your Home Server to
  109. identify itself to other Home Servers, so don't lose or delete them. It would be
  110. wise to back them up somewhere safe. If, for whatever reason, you do need to
  111. change your Home Server's keys, you may find that other Home Servers have the
  112. old key cached. If you update the signing key, you should change the name of the
  113. key in the <server name>.signing.key file (the second word, which by default is
  114. , 'auto') to something different.
  115. By default, registration of new users is disabled. You can either enable
  116. registration in the config by specifying ``enable_registration: true``
  117. (it is then recommended to also set up CAPTCHA), or
  118. you can use the command line to register new users::
  119. $ source ~/.synapse/bin/activate
  120. $ register_new_matrix_user -c homeserver.yaml https://localhost:8448
  121. New user localpart: erikj
  122. Password:
  123. Confirm password:
  124. Success!
  125. For reliable VoIP calls to be routed via this homeserver, you MUST configure
  126. a TURN server. See docs/turn-howto.rst for details.
  127. Using PostgreSQL
  128. ================
  129. As of Synapse 0.9, `PostgreSQL <http://www.postgresql.org>`_ is supported as an
  130. alternative to the `SQLite <http://sqlite.org/>`_ database that Synapse has
  131. traditionally used for convenience and simplicity.
  132. The advantages of Postgres include:
  133. * significant performance improvements due to the superior threading and
  134. caching model, smarter query optimiser
  135. * allowing the DB to be run on separate hardware
  136. * allowing basic active/backup high-availability with a "hot spare" synapse
  137. pointing at the same DB master, as well as enabling DB replication in
  138. synapse itself.
  139. The only disadvantage is that the code is relatively new as of April 2015 and
  140. may have a few regressions relative to SQLite.
  141. For information on how to install and use PostgreSQL, please see
  142. `docs/postgres.rst <docs/postgres.rst>`_.
  143. Running Synapse
  144. ===============
  145. To actually run your new homeserver, pick a working directory for Synapse to
  146. run (e.g. ``~/.synapse``), and::
  147. cd ~/.synapse
  148. source ./bin/activate
  149. synctl start
  150. Platform Specific Instructions
  151. ==============================
  152. ArchLinux
  153. ---------
  154. The quickest way to get up and running with ArchLinux is probably with Ivan
  155. Shapovalov's AUR package from
  156. https://aur.archlinux.org/packages/matrix-synapse/, which should pull in all
  157. the necessary dependencies.
  158. Alternatively, to install using pip a few changes may be needed as ArchLinux
  159. defaults to python 3, but synapse currently assumes python 2.7 by default:
  160. pip may be outdated (6.0.7-1 and needs to be upgraded to 6.0.8-1 )::
  161. sudo pip2.7 install --upgrade pip
  162. You also may need to explicitly specify python 2.7 again during the install
  163. request::
  164. pip2.7 install --process-dependency-links \
  165. https://github.com/matrix-org/synapse/tarball/master
  166. If you encounter an error with lib bcrypt causing an Wrong ELF Class:
  167. ELFCLASS32 (x64 Systems), you may need to reinstall py-bcrypt to correctly
  168. compile it under the right architecture. (This should not be needed if
  169. installing under virtualenv)::
  170. sudo pip2.7 uninstall py-bcrypt
  171. sudo pip2.7 install py-bcrypt
  172. During setup of Synapse you need to call python2.7 directly again::
  173. cd ~/.synapse
  174. python2.7 -m synapse.app.homeserver \
  175. --server-name machine.my.domain.name \
  176. --config-path homeserver.yaml \
  177. --generate-config
  178. ...substituting your host and domain name as appropriate.
  179. Windows Install
  180. ---------------
  181. Synapse can be installed on Cygwin. It requires the following Cygwin packages:
  182. - gcc
  183. - git
  184. - libffi-devel
  185. - openssl (and openssl-devel, python-openssl)
  186. - python
  187. - python-setuptools
  188. The content repository requires additional packages and will be unable to process
  189. uploads without them:
  190. - libjpeg8
  191. - libjpeg8-devel
  192. - zlib
  193. If you choose to install Synapse without these packages, you will need to reinstall
  194. ``pillow`` for changes to be applied, e.g. ``pip uninstall pillow`` ``pip install
  195. pillow --user``
  196. Troubleshooting:
  197. - You may need to upgrade ``setuptools`` to get this to work correctly:
  198. ``pip install setuptools --upgrade``.
  199. - You may encounter errors indicating that ``ffi.h`` is missing, even with
  200. ``libffi-devel`` installed. If you do, copy the ``.h`` files:
  201. ``cp /usr/lib/libffi-3.0.13/include/*.h /usr/include``
  202. - You may need to install libsodium from source in order to install PyNacl. If
  203. you do, you may need to create a symlink to ``libsodium.a`` so ``ld`` can find
  204. it: ``ln -s /usr/local/lib/libsodium.a /usr/lib/libsodium.a``
  205. Troubleshooting
  206. ===============
  207. Troubleshooting Installation
  208. ----------------------------
  209. Synapse requires pip 1.7 or later, so if your OS provides too old a version and
  210. you get errors about ``error: no such option: --process-dependency-links`` you
  211. may need to manually upgrade it::
  212. sudo pip install --upgrade pip
  213. Installing may fail with ``mock requires setuptools>=17.1. Aborting installation``.
  214. You can fix this by upgrading setuptools::
  215. pip install --upgrade setuptools
  216. If pip crashes mid-installation for reason (e.g. lost terminal), pip may
  217. refuse to run until you remove the temporary installation directory it
  218. created. To reset the installation::
  219. rm -rf /tmp/pip_install_matrix
  220. pip seems to leak *lots* of memory during installation. For instance, a Linux
  221. host with 512MB of RAM may run out of memory whilst installing Twisted. If this
  222. happens, you will have to individually install the dependencies which are
  223. failing, e.g.::
  224. pip install twisted
  225. On OS X, if you encounter clang: error: unknown argument: '-mno-fused-madd' you
  226. will need to export CFLAGS=-Qunused-arguments.
  227. Troubleshooting Running
  228. -----------------------
  229. If synapse fails with ``missing "sodium.h"`` crypto errors, you may need
  230. to manually upgrade PyNaCL, as synapse uses NaCl (http://nacl.cr.yp.to/) for
  231. encryption and digital signatures.
  232. Unfortunately PyNACL currently has a few issues
  233. (https://github.com/pyca/pynacl/issues/53) and
  234. (https://github.com/pyca/pynacl/issues/79) that mean it may not install
  235. correctly, causing all tests to fail with errors about missing "sodium.h". To
  236. fix try re-installing from PyPI or directly from
  237. (https://github.com/pyca/pynacl)::
  238. # Install from PyPI
  239. pip install --user --upgrade --force pynacl
  240. # Install from github
  241. pip install --user https://github.com/pyca/pynacl/tarball/master
  242. ArchLinux
  243. ~~~~~~~~~
  244. If running `$ synctl start` fails with 'returned non-zero exit status 1',
  245. you will need to explicitly call Python2.7 - either running as::
  246. python2.7 -m synapse.app.homeserver --daemonize -c homeserver.yaml
  247. ...or by editing synctl with the correct python executable.
  248. Synapse Development
  249. ===================
  250. To check out a synapse for development, clone the git repo into a working
  251. directory of your choice::
  252. git clone https://github.com/matrix-org/synapse.git
  253. cd synapse
  254. Synapse has a number of external dependencies, that are easiest
  255. to install using pip and a virtualenv::
  256. virtualenv env
  257. source env/bin/activate
  258. python synapse/python_dependencies.py | xargs -n1 pip install
  259. pip install setuptools_trial mock
  260. This will run a process of downloading and installing all the needed
  261. dependencies into a virtual env.
  262. Once this is done, you may wish to run Synapse's unit tests, to
  263. check that everything is installed as it should be::
  264. python setup.py test
  265. This should end with a 'PASSED' result::
  266. Ran 143 tests in 0.601s
  267. PASSED (successes=143)
  268. Upgrading an existing Synapse
  269. =============================
  270. The instructions for upgrading synapse are in `UPGRADE.rst`_.
  271. Please check these instructions as upgrading may require extra steps for some
  272. versions of synapse.
  273. .. _UPGRADE.rst: UPGRADE.rst
  274. Setting up Federation
  275. =====================
  276. In order for other homeservers to send messages to your server, it will need to
  277. be publicly visible on the internet, and they will need to know its host name.
  278. You have two choices here, which will influence the form of your Matrix user
  279. IDs:
  280. 1) Use the machine's own hostname as available on public DNS in the form of
  281. its A or AAAA records. This is easier to set up initially, perhaps for
  282. testing, but lacks the flexibility of SRV.
  283. 2) Set up a SRV record for your domain name. This requires you create a SRV
  284. record in DNS, but gives the flexibility to run the server on your own
  285. choice of TCP port, on a machine that might not be the same name as the
  286. domain name.
  287. For the first form, simply pass the required hostname (of the machine) as the
  288. --server-name parameter::
  289. python -m synapse.app.homeserver \
  290. --server-name machine.my.domain.name \
  291. --config-path homeserver.yaml \
  292. --generate-config
  293. python -m synapse.app.homeserver --config-path homeserver.yaml
  294. Alternatively, you can run ``synctl start`` to guide you through the process.
  295. For the second form, first create your SRV record and publish it in DNS. This
  296. needs to be named _matrix._tcp.YOURDOMAIN, and point at at least one hostname
  297. and port where the server is running. (At the current time synapse does not
  298. support clustering multiple servers into a single logical homeserver). The DNS
  299. record would then look something like::
  300. $ dig -t srv _matrix._tcp.machine.my.domain.name
  301. _matrix._tcp IN SRV 10 0 8448 machine.my.domain.name.
  302. At this point, you should then run the homeserver with the hostname of this
  303. SRV record, as that is the name other machines will expect it to have::
  304. python -m synapse.app.homeserver \
  305. --server-name YOURDOMAIN \
  306. --config-path homeserver.yaml \
  307. --generate-config
  308. python -m synapse.app.homeserver --config-path homeserver.yaml
  309. You may additionally want to pass one or more "-v" options, in order to
  310. increase the verbosity of logging output; at least for initial testing.
  311. Running a Demo Federation of Synapses
  312. -------------------------------------
  313. If you want to get up and running quickly with a trio of homeservers in a
  314. private federation (``localhost:8080``, ``localhost:8081`` and
  315. ``localhost:8082``) which you can then access through the webclient running at
  316. http://localhost:8080. Simply run::
  317. demo/start.sh
  318. This is mainly useful just for development purposes.
  319. Running The Demo Web Client
  320. ===========================
  321. The homeserver runs a web client by default at https://localhost:8448/.
  322. If this is the first time you have used the client from that browser (it uses
  323. HTML5 local storage to remember its config), you will need to log in to your
  324. account. If you don't yet have an account, because you've just started the
  325. homeserver for the first time, then you'll need to register one.
  326. Registering A New Account
  327. -------------------------
  328. Your new user name will be formed partly from the hostname your server is
  329. running as, and partly from a localpart you specify when you create the
  330. account. Your name will take the form of::
  331. @localpart:my.domain.here
  332. (pronounced "at localpart on my dot domain dot here")
  333. Specify your desired localpart in the topmost box of the "Register for an
  334. account" form, and click the "Register" button. Hostnames can contain ports if
  335. required due to lack of SRV records (e.g. @matthew:localhost:8448 on an
  336. internal synapse sandbox running on localhost).
  337. If registration fails, you may need to enable it in the homeserver (see
  338. `Synapse Installation`_ above)
  339. Logging In To An Existing Account
  340. ---------------------------------
  341. Just enter the ``@localpart:my.domain.here`` Matrix user ID and password into
  342. the form and click the Login button.
  343. Identity Servers
  344. ================
  345. The job of authenticating 3PIDs and tracking which 3PIDs are associated with a
  346. given Matrix user is very security-sensitive, as there is obvious risk of spam
  347. if it is too easy to sign up for Matrix accounts or harvest 3PID data.
  348. Meanwhile the job of publishing the end-to-end encryption public keys for
  349. Matrix users is also very security-sensitive for similar reasons.
  350. Therefore the role of managing trusted identity in the Matrix ecosystem is
  351. farmed out to a cluster of known trusted ecosystem partners, who run 'Matrix
  352. Identity Servers' such as ``sydent``, whose role is purely to authenticate and
  353. track 3PID logins and publish end-user public keys.
  354. It's currently early days for identity servers as Matrix is not yet using 3PIDs
  355. as the primary means of identity and E2E encryption is not complete. As such,
  356. we are running a single identity server (https://matrix.org) at the current
  357. time.
  358. Where's the spec?!
  359. ==================
  360. The source of the matrix spec lives at https://github.com/matrix-org/matrix-doc.
  361. A recent HTML snapshot of this lives at http://matrix.org/docs/spec
  362. Building Internal API Documentation
  363. ===================================
  364. Before building internal API documentation install sphinx and
  365. sphinxcontrib-napoleon::
  366. pip install sphinx
  367. pip install sphinxcontrib-napoleon
  368. Building internal API documentation::
  369. python setup.py build_sphinx