summary refs log tree commit diff
path: root/docs/client-server/howto.rst
blob: c02ea8d89705328a90cf098796403e4f613679f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
.. TODO kegan
  Room config (specifically: message history,
  public rooms). /register seems super simplistic compared to /login, maybe it
  would be better if /register used the same technique as /login? /register should
  be "user" not "user_id".


How to use the client-server API
================================

This guide focuses on how the client-server APIs *provided by the reference 
home server* can be used. Since this is specific to a home server 
implementation, there may be variations in relation to registering/logging in
which are not covered in extensive detail in this guide.

If you haven't already, get a home server up and running on 
``http://localhost:8008``.


Accounts
========
Before you can send and receive messages, you must **register** for an account. 
If you already have an account, you must **login** into it.

`Try out the fiddle`__

.. __: http://jsfiddle.net/4q2jyxng/

Registration
------------
The aim of registration is to get a user ID and access token which you will need
when accessing other APIs::

    curl -XPOST -d '{"user_id":"example", "password":"wordpass"}' "http://localhost:8008/_matrix/client/api/v1/register"

    {
        "access_token": "QGV4YW1wbGU6bG9jYWxob3N0.AqdSzFmFYrLrTmteXc", 
        "home_server": "localhost", 
        "user_id": "@example:localhost"
    }

NB: If a ``user_id`` is not specified, one will be randomly generated for you. 
If you do not specify a ``password``, you will be unable to login to the account
if you forget the ``access_token``.

Implementation note: The matrix specification does not enforce how users 
register with a server. It just specifies the URL path and absolute minimum 
keys. The reference home server uses a username/password to authenticate user,
but other home servers may use different methods.

Login
-----
The aim when logging in is to get an access token for your existing user ID::

    curl -XGET "http://localhost:8008/_matrix/client/api/v1/login"

    {
        "flows": [
            {
                "type": "m.login.password"
            }
        ]
    }

    curl -XPOST -d '{"type":"m.login.password", "user":"example", "password":"wordpass"}' "http://localhost:8008/_matrix/client/api/v1/login"

    {
        "access_token": "QGV4YW1wbGU6bG9jYWxob3N0.vRDLTgxefmKWQEtgGd", 
        "home_server": "localhost", 
        "user_id": "@example:localhost"
    }
    
Implementation note: Different home servers may implement different methods for 
logging in to an existing account. In order to check that you know how to login 
to this home server, you must perform a ``GET`` first and make sure you 
recognise the login type. If you do not know how to login, you can 
``GET /login/fallback`` which will return a basic webpage which you can use to 
login. The reference home server implementation support username/password login,
but other home servers may support different login methods (e.g. OAuth2).


Communicating
=============

In order to communicate with another user, you must **create a room** with that 
user and **send a message** to that room. 

`Try out the fiddle`__

.. __: http://jsfiddle.net/zL3zto9g/

Creating a room
---------------
If you want to send a message to someone, you have to be in a room with them. To
create a room::

    curl -XPOST -d '{"room_alias_name":"tutorial"}' "http://localhost:8008/_matrix/client/api/v1/createRoom?access_token=YOUR_ACCESS_TOKEN"

    {
        "room_alias": "#tutorial:localhost", 
        "room_id": "!CvcvRuDYDzTOzfKKgh:localhost"
    }
    
The "room alias" is a human-readable string which can be shared with other users
so they can join a room, rather than the room ID which is a randomly generated
string. You can have multiple room aliases per room.

.. TODO(kegan)
  How to add/remove aliases from an existing room.
    

Sending messages
----------------
You can now send messages to this room::

    curl -XPOST -d '{"msgtype":"m.text", "body":"hello"}' "http://localhost:8008/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh%3Alocalhost/send/m.room.message?access_token=YOUR_ACCESS_TOKEN"
    
    {
        "event_id": "YUwRidLecu"
    }
    
The event ID returned is a unique ID which identifies this message.
    
