[]
        
(Showing Draft Content)

Presence

Class: Presence<P>

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);
    });
});

Type parameters

Name Description
P The type of presence data.

Table of contents

Constructors

Accessors

Methods

Constructors

constructor

new Presence<P>(connection)

Type parameters

Name
P

Parameters

Name Type
connection Connection

Accessors

connection

get connection(): Connection

Retrieves the associated connection object.

Returns

Connection

The connection instance.


id

get id(): string

Retrieves the unique identifier of the presence instance.

Returns

string

The presence ID.


localState

get localState(): undefined | P

Retrieves the local presence state.

Returns

undefined | P

The local presence data, or undefined if not set.


otherStates

get otherStates(): IPresences<P>

Retrieves the presence states of other clients.

Returns

IPresences<P>

The collection of other clients' presence data.

Methods

destroy

destroy(): void

Destroys the presence instance and cleans up resources.

Returns

void


off

off<NAME>(name, f): void

Removes a listener for a specific event.

Type parameters

Name Type Description
NAME extends keyof IPresenceEvents<P> The type of the event name, extending keyof IPresenceEvents

.

Parameters

Name Type Description
name NAME The name of the event.
f IPresenceEvents<P>[NAME] The event handler function to remove.

Returns

void


on

on<NAME>(name, f): IPresenceEvents<P>[NAME]

Registers a listener for a specific event.

Type parameters

Name Type Description
NAME extends keyof IPresenceEvents<P> The type of the event name, extending keyof IPresenceEvents

.

Parameters

Name Type Description
name NAME The name of the event.
f IPresenceEvents<P>[NAME] The event handler function.

Returns

IPresenceEvents<P>[NAME]

The registered event handler.


once

once<NAME>(name, f): void

Registers a one-time listener for a specific event.

Type parameters

Name Type Description
NAME extends keyof IPresenceEvents<P> The type of the event name, extending keyof IPresenceEvents

.

Parameters

Name Type Description
name NAME The name of the event.
f IPresenceEvents<P>[NAME] The event handler function to call once.

Returns

void


removeLocalState

removeLocalState(): void

Removes the local presence state from the server, notifying other clients.

Returns

void


submitLocalState

submitLocalState(p): void

Submits the local presence state to the server for broadcasting to other clients.

Parameters

Name Type Description
p P The local presence data to submit.

Returns

void


submitLocalStateField

submitLocalStateField(name, value): void

Submits an update to a specific field of the local presence state to the server for broadcasting.

Parameters

Name Type Description
name string The name of the field to update.
value unknown The new value of the field.

Returns

void


subscribe

subscribe(): Promise<void>

Subscribes to presence updates from other clients.

Returns

Promise<void>

A promise that resolves when subscription is complete; access other states via "presence.otherStates".