Skip to content

Commit

Permalink
fixup! Update README to latest API
Browse files Browse the repository at this point in the history
  • Loading branch information
surminus committed Aug 17, 2023
1 parent 366a613 commit bf32ade
Showing 1 changed file with 64 additions and 8 deletions.
72 changes: 64 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,27 @@ You can also use Spaces with a CDN, such as [unpkg](https://www.unpkg.com/):
<script src="https://unpkg.com/@ably-labs/[email protected]/dist/iife/index.bundle.js"></script>
```

### Space membership
### Space

A space is the virtual, collaborative area of an application you want to monitor. A space can be anything from a web page, a sheet within a spreadsheet, an individual slide in a slideshow, or the slideshow itself. Create a space and listen for events to see when clients enter and leave.

Space membership is used to build avatar stacks and find out which members are online within a space.

```ts
// Create a new space
const space = await spaces.get('demoSlideshow');

// Register a listener to subscribe to events of when users enter or leave the space
space.members.subscribe((memberUpdate) => {
console.log(memberUpdate);
// Subscribe to space state events
space.subscribe('update', (spaceState) => {
console.log(spaceState.members);
});

// Enter a space, publishing a memberUpdate event, including optional profile data
// Enter a space, publishing an update event, including optional profile data
space.enter({
username: 'Claire Lemons',
avatar: 'https://slides-internal.com/users/clemons.png',
});
```

The following is an example `memberUpdate` event received by listeners when a user enters a space:
The following is an example `update` event received by listeners when a user enters a space:

```json
[
Expand All @@ -126,6 +124,64 @@ The following is an example `memberUpdate` event received by listeners when a us
]
```

### Members

Members is used to build avatar stacks and find out which members are online within a space.

```ts
// Subscribe to all member events in a space by explicitly passing the 'update' event
space.members.subscribe('update', (memberUpdate) => {
console.log(memberUpdate);
});

// Not passing the event name is the same behaviour as above
space.members.subscribe((memberUpdate) => {
console.log(memberUpdate);
});

// Subscribe to when a member enters the space
space.members.subscribe('enter', (memberJoined) => {
console.log(memberJoined);
});

// Subscribe to when a member leaves the space
space.members.subscribe('leave', (memberLeft) => {
console.log(memberLeft);
});
```

The following is an example `memberUpdate` event received by listeners:

```json
{
"clientId": "clemons#142",
"connectionId": "hd9743gjDc",
"isConnected": true,
"lastEvent": {
"name": "enter",
"timestamp": 1677595689759
},
"location": null,
"profileData": {
"username": "Claire Lemons",
"avatar": "https://slides-internal.com/users/clemons.png"
}
}
```

Members has methods to understand the current member state:

```ts
// Get all members in a space
const allMembers = space.members.getAll();

// Get your own member object
const myMemberInfo = space.members.getSelf();

// Get everyone else's member object but yourself
const othersMemberInfo = space.members.getOthers();
```

### Location

Member locations enable you to track where users are within an application. A location could be a form field, multiple cells in a spreadsheet or a slide in a slide deck editor. Subscribe to all location updates, specific location, or locations changes for a given member.
Expand Down

0 comments on commit bf32ade

Please sign in to comment.