Documentation 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.
MatrixClient is the main entry point for the matrix-js-sdk. Every interaction with a Matrix homeserver — syncing, sending events, managing rooms, calling APIs — goes through a single MatrixClient instance. It extends TypedEventEmitter, so your application reacts to state changes and incoming data by registering event listeners rather than polling.
Creating a client
Use thecreateClient helper exported from matrix-js-sdk. It accepts an ICreateClientOpts object.
Key options
baseUrl (required)
baseUrl (required)
The base URL of the homeserver, e.g.
https://matrix.org. All HTTP requests are made relative to this URL.accessToken
accessToken
The access token to authenticate requests. Omit only if the client will be used unauthenticated (e.g. to list public rooms).
userId
userId
The fully-qualified Matrix user ID (
@localpart:server). Required for most authenticated operations.deviceId
deviceId
A unique identifier for this device. Required when end-to-end encryption is enabled — if unset, E2EE is disabled.
store
store
An
IStore implementation for persisting sync data. Defaults to a MemoryStore if omitted. See Storage for details.scheduler
scheduler
A
MatrixScheduler instance for queuing and retrying outbound events. Without one, failed sends are not retried automatically.cryptoStore
cryptoStore
A
CryptoStore for E2EE session data. Used by the legacy crypto stack and for migration to the Rust crypto backend.logger
logger
A custom
Logger instance. Defaults to the built-in global logger.Starting and stopping the client
Creating a client does not start syncing. CallstartClient() to begin the sync loop, and stopClient() to shut it down.
Register a sync listener before starting
Register at least a
ClientEvent.Sync listener before calling startClient() so you don’t miss the initial PREPARED transition.IStartClientOpts
The options object passed to startClient() controls sync behaviour:
| Option | Type | Default | Description |
|---|---|---|---|
initialSyncLimit | number | 8 | Number of events to fetch per room on first sync |
lazyLoadMembers | boolean | false | Defer loading room members until explicitly requested |
pendingEventOrdering | PendingEventOrdering | chronological | Where locally-sent (pending) events appear in the timeline |
pollTimeout | number | 30000 | Milliseconds to hold open each /sync long-poll request |
disablePresence | boolean | false | Suppress automatic presence updates |
threadSupport | boolean | false | Organise events into Thread objects when thread relations are found |
slidingSync | SlidingSync | — | Pass a SlidingSync instance to use MSC4186 instead of the classic sync API |
The event-driven model
MatrixClient extends TypedEventEmitter. Rather than polling the client for new data, register listeners for the events you care about.
ClientEvent enum
| Value | Constant | Description |
|---|---|---|
"sync" | ClientEvent.Sync | Sync state changed (see Sync) |
"event" | ClientEvent.Event | A live event arrived via /sync |
"accountData" | ClientEvent.AccountData | User-scoped account data was updated |
"Room" | ClientEvent.Room | A new room object was created |
"deleteRoom" | ClientEvent.DeleteRoom | A room was forgotten |
"WellKnown.client" | ClientEvent.ClientWellKnown | .well-known data was fetched |
"turnServers" | ClientEvent.TurnServers | TURN server credentials refreshed |
ClientEvent.ToDeviceEvent is deprecated. Use ClientEvent.ReceivedToDeviceMessage instead, which also provides encryption metadata.Authenticated versus unauthenticated usage
AMatrixClient without accessToken/userId can still make unauthenticated requests such as listing public rooms:
Widget embedding
For Matrix widget integrations, usecreateRoomWidgetClient instead of createClient. It sets up a client pre-wired to communicate over the widget postMessage transport.