# ODIN Connection Test Example

This example demonstrates how to connect to an ODIN room and use the SDK's event system and diagnostic methods.

## Features Demonstrated

- **Room Connection**: Connecting to an ODIN room using the factory pattern
- **Event Handling**: All major event types (Joined, PeerJoined, MediaStarted, etc.)
- **End-to-End Encryption**: Optional E2EE configuration with OdinCipher
- **Diagnostic Methods**: Connection stats, jitter stats, and peer encryption status

## Prerequisites

You need an ODIN access key. Get one for free at [4Players ODIN](https://docs.4players.io/voice/introduction/access-keys/).

## Configuration

Edit `index.js` and replace the placeholder values:

```javascript
const accessKey = "__YOUR_ACCESS_KEY__";  // Your ODIN access key
const roomId = "__YOUR_ROOM_ID__";        // Room to join
const userId = "TestUser-123";            // Your user ID
```

To enable E2EE, set a password:

```javascript
const cipherPassword = "my-secret-password";  // All peers must use the same password
```

## Running the Example

```bash
cd tests/connection-test
node index.js
```

## What This Example Does

1. **Initializes** the ODIN client
2. **Generates** a room token from your access key
3. **Configures** E2EE (if password is set)
4. **Sets up** event handlers for all room events
5. **Joins** the room and waits for 15 seconds
6. **Runs** diagnostic tests:
   - `getConnectionId()` - Connection identifier
   - `getConnectionStats()` - RTT, packet loss, bandwidth
   - `getJitterStats()` - Audio jitter buffer metrics
   - `getPeerStatus()` - E2EE peer verification
7. **Closes** the room and exits

## Expected Output

```
=== ODIN NodeJS SDK Connection Test ===

1. Initializing Odin Client...
2. Generating token from access key...
   Token generated successfully.

3. Setting up Cipher (E2EE)...
   E2EE disabled (no password configured).

4. Creating Room via client.createRoom()...
5. Setting up event handlers...

6. Joining room...
   Join initiated.

[Event] ConnectionStateChanged: Connecting
[Event] ConnectionStateChanged: Joined
[Event] Joined room!
   Room ID: my-room
   Own Peer ID: 12345
   Available Media IDs: [1, 2, 3, 4]

8. Testing diagnostic methods...

   8a. getConnectionId():
       Connection ID: 567890
       ✅ Connection ID retrieved successfully

   8b. getConnectionStats():
       ✅ Connection stats retrieved:
       - RTT:              25.50 ms
       - TX Bytes:         1234
       - RX Bytes:         5678
       ...

=== Test Complete ===
```

## Related Examples

- [audio-recording](../audio-recording/) - Record audio from peers
- [sending-audio](../sending-audio/) - Send audio to the room
