Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type errors in README.md #3766

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ if you do not have it already.
```javascript
import * as sdk from "matrix-js-sdk";
const client = sdk.createClient({ baseUrl: "https://matrix.org" });
client.publicRooms(function (err, data) {
console.log("Public Rooms: %s", JSON.stringify(data));
});
const data = await client.publicRooms({ limit: 10 });
console.log("Public Rooms: %s", JSON.stringify(data));
```

See below for how to include libolm to enable end-to-end-encryption. Please check
[the Node.js terminal app](examples/node) for a more complex example.

You can also use the sdk with [Deno](https://deno.land/) (`import npm:matrix-js-sdk`) but its not officialy supported.
You can also use the sdk with [Deno](https://deno.land/) (`import npm:matrix-js-sdk`) but its not officially supported.

To start the client:

Expand All @@ -58,7 +57,7 @@ await client.startClient({ initialSyncLimit: 10 });
You can perform a call to `/sync` to get the current state of the client:

```javascript
client.once("sync", function (state, prevState, res) {
client.once(sdk.ClientEvent.Sync, function (state, prevState, res) {
if (state === "PREPARED") {
console.log("prepared");
} else {
Expand All @@ -75,29 +74,32 @@ const content = {
body: "message text",
msgtype: "m.text",
};
client.sendEvent("roomId", "m.room.message", content, "", (err, res) => {
console.log(err);
});
const res = await client.sendEvent("roomId", "m.room.message", content, "");
console.log(res);
```

To listen for message events:

```javascript
client.on("Room.timeline", function (event, room, toStartOfTimeline) {
client.on(sdk.RoomEvent.Timeline, function (event, room, toStartOfTimeline) {
if (event.getType() !== "m.room.message") {
return; // only use messages
}
console.log(event.event.content.body);
console.log(event.event.content?.body);
});
```

By default, the `matrix-js-sdk` client uses the `MemoryStore` to store events as they are received. For example to iterate through the currently stored timeline for a room:

```javascript
Object.keys(client.store.rooms).forEach((roomId) => {
client.getRoom(roomId).timeline.forEach((t) => {
Object.keys(client.store.getRooms()).forEach((roomId) => {
client
.getRoom(roomId)
?.getLiveTimeline()
.getEvents()
.forEach((t) => {
console.log(t.event);
});
});
});
```

Expand Down Expand Up @@ -144,12 +146,12 @@ are updated.

```javascript
// Listen for low-level MatrixEvents
client.on("event", function (event) {
client.on(sdk.ClientEvent.Event, function (event) {
console.log(event.getType());
});

// Listen for typing changes
client.on("RoomMember.typing", function (event, member) {
client.on(sdk.RoomMemberEvent.Typing, function (event, member) {
if (member.typing) {
console.log(member.name + " is typing...");
} else {
Expand Down Expand Up @@ -211,7 +213,7 @@ const matrixClient = sdk.createClient({
### Automatically join rooms when invited

```javascript
matrixClient.on("RoomMember.membership", function (event, member) {
matrixClient.on(sdk.RoomMemberEvent.Membership, function (event, member) {
if (member.membership === "invite" && member.userId === myUserId) {
matrixClient.joinRoom(member.roomId).then(function () {
console.log("Auto-joined %s", member.roomId);
Expand All @@ -225,7 +227,7 @@ matrixClient.startClient();
### Print out messages for all rooms

```javascript
matrixClient.on("Room.timeline", function (event, room, toStartOfTimeline) {
matrixClient.on(sdk.RoomEvent.Timeline, function (event, room, toStartOfTimeline) {
if (toStartOfTimeline) {
return; // don't print paginated results
}
Expand All @@ -235,7 +237,7 @@ matrixClient.on("Room.timeline", function (event, room, toStartOfTimeline) {
console.log(
// the room name will update with m.room.name events automatically
"(%s) %s :: %s",
room.name,
room?.name,
event.getSender(),
event.getContent().body,
);
Expand All @@ -257,7 +259,7 @@ Output:
### Print out membership lists whenever they are changed

```javascript
matrixClient.on("RoomState.members", function (event, state, member) {
matrixClient.on(sdk.RoomStateEvent.Members, function (event, state, member) {
const room = matrixClient.getRoom(state.roomId);
if (!room) {
return;
Expand Down
Loading