This guide walks you through the essential steps to build a Matrix client withDocumentation 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.
matrix-js-sdk: installing the package, creating an authenticated client, syncing with the homeserver, sending a message, and listening for incoming events.
Create a client
Import the SDK and call
createClient with your homeserver URL, user ID, and access token.createClient automatically provisions a MemoryStore for event storage, a MatrixScheduler for message queuing, and a crypto store. All of these can be overridden via the options object.To make unauthenticated requests (for example, listing public rooms), you can create a minimal client with only baseUrl:Start syncing
Call Listen for the
startClient to begin the /sync loop. The initialSyncLimit option controls how many timeline events are fetched per room on the initial sync.ClientEvent.Sync event to know when the initial sync is complete and the client is ready to use:The
PREPARED state means the initial sync has completed and all room state is available locally. Do not attempt to read room data before this event fires.Send a message
Use The SDK handles local echo automatically: the message will appear in the room timeline with an
sendEvent to send an m.room.message event to a room. Replace "roomId" with the actual Matrix room ID (e.g. !abc123:localhost).EventStatus.SENDING status immediately, before the server response arrives.Auto-joining rooms
A common pattern is to automatically accept room invitations. Listen forRoomEvent.MyMembership and call joinRoom when the membership becomes invite:
Reading stored timeline events
By default,matrix-js-sdk uses MemoryStore to store events as they arrive. You can iterate over the current in-memory timeline for all rooms:
Authenticated media
Servers that support MSC3916 (Matrix 1.11) require anAuthorization header when fetching media. In Node.js environments you can construct an authenticated download URL as follows:
Full example
Theexamples/node directory in the repository contains a fully functional terminal chat client that demonstrates room listing, joining, messaging, member lists, and scrollback. To run it, edit app.js to set your homeserver, access_token, and user_id, then: