Skip to content

Commit

Permalink
Add peer awareness design document
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jan 3, 2021
1 parent b00cfe3 commit 3580618
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions design/peer-awareness.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Peer Awareness

Target version: Yorkie 0.1.2

## Use case

When multiple users edit a single document, the users want to know who is connected and what is being edited by the peers. For example, in Google Docs, the ID and profile of the user editing together are displayed in the upper-right.

## How to use

Users can pass metadata along with client options when creating a client.

```typescript
const client = yorkie.createClient('https://yorkie.dev/api', {
metadata: {
username: 'hackerwins'
}
});
```

Then the users create a document in the usual way and attach it to the client.

```typescript
const doc = yorkie.createDocument('examples', 'codemirror');
await client.attach(doc);
```

When a new peer registers or leaves, `peers-changed` event is fired, and the other peer's clientID and metadata can be obtained from the event.

```typescript
client.subscribe((event) => {
if (event.name === 'peers-changed') {
const peers = event.value[doc.getKey().toIDString()];
for (const [clientID, metadata] of Object.entries(peers)) {
console.log(clientID, metadata);
}
}
});
```

## Implementation
...

0 comments on commit 3580618

Please sign in to comment.