The Matrix Client-Server API is driven by a long-pollingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/matrix-org/matrix-js-sdk/llms.txt
Use this file to discover all available pages before exploring further.
/sync endpoint. The SDK handles the sync loop automatically once you call startClient(). Your application observes the sync lifecycle by listening to ClientEvent.Sync.
How sync works
WhenstartClient() is called, the SDK:
- Establishes (or restores from store) a filter for the
/syncrequest. - Sends a
/syncrequest with the last-knownsincetoken (or none, for a fresh session). - Processes the response — updating rooms, state, account data, and timelines.
- Emits
ClientEvent.Syncwith the newSyncState. - Immediately opens the next long-poll with
timeout=30000(configurable viapollTimeout).
RECONNECTING and eventually ERROR before retrying.
SyncState enum
Defined in src/sync.ts:
| State | Meaning |
|---|---|
PREPARED | First sync completed; the client is ready. Always followed immediately by SYNCING. |
SYNCING | The sync loop is running normally; emitted after each successful poll. |
RECONNECTING | A sync request failed but the keepalive (/versions) succeeded; will retry. |
ERROR | More than FAILED_SYNC_ERROR_THRESHOLD (3) consecutive failures, or the token is invalid. |
CATCHUP | Connection may have recovered; attempting to catch up with missed events. |
STOPPED | stopClient() was called; the loop is no longer running. |
State transition diagram
Handling sync state changes
Register the listener before callingstartClient():
ISyncStateData
The data argument passed to the ClientEvent.Sync callback has this shape:
startClient() options
Passed as IStartClientOpts to client.startClient():
Setting
lazyLoadMembers: true significantly reduces initial sync payload in large rooms. Members are loaded on demand when you call room.loadMembersIfNeeded().Presence
TheSetPresence enum controls the set_presence parameter sent to /sync:
disablePresence: true to startClient() to suppress automatic presence advertisements entirely.
Pagination (scrollback)
To load older events for a room, callscrollback:
SCROLLBACK_DELAY_MS (3 000 ms) guard to avoid hammering the server.
Sliding Sync (MSC4186)
Sliding Sync is an experimental alternative to the classic/sync API, designed for much faster initial load times by delivering only the rooms the client currently needs.
To opt in, construct a SlidingSync instance and pass it to startClient():
Stopping the client
stopClient(), the SyncState transitions to STOPPED and no further sync requests are issued. You can restart the sync loop by calling startClient() again.