README.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. Synapse is written in python but some of the libraries is uses are written in
  75. C. So before we can install synapse itself we need a working C compiler and the
  76. header files for python C extensions.
  77. Installing prerequisites on Ubuntu or Debian::
  78. $ sudo apt-get install build-essential python2.7-dev libffi-dev \
  79. python-pip python-setuptools sqlite3 \
  80. libssl-dev python-virtualenv libjpeg-dev
  81. Installing prerequisites on ArchLinux::
  82. $ sudo pacman -S base-devel python2 python-pip \
  83. python-setuptools python-virtualenv sqlite3
  84. Installing prerequisites on Mac OS X::
  85. $ xcode-select --install
  86. $ sudo pip install virtualenv
  87. To install the synapse homeserver run::
  88. $ virtualenv ~/.synapse
  89. $ source ~/.synapse/bin/activate
  90. $ pip install --process-dependency-links https://github.com/matrix-org/synapse/tarball/master
  91. This installs synapse, along with the libraries it uses, into a virtual
  92. environment under ``~/.synapse``.
  93. Alternatively, Silvio Fricke has contributed a Dockerfile to automate the
  94. above in Docker at https://registry.hub.docker.com/u/silviof/docker-matrix/.
  95. To set up your homeserver, run (in your virtualenv, as before)::
  96. $ cd ~/.synapse
  97. $ python -m synapse.app.homeserver \
  98. --server-name machine.my.domain.name \
  99. --config-path homeserver.yaml \
  100. --generate-config
  101. Substituting your host and domain name as appropriate.
  102. This will generate you a config file that you can then customise, but it will
  103. also generate a set of keys for you. These keys will allow your Home Server to
  104. identify itself to other Home Servers, so don't lose or delete them. It would be
  105. wise to back them up somewhere safe. If, for whatever reason, you do need to
  106. change your Home Server's keys, you may find that other Home Servers have the
  107. old key cached. If you update the signing key, you should change the name of the
  108. key in the <server name>.signing.key file (the second word, which by default is
  109. , 'auto') to something different.
  110. By default, registration of new users is disabled. You can either enable
  111. registration in the config by specifying ``enable_registration: true``
  112. (it is then recommended to also set up CAPTCHA), or
  113. you can use the command line to register new users::
  114. $ source ~/.synapse/bin/activate
  115. $ register_new_matrix_user -c homeserver.yaml https://localhost:8448
  116. New user localpart: erikj
  117. Password:
  118. Confirm password:
  119. Success!
  120. For reliable VoIP calls to be routed via this homeserver, you MUST configure
  121. a TURN server. See docs/turn-howto.rst for details.
  122. Using PostgreSQL
  123. ================
  124. As of Synapse 0.9, `PostgreSQL <http://www.postgresql.org>`_ is supported as an
  125. alternative to the `SQLite <http://sqlite.org/>`_ database that Synapse has
  126. traditionally used for convenience and simplicity.
  127. The advantages of Postgres include:
  128. * significant performance improvements due to the superior threading and
  129. caching model, smarter query optimiser
  130. * allowing the DB to be run on separate hardware
  131. * allowing basic active/backup high-availability with a "hot spare" synapse
  132. pointing at the same DB master, as well as enabling DB replication in
  133. synapse itself.
  134. The only disadvantage is that the code is relatively new as of April 2015 and
  135. may have a few regressions relative to SQLite.
  136. For information on how to install and use PostgreSQL, please see
  137. `docs/postgres.rst <docs/postgres.rst>`_.
  138. Running Synapse
  139. ===============
  140. To actually run your new homeserver, pick a working directory for Synapse to run
  141. (e.g. ``~/.synapse``), and::
  142. $ cd ~/.synapse
  143. $ source ./bin/activate
  144. $ synctl start
  145. Platform Specific Instructions
  146. ==============================
  147. ArchLinux
  148. ---------
  149. The quickest way to get up and running with ArchLinux is probably with Ivan
  150. Shapovalov's AUR package from
  151. https://aur.archlinux.org/packages/matrix-synapse/, which should pull in all
  152. the necessary dependencies.
  153. Alternatively, to install using pip a few changes may be needed as ArchLinux
  154. defaults to python 3, but synapse currently assumes python 2.7 by default:
  155. pip may be outdated (6.0.7-1 and needs to be upgraded to 6.0.8-1 )::
  156. $ sudo pip2.7 install --upgrade pip
  157. You also may need to explicitly specify python 2.7 again during the install
  158. request::
  159. $ pip2.7 install --process-dependency-links \
  160. https://github.com/matrix-org/synapse/tarball/master
  161. If you encounter an error with lib bcrypt causing an Wrong ELF Class:
  162. ELFCLASS32 (x64 Systems), you may need to reinstall py-bcrypt to correctly
  163. compile it under the right architecture. (This should not be needed if
  164. installing under virtualenv)::
  165. $ sudo pip2.7 uninstall py-bcrypt
  166. $ sudo pip2.7 install py-bcrypt
  167. During setup of Synapse you need to call python2.7 directly again::
  168. $ cd ~/.synapse
  169. $ python2.7 -m synapse.app.homeserver \
  170. --server-name machine.my.domain.name \
  171. --config-path homeserver.yaml \
  172. --generate-config
  173. ...substituting your host and domain name as appropriate.
  174. Windows Install
  175. ---------------
  176. Synapse can be installed on Cygwin. It requires the following Cygwin packages:
  177. - gcc
  178. - git
  179. - libffi-devel
  180. - openssl (and openssl-devel, python-openssl)
  181. - python
  182. - python-setuptools
  183. The content repository requires additional packages and will be unable to process
  184. uploads without them:
  185. - libjpeg8
  186. - libjpeg8-devel
  187. - zlib
  188. If you choose to install Synapse without these packages, you will need to reinstall
  189. ``pillow`` for changes to be applied, e.g. ``pip uninstall pillow`` ``pip install
  190. pillow --user``
  191. Troubleshooting:
  192. - You may need to upgrade ``setuptools`` to get this to work correctly:
  193. ``pip install setuptools --upgrade``.
  194. - You may encounter errors indicating that ``ffi.h`` is missing, even with
  195. ``libffi-devel`` installed. If you do, copy the ``.h`` files:
  196. ``cp /usr/lib/libffi-3.0.13/include/*.h /usr/include``
  197. - You may need to install libsodium from source in order to install PyNacl. If
  198. you do, you may need to create a symlink to ``libsodium.a`` so ``ld`` can find
  199. it: ``ln -s /usr/local/lib/libsodium.a /usr/lib/libsodium.a``
  200. Troubleshooting
  201. ===============
  202. Troubleshooting Installation
  203. ----------------------------
  204. Synapse requires pip 1.7 or later, so if your OS provides too old a version and
  205. you get errors about ``error: no such option: --process-dependency-links`` you
  206. may need to manually upgrade it::
  207. $ sudo pip install --upgrade pip
  208. If pip crashes mid-installation for reason (e.g. lost terminal), pip may
  209. refuse to run until you remove the temporary installation directory it
  210. created. To reset the installation::
  211. $ rm -rf /tmp/pip_install_matrix
  212. pip seems to leak *lots* of memory during installation. For instance, a Linux
  213. host with 512MB of RAM may run out of memory whilst installing Twisted. If this
  214. happens, you will have to individually install the dependencies which are
  215. failing, e.g.::
  216. $ pip install twisted
  217. On OSX, if you encounter clang: error: unknown argument: '-mno-fused-madd' you
  218. will need to export CFLAGS=-Qunused-arguments.
  219. Troubleshooting Running
  220. -----------------------
  221. If synapse fails with ``missing "sodium.h"`` crypto errors, you may need
  222. to manually upgrade PyNaCL, as synapse uses NaCl (http://nacl.cr.yp.to/) for
  223. encryption and digital signatures.
  224. Unfortunately PyNACL currently has a few issues
  225. (https://github.com/pyca/pynacl/issues/53) and
  226. (https://github.com/pyca/pynacl/issues/79) that mean it may not install
  227. correctly, causing all tests to fail with errors about missing "sodium.h". To
  228. fix try re-installing from PyPI or directly from
  229. (https://github.com/pyca/pynacl)::
  230. $ # Install from PyPI
  231. $ pip install --user --upgrade --force pynacl
  232. $ # Install from github
  233. $ pip install --user https://github.com/pyca/pynacl/tarball/master
  234. ArchLinux
  235. ~~~~~~~~~
  236. If running `$ synctl start` fails with 'returned non-zero exit status 1',
  237. you will need to explicitly call Python2.7 - either running as::
  238. $ python2.7 -m synapse.app.homeserver --daemonize -c homeserver.yaml --pid-file homeserver.pid
  239. ...or by editing synctl with the correct python executable.
  240. Synapse Development
  241. ===================
  242. To check out a synapse for development, clone the git repo into a working
  243. directory of your choice::
  244. $ git clone https://github.com/matrix-org/synapse.git
  245. $ cd synapse
  246. Synapse has a number of external dependencies, that are easiest
  247. to install using pip and a virtualenv::
  248. $ virtualenv env
  249. $ source env/bin/activate
  250. $ python synapse/python_dependencies.py | xargs -n1 pip install
  251. $ pip install setuptools_trial mock
  252. This will run a process of downloading and installing all the needed
  253. dependencies into a virtual env.
  254. Once this is done, you may wish to run Synapse's unit tests, to
  255. check that everything is installed as it should be::
  256. $ python setup.py test
  257. This should end with a 'PASSED' result::
  258. Ran 143 tests in 0.601s
  259. PASSED (successes=143)
  260. Upgrading an existing Synapse
  261. =============================
  262. IMPORTANT: Before upgrading an existing synapse to a new version, please
  263. refer to UPGRADE.rst for any additional instructions.
  264. Otherwise, simply re-install the new codebase over the current one - e.g.
  265. by ``pip install --process-dependency-links
  266. https://github.com/matrix-org/synapse/tarball/master``
  267. if using pip, or by ``git pull`` if running off a git working copy.
  268. Setting up Federation
  269. =====================
  270. In order for other homeservers to send messages to your server, it will need to
  271. be publicly visible on the internet, and they will need to know its host name.
  272. You have two choices here, which will influence the form of your Matrix user
  273. IDs:
  274. 1) Use the machine's own hostname as available on public DNS in the form of
  275. its A or AAAA records. This is easier to set up initially, perhaps for
  276. testing, but lacks the flexibility of SRV.
  277. 2) Set up a SRV record for your domain name. This requires you create a SRV
  278. record in DNS, but gives the flexibility to run the server on your own
  279. choice of TCP port, on a machine that might not be the same name as the
  280. domain name.
  281. For the first form, simply pass the required hostname (of the machine) as the
  282. --server-name parameter::
  283. $ python -m synapse.app.homeserver \
  284. --server-name machine.my.domain.name \
  285. --config-path homeserver.yaml \
  286. --generate-config
  287. $ python -m synapse.app.homeserver --config-path homeserver.yaml
  288. Alternatively, you can run ``synctl start`` to guide you through the process.
  289. For the second form, first create your SRV record and publish it in DNS. This
  290. needs to be named _matrix._tcp.YOURDOMAIN, and point at at least one hostname
  291. and port where the server is running. (At the current time synapse does not
  292. support clustering multiple servers into a single logical homeserver). The DNS
  293. record would then look something like::
  294. $ dig -t srv _matrix._tcp.machine.my.domain.name
  295. _matrix._tcp IN SRV 10 0 8448 machine.my.domain.name.
  296. At this point, you should then run the homeserver with the hostname of this
  297. SRV record, as that is the name other machines will expect it to have::
  298. $ python -m synapse.app.homeserver \
  299. --server-name YOURDOMAIN \
  300. --bind-port 8448 \
  301. --config-path homeserver.yaml \
  302. --generate-config
  303. $ python -m synapse.app.homeserver --config-path homeserver.yaml
  304. You may additionally want to pass one or more "-v" options, in order to
  305. increase the verbosity of logging output; at least for initial testing.
  306. Running a Demo Federation of Synapses
  307. -------------------------------------
  308. If you want to get up and running quickly with a trio of homeservers in a
  309. private federation (``localhost:8080``, ``localhost:8081`` and
  310. ``localhost:8082``) which you can then access through the webclient running at
  311. http://localhost:8080. Simply run::
  312. $ demo/start.sh
  313. This is mainly useful just for development purposes.
  314. Running The Demo Web Client
  315. ===========================
  316. The homeserver runs a web client by default at https://localhost:8448/.
  317. If this is the first time you have used the client from that browser (it uses
  318. HTML5 local storage to remember its config), you will need to log in to your
  319. account. If you don't yet have an account, because you've just started the
  320. homeserver for the first time, then you'll need to register one.
  321. Registering A New Account
  322. -------------------------
  323. Your new user name will be formed partly from the hostname your server is
  324. running as, and partly from a localpart you specify when you create the
  325. account. Your name will take the form of::
  326. @localpart:my.domain.here
  327. (pronounced "at localpart on my dot domain dot here")
  328. Specify your desired localpart in the topmost box of the "Register for an
  329. account" form, and click the "Register" button. Hostnames can contain ports if
  330. required due to lack of SRV records (e.g. @matthew:localhost:8448 on an
  331. internal synapse sandbox running on localhost).
  332. If registration fails, you may need to enable it in the homeserver (see
  333. `Synapse Installation`_ above)
  334. Logging In To An Existing Account
  335. ---------------------------------
  336. Just enter the ``@localpart:my.domain.here`` Matrix user ID and password into
  337. the form and click the Login button.
  338. Identity Servers
  339. ================
  340. The job of authenticating 3PIDs and tracking which 3PIDs are associated with a
  341. given Matrix user is very security-sensitive, as there is obvious risk of spam
  342. if it is too easy to sign up for Matrix accounts or harvest 3PID data.
  343. Meanwhile the job of publishing the end-to-end encryption public keys for
  344. Matrix users is also very security-sensitive for similar reasons.
  345. Therefore the role of managing trusted identity in the Matrix ecosystem is
  346. farmed out to a cluster of known trusted ecosystem partners, who run 'Matrix
  347. Identity Servers' such as ``sydent``, whose role is purely to authenticate and
  348. track 3PID logins and publish end-user public keys.
  349. It's currently early days for identity servers as Matrix is not yet using 3PIDs
  350. as the primary means of identity and E2E encryption is not complete. As such,
  351. we are running a single identity server (https://matrix.org) at the current
  352. time.
  353. Where's the spec?!
  354. ==================
  355. The source of the matrix spec lives at https://github.com/matrix-org/matrix-doc.
  356. A recent HTML snapshot of this lives at http://matrix.org/docs/spec
  357. Building Internal API Documentation
  358. ===================================
  359. Before building internal API documentation install sphinx and
  360. sphinxcontrib-napoleon::
  361. $ pip install sphinx
  362. $ pip install sphinxcontrib-napoleon
  363. Building internal API documentation::
  364. $ python setup.py build_sphinx