# React Native: phone apps (calling side)

`@huelake/connectivitizer-react-native` connects a React Native app (Expo or bare) to a service on a device: an ESP32, a PC, a server. It brings the native parts peer-to-peer connections need (UDP, the phone's own addresses on Wi-Fi and mobile data, IPv6), so the app passes nothing but its address, its key and the shared key.

## Install

```bash
npm install @huelake/connectivitizer @huelake/connectivitizer-react-native
npx expo install expo-crypto
```

The package is an Expo module with native code. A bare React Native app needs Expo modules set up first (`npx install-expo-modules@latest`). The app runs as a **development build** (`npx expo run:android`, `npx expo run:ios`, or an EAS build), not in Expo Go. In Expo Go it still works, but every session goes through the relay (still end-to-end encrypted). `peerToPeerAvailable()` tells your app which case applies. The native code is there for Android and iOS. On iOS, give the app a Local Network text (`ios.infoPlist.NSLocalNetworkUsageDescription` in `app.json`): without it iOS blocks the direct path to a device in the same Wi-Fi.

The linking example below also uses `expo-secure-store` and `expo-web-browser`: `npx expo install expo-secure-store expo-web-browser`.

## What you need

- A key for the app. If other people use the app, it is **linked** (below) and nobody types in a key. If only you use it, an **API key** (`czk_…`; web app: *Credentials → Create an API key*) will do for a first try.
- The **shared key** (`czs_…`): the same key the device has. If the device has none yet, generate one (web app: *Credentials → Generate a shared key*) and put it on both. In the app, put it into the app's configuration (for example a git-ignored config file bundled with the app), not into committed code.
- The **device name** and the **service name** to connect to, as the device registered them (web app: *Devices*).

## Connect

```ts
import { createClient } from '@huelake/connectivitizer-react-native';

const client = createClient({
  auth: { apiKey },             // an API key, or the one linkApp returned (below)
  sharedKey,                    // the same key the device has
});

const session = await client.connect('living-room-lamp', ['api']);
const state = await (await session.fetch('api', '/state')).json();

session.info.mode;                // 'direct' or 'e2e-relay'
session.info.endToEndEncrypted;   // established by this endpoint
session.info.directFailureReason; // why not direct, for a relayed session
```

`session.webSocket('api', '/live')` opens a WebSocket to the device's service; `session.openStream('api')` gives the raw byte stream. Errors are `ConnectivitizerError` with a stable `code`: see [Error codes](https://connectivitizer.com/guides/errors.md).

## Link the app instead of typing in a key

```ts
import * as SecureStore from 'expo-secure-store';
import * as WebBrowser from 'expo-web-browser';
import { linkApp } from '@huelake/connectivitizer-react-native';

const linked = await linkApp({
  client: 'Lamp app',                                         // the name the app appears under
  onPrompt: ({ url }) => WebBrowser.openBrowserAsync(url),    // the person signs in on https://connectivitizer.com and approves
});
await SecureStore.setItemAsync('cz_api_key', linked.apiKey); // returned this once
```

Nothing has to be registered for the app, and no redirect back into the app is involved. `linkApp` rejects with `LINK_DENIED` or `LINK_EXPIRED` (after ten minutes). The linked app is listed, and revoked, under *Credentials* in the web app.

## Showing how the app is connected

If your app shows how a session runs, take it from `session.info`, which this endpoint established itself: *Peer to peer* only when `mode` is `direct`, *Through the relay* otherwise (with `directFailureReason` for why), and *end-to-end encrypted* only when `endToEndEncrypted` is true.

## Good to know

- You do not need UDP modules or code that reads the phone's IP addresses: the package does that.
- Sessions from the Android emulator to a device in your network are relayed (the emulator has a NAT of its own). Test peer to peer on a real phone. More in [Troubleshooting](https://connectivitizer.com/guides/troubleshooting.md).
- A device that uses only certificates (a `cz` device without a shared key) cannot be reached from a phone app; add the shared key on that device (`cz key add default czs_…`).
