Set member location
Use theset() method to emit a location event in realtime when a member changes their location. This will be received by all location subscribers to inform them of the location change. A location can be any JSON-serializable object, such as a slide number or element ID.
A member must have been entered into the space to set their location.
The set() method is commonly combined with addEventListener() or a React synthetic event, such as onClick or onHover.
The following is an example of a member setting their location to a specific slide number, and element on that slide:
Subscribe to location events
Subscribe to location events by registering a listener. Location events are emitted whenever a member changes location by callingset(). Use the subscribe() method on the locations namespace of the space to receive updates.
All location changes are update events. When a location update is received, clear the highlight from the UI component of the member’s previousLocation and add it to currentLocation.
The following is an example of subscribing to location events:
The following is an example payload of a location event. Information about location is returned in currentLocation and previousLocation:
The following are the properties of a location event payload:
| Property | Description | Type |
|---|---|---|
| member.clientId | The client identifier for the member. | String |
| member.connectionId | The unique identifier of the member’s connection. | String |
| member.isConnected | Whether the member is connected to Ably or not. | Boolean |
| member.lastEvent.name | The most recent event emitted by the member. Will be one of enter, update, present, or leave. | String |
| member.lastEvent.timestamp | The timestamp of the most recently emitted event. | Number |
| member.profileData | The optional profile data associated with the member. | Object |
| previousLocation | The previous location of the member. | Object |
| currentLocation | The new location of the member. | Object |
Unsubscribe from location events
Unsubscribe from location events to remove previously registered listeners. The following is an example of removing a listener for location update events: Or remove all listeners:Retrieve member locations
Member locations can also be retrieved in one-off calls. These are local calls and retrieve the location of members retained in memory by the SDK. The following is an example of retrieving a member’s own location: The following is an example payload returned byspace.locations.getSelf(). It will return the properties of the member’s location:
The following is an example of retrieving the location objects of all members other than the member themselves:
The following is an example payload returned by space.locations.getOthers(). It will return the properties of all member’s location by their connectionId:
The following is an example of retrieving the location objects of all members, including the member themselves:
The following is an example payload returned by space.locations.getAll(). It will return the properties of all member’s location by their connectionId:
