[]
Manages presence information for real-time collaboration.
example
const uiComponent = new xxx.uiComponent();
const connection = new Client('ws://localhost:8080/').connect('room1');
const presence = new Presence(connection);
presence.subscribe().then(() => {
uiComponent.showPresences(presence.otherStates);
presence.submitLocalState({ userId: xxx, selection: xxx });
uiComponent.on('selectionChanges', () => {
presence.submitLocalStateField('selection', xxx);
});
presence.on('add', () => {
uiComponent.showPresences(presence.otherStates);
});
presence.on('update', () => {
uiComponent.showPresences(presence.otherStates);
});
presence.on('remove', () => {
uiComponent.showPresences(presence.otherStates);
});
});
Name | Description |
---|---|
P |
The type of presence data. |
• new Presence<P
>(connection
)
Name |
---|
P |
Name | Type |
---|---|
connection |
Connection |
• get
connection(): Connection
Retrieves the associated connection object.
Connection
The connection instance.
• get
id(): string
Retrieves the unique identifier of the presence instance.
string
The presence ID.
• get
localState(): undefined
| P
Retrieves the local presence state.
undefined
| P
The local presence data, or undefined if not set.
• get
otherStates(): IPresences
<P
>
Retrieves the presence states of other clients.
IPresences
<P
>
The collection of other clients' presence data.
▸ destroy(): void
Destroys the presence instance and cleans up resources.
void
▸ off<NAME
>(name
, f
): void
Removes a listener for a specific event.
Name | Type | Description |
---|---|---|
NAME |
extends keyof IPresenceEvents <P > |
The type of the event name, extending keyof IPresenceEvents . |
Name | Type | Description |
---|---|---|
name |
NAME |
The name of the event. |
f |
IPresenceEvents <P >[NAME ] |
The event handler function to remove. |
void
▸ on<NAME
>(name
, f
): IPresenceEvents
<P
>[NAME
]
Registers a listener for a specific event.
Name | Type | Description |
---|---|---|
NAME |
extends keyof IPresenceEvents <P > |
The type of the event name, extending keyof IPresenceEvents . |
Name | Type | Description |
---|---|---|
name |
NAME |
The name of the event. |
f |
IPresenceEvents <P >[NAME ] |
The event handler function. |
IPresenceEvents
<P
>[NAME
]
The registered event handler.
▸ once<NAME
>(name
, f
): void
Registers a one-time listener for a specific event.
Name | Type | Description |
---|---|---|
NAME |
extends keyof IPresenceEvents <P > |
The type of the event name, extending keyof IPresenceEvents . |
Name | Type | Description |
---|---|---|
name |
NAME |
The name of the event. |
f |
IPresenceEvents <P >[NAME ] |
The event handler function to call once. |
void
▸ removeLocalState(): void
Removes the local presence state from the server, notifying other clients.
void
▸ submitLocalState(p
): void
Submits the local presence state to the server for broadcasting to other clients.
Name | Type | Description |
---|---|---|
p |
P |
The local presence data to submit. |
void
▸ submitLocalStateField(name
, value
): void
Submits an update to a specific field of the local presence state to the server for broadcasting.
Name | Type | Description |
---|---|---|
name |
string |
The name of the field to update. |
value |
unknown |
The new value of the field. |
void
▸ subscribe(): Promise
<void
>
Subscribes to presence updates from other clients.
Promise
<void
>
A promise that resolves when subscription is complete; access other states via "presence.otherStates".