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

Examples of API usage #108

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = {
extends: ['plugin:jsdoc/recommended'],
},
],
ignorePatterns: ['dist', 'build'],
ignorePatterns: ['dist', 'build', 'examples'],
settings: {
jsdoc: {
tagNamePreference: {
Expand Down
28 changes: 28 additions & 0 deletions examples/avatar-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Spaces from '@ably-labs/spaces';
import { Realtime } from 'ably';

import { renderAvatars, renderNotification } from './my-application';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will my-application usually be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is meant to represent any developer owned code, components etc


// Create Ably client
const client = new Realtime.Promise({ authUrl: '<auth-endpoint>', clientId: '<client-ID>' });

// Initialize the Spaces SDK with an Ably client
const spaces = new Spaces(client);

// Create a new space
const space = await spaces.get('slide-deck-224');

// Enter a space to become a member
await space.enter({ name: 'Kyle' });

// Listen to the member changes within a space
// Triggers for members entering/ leaving a space or updating their profile
space.members.subscribe('update', () => {
const otherMembers = space.members.getOthers();
renderAvatars(otherMembers);
});

// Listen to leave events only to show a notification
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the renderAvatars method not be called here again to reflect the change (recently left) in the UI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea here is the actual component is managed by members.subscribe('update', and the other subscriber is for a completely different piece of UI (notifications).

Saying that I think with our API design changes the first subscriber needs to be subscribe(['enter', 'update', 'leave'],

space.members.subscribe('leave', (member) => {
renderNotification('memberHasLeft', member);
});
27 changes: 27 additions & 0 deletions examples/live-cursors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Spaces from '@ably-labs/spaces';
import { Realtime } from 'ably';

import renderCursor from './my-application';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as in avatar-stack


// Create Ably client
const client = new Realtime.Promise({ authUrl: '<auth-endpoint>', clientId: '<client-ID>' });

// Initialize the Spaces SDK with an Ably client
const spaces = new Spaces(client);

// Create a new space
const space = await spaces.get('slide-deck-224');

// Enter a space to become a member
space.enter({ name: 'Helmut' });

// Listen to all changes to all members within a space
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Listen to all changes to all members within a space
// Listen to cursor position updates from members within a space
// and apply the new position for that member's cursor component

space.cursors.subscribe('update', (cursorUpdate) => {
const member = space.members.getAll().find((member) => member.connectionId === cursorUpdate.connectionId);
renderCursor(cursorUpdate, member);
});

// Publish cursor events to other members
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Publish cursor events to other members
// Publish your cursor position to other members

window.addEventListener('mousemove', ({ clientX, clientY }) => {
space.cursors.set({ position: { x: clientX, y: clientY } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// The Spaces SDK automatically batches position updates
// so you don't end up publishing millions of messages unnecessarily

});
25 changes: 25 additions & 0 deletions examples/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Spaces from '@ably-labs/spaces';
import { Realtime } from 'ably';

import updateLocationsForMember from './my-application';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as in avatar-stack


// Create Ably client
const client = new Realtime.Promise({ authUrl: '<auth-endpoint>', clientId: '<client-ID>' });

// Initialize the Spaces SDK with an Ably client
const spaces = new Spaces(client);

// Create a new space
const space = await spaces.get('slide-deck-224');

// Enter a space to become a member
await space.enter({ name: 'Amelie' });

// Subscribe to all members' location updates
space.locations.subscribe('update', ({ member, currentLocation, previousLocation }) => {
// Update UI to reflect other members locations
updateLocationsForMember(member, currentLocation, previousLocation);
dpiatek marked this conversation as resolved.
Show resolved Hide resolved
});

// Set your location
space.locations.set({ slide: 0, elementId: 'title' });
35 changes: 35 additions & 0 deletions examples/locking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Spaces, { LockStatus } from '@ably-labs/spaces';
import { Realtime } from 'ably';

import { enableLocationEditing, lockId } from './my-application';

// Create Ably client
const client = new Realtime.Promise({ authUrl: '<auth-endpoint>', clientId: '<client-ID>' });

// Initialize the Spaces SDK with an Ably client
const spaces = new Spaces(client);

// Create a new space
const space = await spaces.get('slide-deck-224');

// Enter a space to become a member
await space.enter({ name: 'Yoshi' });

const isLocked = space.locks.get(lockId);

if (!isLocked) {
await space.locks.acquire(lockId);
}

// Update UI when parts of the UI are locked
space.locks.subscribe('update', (lock) => {
const self = space.members.getSelf();

if (lock.request.status === LockStatus.LOCKED && self.connectionId === lock.member.connectionId) {
const location = {
slide: lock.request.attributes.get('slide'),
elementId: lock.request.attributes.get('elementId'),
};
enableLocationEditing({ location });
}
});
Loading