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 cursor position

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:

ParameterDescriptionType
position.xThe position of the member's cursor on the X-axis.Number
position.yThe position of the member's cursor on the Y-axis.Number
dataAn 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

Subscribe to cursor events

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

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

JSON

The following are the properties of a cursor event payload:

PropertyDescriptionType
connectionIdThe unique identifier of the member's connection.String
clientIdThe client identifier for the member.String
positionAn object containing the position of a member's cursor.Object
position.xThe position of the member's cursor on the X-axis.Number
position.yThe position of the member's cursor on the Y-axis.Number
dataAn optional arbitrary JSON-serializable object containing additional information about the cursor.Object

Unsubscribe from cursor events

Unsubscribe from cursor events to remove previously registered listeners.

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

JavaScript

Or remove all listeners:

JavaScript

Cursor options

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

outboundBatchInterval

The outboundBatchInterval is the interval at which a batch of cursor positions are published, in milliseconds, for each client. This interval increases as the number of members in a space grows, adjusting based on groups of 100 members.

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.

paginationLimit

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.

Retrieve cursors

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

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

JSON

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

JavaScript

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

JSON

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

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

JSON

Example usage

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

JavaScript

Live cursor foundations

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 Ably Pub/Sub 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.

Select...