# StreamPixelVoiceChat

Voice and text chat powered by LiveKit.

## Constructor

{% code lineNumbers="true" %}

```javascript
import { StreamPixelVoiceChat } from 'streampixelsdk';

const voiceChat = new StreamPixelVoiceChat(roomName, userName, voiceChat, avatar, micStart);
```

{% endcode %}

| Parameter   | Type      | Description                                                   |
| ----------- | --------- | ------------------------------------------------------------- |
| `roomName`  | `string`  | Room identifier. Users in the same room can communicate.      |
| `userName`  | `string`  | Display name for this user                                    |
| `voiceChat` | `boolean` | `true` for voice chat, `false` for text-only                  |
| `avatar`    | `string`  | URL to user's avatar image                                    |
| `micStart`  | `boolean` | Start with microphone enabled (only if `voiceChat` is `true`) |

## Methods

### `async join()`

Join the voice chat room. Fetches a token, connects to the LiveKit server, subscribes to audio, and enables the mic if configured.

```javascript
await voiceChat.join();
```

### `async leave()`

Disconnect from the room.

```javascript
await voiceChat.leave();
```

### `async toggleMic()`

Toggle the local microphone on or off.

```javascript
await voiceChat.toggleMic();
```

### `sendMessage(text)`

Send a text message to all participants in the room.

| Parameter | Type     | Description     |
| --------- | -------- | --------------- |
| `text`    | `string` | Message content |

```javascript
voiceChat.sendMessage('Hello!');
```

### `async muteAllRemote()`

Mute all remote participants locally (you stop hearing them).

```javascript
await voiceChat.muteAllRemote();
```

### `async unmuteAllRemote()`

Unmute all remote participants locally.

```javascript
await voiceChat.unmuteAllRemote();
```

### `async muteSelected(identity)`

Mute a specific participant locally.

| Parameter  | Type     | Description                |
| ---------- | -------- | -------------------------- |
| `identity` | `string` | The participant's identity |

```javascript
await voiceChat.muteSelected('user123');
```

### `async unmuteSelected(identity)`

Unmute a specific participant locally.

| Parameter  | Type     | Description                |
| ---------- | -------- | -------------------------- |
| `identity` | `string` | The participant's identity |

```javascript
await voiceChat.unmuteSelected('user123');
```

{% hint style="warning" %}
All mute/unmute operations (`muteAllRemote`, `unmuteAllRemote`, `muteSelected`, `unmuteSelected`) are local only. They control what you hear, not what other participants hear. Other users are not affected.
{% endhint %}

### `onMessage(callback)`

Register a callback for incoming messages (including your own).

```javascript
voiceChat.onMessage((msg) => {
  console.log(msg.from, msg.text, msg.avatar);
});
```

**Callback data:**

| Property | Type     | Description                                    |
| -------- | -------- | ---------------------------------------------- |
| `from`   | `string` | Sender's identity (`"You"` for local messages) |
| `text`   | `string` | Message content                                |
| `avatar` | `string` | Sender's avatar URL                            |

### `onParticipantUpdate(callback)`

Register a callback for participant list changes and speaking state updates.

```javascript
voiceChat.onParticipantUpdate((data) => {
  console.log(data.localParticipant);
  console.log(data.remoteParticipants);
});
```

**Callback data:**

```javascript
{
  localParticipant: {
    id: string,       // User identity
    avatar: string,   // Avatar URL
    speaking: boolean // Currently speaking
  },
  remoteParticipants: [
    {
      id: string,
      avatar: string,
      speaking: boolean
    }
  ]
}
```
