# Presence

## Content

# Class: Presence<P\>

Manages presence information for real-time collaboration.

**`example`**
```javascript
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

- [constructor](Presence#constructor)

### Accessors

- [connection](Presence#connection)
- [id](Presence#id)
- [localState](Presence#localstate)
- [otherStates](Presence#otherstates)

### Methods

- [destroy](Presence#destroy)
- [off](Presence#off)
- [on](Presence#on)
- [once](Presence#once)
- [removeLocalState](Presence#removelocalstate)
- [submitLocalState](Presence#submitlocalstate)
- [submitLocalStateField](Presence#submitlocalstatefield)
- [subscribe](Presence#subscribe)

## Constructors

### <a id="constructor" name="constructor"></a> constructor

• **new Presence**<`P`\>(`connection`)

#### Type parameters

| Name |
| :------ |
| `P` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `connection` | `Connection` |

## Accessors

### <a id="connection" name="connection"></a> connection

• `get` **connection**(): `Connection`

Retrieves the associated connection object.

#### Returns

`Connection`

The connection instance.

___

### <a id="id" name="id"></a> id

• `get` **id**(): `string`

Retrieves the unique identifier of the presence instance.

#### Returns

`string`

The presence ID.

___

### <a id="localstate" name="localstate"></a> localState

• `get` **localState**(): `undefined` \| `P`

Retrieves the local presence state.

#### Returns

`undefined` \| `P`

The local presence data, or undefined if not set.

___

### <a id="otherstates" name="otherstates"></a> otherStates

• `get` **otherStates**(): [`IPresences`](../interfaces/IPresences)<`P`\>

Retrieves the presence states of other clients.

#### Returns

[`IPresences`](../interfaces/IPresences)<`P`\>

The collection of other clients' presence data.

## Methods

### <a id="destroy" name="destroy"></a> destroy

▸ **destroy**(): `void`

Destroys the presence instance and cleans up resources.

#### Returns

`void`

___

### <a id="off" name="off"></a> off

▸ **off**<`NAME`\>(`name`, `f`): `void`

Removes a listener for a specific event.

#### Type parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `NAME` | extends keyof [`IPresenceEvents`](../interfaces/IPresenceEvents)<`P`\> | The type of the event name, extending keyof IPresenceEvents<P>. |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `NAME` | The name of the event. |
| `f` | [`IPresenceEvents`](../interfaces/IPresenceEvents)<`P`\>[`NAME`] | The event handler function to remove. |

#### Returns

`void`

___

### <a id="on" name="on"></a> on

▸ **on**<`NAME`\>(`name`, `f`): [`IPresenceEvents`](../interfaces/IPresenceEvents)<`P`\>[`NAME`]

Registers a listener for a specific event.

#### Type parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `NAME` | extends keyof [`IPresenceEvents`](../interfaces/IPresenceEvents)<`P`\> | The type of the event name, extending keyof IPresenceEvents<P>. |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `NAME` | The name of the event. |
| `f` | [`IPresenceEvents`](../interfaces/IPresenceEvents)<`P`\>[`NAME`] | The event handler function. |

#### Returns

[`IPresenceEvents`](../interfaces/IPresenceEvents)<`P`\>[`NAME`]

The registered event handler.

___

### <a id="once" name="once"></a> once

▸ **once**<`NAME`\>(`name`, `f`): `void`

Registers a one-time listener for a specific event.

#### Type parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `NAME` | extends keyof [`IPresenceEvents`](../interfaces/IPresenceEvents)<`P`\> | The type of the event name, extending keyof IPresenceEvents<P>. |

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `name` | `NAME` | The name of the event. |
| `f` | [`IPresenceEvents`](../interfaces/IPresenceEvents)<`P`\>[`NAME`] | The event handler function to call once. |

#### Returns

`void`

___

### <a id="removelocalstate" name="removelocalstate"></a> removeLocalState

▸ **removeLocalState**(): `void`

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

#### Returns

`void`

___

### <a id="submitlocalstate" name="submitlocalstate"></a> 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`

___

### <a id="submitlocalstatefield" name="submitlocalstatefield"></a> 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`

___

### <a id="subscribe" name="subscribe"></a> 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".