NB: There are no limitations to the types of messages which can be exchanged.
The only requirement is that ``"msgtype"`` is specified. The Matrix 
specification outlines the following standard types: ``m.text``, ``m.image``,
``m.audio``, ``m.video``, ``m.location``, ``m.emote``. See the specification for
more information on these types.

Users and rooms
===============

Each room can be configured to allow or disallow certain rules. In particular,
these rules may specify if you require an **invitation** from someone already in
the room in order to **join the room**. In addition, you may also be able to 
join a room **via a room alias** if one was set up.

`Try out the fiddle`__

.. __: http://jsfiddle.net/7fhotf1b/

Inviting a user to a room
-------------------------
You can directly invite a user to a room like so::

    curl -XPOST -d '{"user_id":"@myfriend:localhost"}' "http://localhost:8008/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh%3Alocalhost/invite?access_token=YOUR_ACCESS_TOKEN"
    
This informs ``@myfriend:localhost`` of the room ID 
``!CvcvRuDYDzTOzfKKgh:localhost`` and allows them to join the room.

Joining a room via an invite
----------------------------
If you receive an invite, you can join the room::

    curl -XPOST -d '{}' "http://localhost:8008/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh%3Alocalhost/join?access_token=YOUR_ACCESS_TOKEN"
    
NB: Only the person invited (``@myfriend:localhost``) can change the membership
state to ``"join"``. Repeatedly joining a room does nothing.

Joining a room via an alias
---------------------------
Alternatively, if you know the room alias for this room and the room config 
allows it, you can directly join a room via the alias::

    curl -XPOST -d '{}' "http://localhost:8008/_matrix/client/api/v1/join/%23tutorial%3Alocalhost?access_token=YOUR_ACCESS_TOKEN"
    
    {
        "room_id": "!CvcvRuDYDzTOzfKKgh:localhost"
    }
    
You will need to use the room ID when sending messages, not the room alias.

NB: If the room is configured to be an invite-only room, you will still require
an invite in order to join the room even though you know the room alias. As a
result, it is more common to see a room alias in relation to a public room, 
which do not require invitations.

Getting events
==============
An event is some interesting piece of data that a client may be interested in. 
It can be a message in a room, a room invite, etc. There are many different ways
of getting events, depending on what the client already knows.

`Try out the fiddle`__

.. __: http://jsfiddle.net/vw11mg37/

