-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the idea here is the actual component is managed by Saying that I think with our API design changes the first subscriber needs to be |
||
space.members.subscribe('leave', (member) => { | ||
renderNotification('memberHasLeft', member); | ||
}); |
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'; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
window.addEventListener('mousemove', ({ clientX, clientY }) => { | ||||||||
space.cursors.set({ position: { x: clientX, y: clientY } }); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // The Spaces SDK automatically batches position updates |
||||||||
}); |
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' }); |
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 }); | ||
} | ||
}); |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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