Live cursors

The live cursors feature enables you to track the cursors of members within a space in realtime.

Cursor events are emitted whenever a member moves their mouse within a space. In order to optimize the efficiency and frequency of updates, cursor position events are automatically batched. The batching interval may be customized in order to further optimize for increased performance versus the number of events published.

Live cursor updates are not available as part of the space state and must be subscribed to using space.cursors.subscribe().

Set the position of a member’s cursor using the set() method. A position must contain an X-axis value and a Y-axis value to set the cursor position on a 2D plane. Calling set() will emit a cursor event so that other members are informed of the cursor movement in realtime.

A member must have been entered into the space to set their cursor position.

The set() method takes the following parameters:

Parameter Description Type
position.x The position of the member’s cursor on the X-axis. Number
position.y The position of the member’s cursor on the Y-axis. Number
data An optional arbitrary JSON-serializable object containing additional information about the cursor, such as a color. Object

The following is an example of a member setting their cursor position by adding an event listener to obtain their cursor coordinates and then publishing their position using the set() method:

JavaScript v1.2
window.addEventListener('mousemove', ({ clientX, clientY }) => { space.cursors.set({ position: { x: clientX, y: clientY }, data: { color: 'red' } }); });
Copied!

The following is an example payload of a cursor event. Cursor events are uniquely identifiable by the connectionId of a cursor.

{ "hd9743gjDc": { "connectionId": "hd9743gjDc", "clientId": "clemons#142", "position": { "x": 864, "y": 32 }, "data": { "color": "red" } } }
Copied!

The following are the properties of a cursor event payload:

Property Description Type
connectionId The unique identifier of the member’s connection. String
clientId The client identifier for the member. String
position An object containing the position of a member’s cursor. Object
position.x The position of the member’s cursor on the X-axis. Number
position.y The position of the member’s cursor on the Y-axis. Number
data An optional arbitrary JSON-serializable object containing additional information about the cursor. Object

Subscribe to cursor events by registering a listener. Cursor events are emitted whenever a member moves their cursor by calling set(). Use the subscribe() method on the cursors object of a space to receive updates.

The following is an example of subscribing to cursor events:

JavaScript v1.2
space.cursors.subscribe('update', (cursorUpdate) => { console.log(cursorUpdate); });
Copied!

Unsubscribe from cursor events to remove previously registered listeners.

The following is an example of removing a listener for cursor update events:

JavaScript v1.2
space.cursors.unsubscribe(`update`, listener);
Copied!

Or remove all listeners:

JavaScript v1.2
space.cursors.unsubscribe();
Copied!

Cursor options are set when creating or retrieving a Space instance. They are used to control the behavior of live cursors.

The following cursor options can be set:

The outboundBatchInterval is the interval at which a batch of cursor positions are published, in milliseconds, for each client. This is multiplied by the number of members in a space.

The default value is 25ms which is optimal for the majority of use cases. If you wish to optimize the interval further, then decreasing the value will improve performance by further ‘smoothing’ the movement of cursors at the cost of increasing the number of events sent. Be aware that at a certain point the rate at which a browser is able to render the changes will impact optimizations.

The volume of messages sent can be high when using live cursors. Because of this, the last known position of every members’ cursor is obtained from history. The paginationLimit is the number of pages that should be searched to find the last position of each cursor. The default is 5.

Cursor positions can be retrieved in one-off calls. These are local calls that retrieve the latest position of cursors retained in memory by the SDK.

The following is an example of retrieving a member’s own cursor position:

JavaScript v1.2
const myCursor = await space.cursors.getSelf();
Copied!

The following is an example payload returned by space.cursors.getSelf()

{ “clientId”: “DzOBJqgGXzyUBb816Oa6i”, “connectionId”: “__UJBKZchX”, "position": { "x": 864, "y": 32 } }
Copied!

The following is an example of retrieving the cursor positions for all members other than the member themselves:

JavaScript v1.2
const othersCursors = await space.cursors.getOthers();
Copied!

The following is an example payload returned by space.cursors.getOthers()

{ "3ej3q7yZZz": { "clientId": "yyXidHatpP3hJpMpXZi8W", "connectionId": "3ej3q7yZZz", "position": { "x": 12, "y": 3 } }, "Z7CA3-1vlR": { "clientId": "b18mj5B5hm-govdFEYRyb", "connectionId": "Z7CA3-1vlR", "position": { "x": 502, "y": 43 } } }
Copied!

The following is an example of retrieving the cursor positions for all members, including the member themselves. getAll() is useful for retrieving the initial position of members’ cursors.

JavaScript v1.2
const allCursors = await space.cursors.getAll();
Copied!

The following is an example payload returned by space.cursors.getAll()

{ "3ej3q7yZZz": { "clientId": "yyXidHatpP3hJpMpXZi8W", "connectionId": "3ej3q7yZZz", "position": { "x": 12, "y": 3 } }, "Z7CA3-1vlR": { "clientId": "b18mj5B5hm-govdFEYRyb", "connectionId": "Z7CA3-1vlR", "position": { "x": 502, "y": 43 } }, "__UJBKZchX": { “clientId”: “DzOBJqgGXzyUBb816Oa6i”, “connectionId”: “__UJBKZchX”, "position": { "x": 864, "y": 32 } } }
Copied!

The following is an example of the steps involved in implementing live cursors.

JavaScript v1.2
import Spaces from '@ably/spaces'; import { Realtime } from 'ably'; // Import your custom logic for handling live cursors import { renderCursor } from '/src/own-logic'; // Create an Ably client const client = new Realtime.Promise({ authUrl: '<authEndpoint>', clientId: '<clientId>' }); // Initialize the Spaces SDK using the Ably client const spaces = new Spaces(client); // Create a new space const space = await spaces.get('board-presentation'); // Enter the space to become a member, passing a nickname await space.enter({ name: 'Helmut' }); // Listen for cursor updates from members space.cursors.subscribe('update', async (cursorUpdate) => { // Use getAll() and filter by the member that moved their cursor to only update the position of that member's cursor const members = await space.members.getAll(); const member = members.find((member) => member.connectionId === cursorUpdate.connectionId); renderCursor(cursorUpdate, member); }); // Publish the member's cursor position window.addEventListener('mousemove', ({ clientX, clientY }) => { space.cursors.set({ position: { x: clientX, y: clientY } }); });
Copied!

The Spaces SDK is built upon existing Ably functionality available in Ably’s Core SDKs. Understanding which core features are used to provide the abstractions in the Spaces SDK enables you to manage space state and build additional functionality into your application.

Live cursors build upon the functionality of the Pub/Sub Channels presence feature.

Due to the high frequency at which updates are streamed for cursor movements, live cursors utilizes its own channel. The other features of the Spaces SDK, such as avatar stacks, member locations and component locking all share a single channel. For this same reason, cursor position updates are not included in the space state and may only be subscribed to on the cursors namespace.

The channel is only created when a member calls space.cursors.set(). The live cursors channel object can be accessed through space.cursors.channel. To monitor the underlying state of the cursors channel, the channel object can be accessed through space.cursors.channel.

Set cursor position
v1.2