Getting all state
-----------------
If the client doesn't know any information on the rooms the user is 
invited/joined on, they can get all the user's state for all rooms::

    curl -XGET "http://localhost:8008/_matrix/client/api/v1/initialSync?access_token=YOUR_ACCESS_TOKEN"
    
    {
        "end": "s39_18_0", 
        "presence": [
            {
                "content": {
                    "last_active_ago": 1061436, 
                    "user_id": "@example:localhost"
                }, 
                "type": "m.presence"
            }
        ], 
        "rooms": [
            {
                "membership": "join", 
                "messages": {
                    "chunk": [
                        {
                            "content": {
                                "@example:localhost": 10, 
                                "default": 0
                            }, 
                            "event_id": "wAumPSTsWF", 
                            "required_power_level": 10, 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "", 
                            "ts": 1409665585188, 
                            "type": "m.room.power_levels", 
                            "user_id": "@example:localhost"
                        }, 
                        {
                            "content": {
                                "join_rule": "public"
                            }, 
                            "event_id": "jrLVqKHKiI", 
                            "required_power_level": 10, 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "", 
                            "ts": 1409665585188, 
                            "type": "m.room.join_rules", 
                            "user_id": "@example:localhost"
                        }, 
                        {
                            "content": {
                                "level": 10
                            }, 
                            "event_id": "WpmTgsNWUZ", 
                            "required_power_level": 10, 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "", 
                            "ts": 1409665585188, 
                            "type": "m.room.add_state_level", 
                            "user_id": "@example:localhost"
                        }, 
                        {
                            "content": {
                                "level": 0
                            }, 
                            "event_id": "qUMBJyKsTQ", 
                            "required_power_level": 10, 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "", 
                            "ts": 1409665585188, 
                            "type": "m.room.send_event_level", 
                            "user_id": "@example:localhost"
                        }, 
                        {
                            "content": {
                                "ban_level": 5, 
                                "kick_level": 5
                            }, 
                            "event_id": "YAaDmKvoUW", 
                            "required_power_level": 10, 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "", 
                            "ts": 1409665585188, 
                            "type": "m.room.ops_levels", 
                            "user_id": "@example:localhost"
                        }, 
                        {
                            "content": {
                                "avatar_url": null, 
                                "displayname": null, 
                                "membership": "join"
                            }, 
                            "event_id": "RJbPMtCutf", 
                            "membership": "join", 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "@example:localhost", 
                            "ts": 1409665586730, 
                            "type": "m.room.member", 
                            "user_id": "@example:localhost"
                        }, 
                        {
                            "content": {
                                "body": "hello", 
                                "hsob_ts": 1409665660439, 
                                "msgtype": "m.text"
                            }, 
                            "event_id": "YUwRidLecu", 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "ts": 1409665660439, 
                            "type": "m.room.message", 
                            "user_id": "@example:localhost"
                        }, 
                        {
                            "content": {
                                "membership": "invite"
                            }, 
                            "event_id": "YjNuBKnPsb", 
                            "membership": "invite", 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "@myfriend:localhost", 
                            "ts": 1409666426819, 
                            "type": "m.room.member", 
                            "user_id": "@example:localhost"
                        }, 
                        {
                            "content": {
                                "avatar_url": null, 
                                "displayname": null, 
                                "membership": "join", 
                                "prev": "join"
                            }, 
                            "event_id": "KWwdDjNZnm", 
                            "membership": "join", 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "@example:localhost", 
                            "ts": 1409666551582, 
                            "type": "m.room.member", 
                            "user_id": "@example:localhost"
                        }, 
                        {
                            "content": {
                                "avatar_url": null, 
                                "displayname": null, 
                                "membership": "join"
                            }, 
                            "event_id": "JFLVteSvQc", 
                            "membership": "join", 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "@example:localhost", 
                            "ts": 1409666587265, 
                            "type": "m.room.member", 
                            "user_id": "@example:localhost"
                        }
                    ], 
                    "end": "s39_18_0", 
                    "start": "t1-11_18_0"
                }, 
                "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                "state": [
                    {
                        "content": {
                            "creator": "@example:localhost"
                        }, 
                        "event_id": "dMUoqVTZca", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.create", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "@example:localhost": 10, 
                            "default": 0
                        }, 
                        "event_id": "wAumPSTsWF", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.power_levels", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "join_rule": "public"
                        }, 
                        "event_id": "jrLVqKHKiI", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.join_rules", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "level": 10
                        }, 
                        "event_id": "WpmTgsNWUZ", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.add_state_level", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "level": 0
                        }, 
                        "event_id": "qUMBJyKsTQ", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.send_event_level", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "ban_level": 5, 
                            "kick_level": 5
                        }, 
                        "event_id": "YAaDmKvoUW", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.ops_levels", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "membership": "invite"
                        }, 
                        "event_id": "YjNuBKnPsb", 
                        "membership": "invite", 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "@myfriend:localhost", 
                        "ts": 1409666426819, 
                        "type": "m.room.member", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "avatar_url": null, 
                            "displayname": null, 
                            "membership": "join"
                        }, 
                        "event_id": "JFLVteSvQc", 
                        "membership": "join", 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "@example:localhost", 
                        "ts": 1409666587265, 
                        "type": "m.room.member", 
                        "user_id": "@example:localhost"
                    }
                ]
            }
        ]
    }
    
