---
name: "connectDevice"
mode: "kit"
versionRequirements:
  - { name: "P2PKit", version: "0.0.1" }
  - { name: "@ray-js/ray", version: "1.3.15" }
platform:
  - "iOS"
  - "Android"
async: true
title: "p2p.connectDevice - Establish a P2P connection"
---

## connectDevice

> [VERSION] P2PKit >= 0.0.1 | @ray-js/ray >= 1.3.15

> [PLATFORM] iOS, Android

> ⚡ **Supports Promise** — Returns a Promise when success / fail / complete callbacks are omitted.

### Description

Connect via P2P

### Parameters

| Property | Type | Required | Default | Since | Description |
| --- | --- | --- | --- | --- | --- |
| `deviceId` | `string` | Yes | - | `0.0.1` | Device ID |
| `mode` | `number` | No | `0` | `0.0.1` | Connection mode, 0: INTERNET  1: LAN |
| `timeout` | `number` | No | `0` | `0.0.1` | Timeout (ms). Defaults: INTERNET 15000 ms, LAN 3000 ms |
| `complete` | `() => void` | No | - | - | Completion callback (executed on both success and failure) |
| `success` | `() => void` | No | - | - | Callback on successful API call |
| `fail` | `(params: Object) => void` ↓see below | No | - | - | Failure callback |

#### fail callback parameters

| Property | Type | Description |
| --- | --- | --- |
| `errorMsg` | `string` | Error message |
| `errorCode` | `string \| number` | Error code |
| `innerError` | `Object` | Error extension |

##### fail(params).innerError properties

| Property | Type | Description |
| --- | --- | --- |
| `errorCode` | `string \| number` | Error extension code |
| `errorMsg` | `string` | Error extension message |


### Examples

#### Demo

```tsx
import { p2p } from '@ray-js/ray'

p2p.connectDevice({
  deviceId: "xx",
  mode: 0,
  timeout: 0,
  success: data => {
    console.log(data);
  },
  fail: error => {
    console.error(error);
  },
});
```