This returns all the room information the user is invited/joined on, as well as
all of the presences relevant for these rooms. This can be a LOT of data. You
may just want the most recent event for each room. This can be achieved by 
applying query parameters to ``limit`` this request::

    curl -XGET "http://localhost:8008/_matrix/client/api/v1/initialSync?limit=1&access_token=YOUR_ACCESS_TOKEN"
    
    {
        "end": "s39_18_0", 
        "presence": [
            {
                "content": {
                    "last_active_ago": 1279484, 
                    "user_id": "@example:localhost"
                }, 
                "type": "m.presence"
            }
        ], 
        "rooms": [
            {
                "membership": "join", 
                "messages": {
                    "chunk": [
                        {
                            "content": {
                                "avatar_url": null, 
                                "displayname": null, 
                                "membership": "join"
                            }, 
                            "event_id": "JFLVteSvQc", 
                            "membership": "join", 
                            "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                            "state_key": "@example:localhost", 
                            "ts": 1409666587265, 
                            "type": "m.room.member", 
                            "user_id": "@example:localhost"
                        }
                    ], 
                    "end": "s39_18_0", 
                    "start": "t10-30_18_0"
                }, 
                "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                "state": [
                    {
                        "content": {
                            "creator": "@example:localhost"
                        }, 
                        "event_id": "dMUoqVTZca", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.create", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "@example:localhost": 10, 
                            "default": 0
                        }, 
                        "event_id": "wAumPSTsWF", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.power_levels", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "join_rule": "public"
                        }, 
                        "event_id": "jrLVqKHKiI", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.join_rules", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "level": 10
                        }, 
                        "event_id": "WpmTgsNWUZ", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.add_state_level", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "level": 0
                        }, 
                        "event_id": "qUMBJyKsTQ", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.send_event_level", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "ban_level": 5, 
                            "kick_level": 5
                        }, 
                        "event_id": "YAaDmKvoUW", 
                        "required_power_level": 10, 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "", 
                        "ts": 1409665585188, 
                        "type": "m.room.ops_levels", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "membership": "invite"
                        }, 
                        "event_id": "YjNuBKnPsb", 
                        "membership": "invite", 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "@myfriend:localhost", 
                        "ts": 1409666426819, 
                        "type": "m.room.member", 
                        "user_id": "@example:localhost"
                    }, 
                    {
                        "content": {
                            "avatar_url": null, 
                            "displayname": null, 
                            "membership": "join"
                        }, 
                        "event_id": "JFLVteSvQc", 
                        "membership": "join", 
                        "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", 
                        "state_key": "@example:localhost", 
                        "ts": 1409666587265, 
                        "type": "m.room.member", 
                        "user_id": "@example:localhost"
                    }
                ]
            }
        ]
    }

Getting live state
------------------
Once you know which rooms the client has previously interacted with, you need to
listen for incoming events. This can be done like so::

    curl -XGET "http://localhost:8008/_matrix/client/api/v1/events?access_token=YOUR_ACCESS_TOKEN"
    
    {
        "chunk": [], 
        "end": "s39_18_0", 
        "start": "s39_18_0"
    }
    
This will block waiting for an incoming event, timing out after several seconds.
Even if there are no new events (as in the example above), there will be some
pagination stream response keys. The client should make subsequent requests 
using the value of the ``"end"`` key (in this case ``s39_18_0``) as the ``from`` 
query parameter e.g. ``http://localhost:8008/_matrix/client/api/v1/events?access
_token=YOUR_ACCESS_TOKEN&from=s39_18_0``. This value should be stored so when the 
client reopens your app after a period of inactivity, you can resume from where 
you got up to in the event stream. If it has been a long period of inactivity, 
there may be LOTS of events waiting for the user. In this case, you may wish to 
get all state instead and then resume getting live state from a newer end token.

NB: The timeout can be changed by adding a ``timeout`` query parameter, which is
in milliseconds. A timeout of 0 will not block.


Example application
-------------------
The following example demonstrates registration and login, live event streaming,
creating and joining rooms, sending messages, getting member lists and getting 
historical messages for a room. This covers most functionality of a messaging
application.

`Try out the fiddle`__

.. __: http://jsfiddle.net/uztL3yme/