Skip to content

Commit

Permalink
React-native client info (#629)
Browse files Browse the repository at this point in the history
* react-native client info

* Changeset

* prettier

* Use navigator.product for detecting RN

* Use global to pass along platform info to avoid using require

* prettier
  • Loading branch information
davidliu authored Mar 27, 2023
1 parent 38f03bc commit cb380c2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-boats-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

Identify react-native apps when connecting to server
4 changes: 2 additions & 2 deletions src/api/SignalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from '../proto/livekit_rtc';
import { ConnectionError, ConnectionErrorReason } from '../room/errors';
import CriticalTimers from '../room/timers';
import { getClientInfo, Mutex, sleep } from '../room/utils';
import { getClientInfo, isReactNative, Mutex, sleep } from '../room/utils';

// internal options
interface ConnectOpts {
Expand Down Expand Up @@ -706,7 +706,7 @@ function createConnectionParams(token: string, info: ClientInfo, opts: ConnectOp
params.set('auto_subscribe', opts.autoSubscribe ? '1' : '0');

// ClientInfo
params.set('sdk', 'js');
params.set('sdk', isReactNative() ? 'reactnative' : 'js');
params.set('version', info.version!);
params.set('protocol', info.protocol!.toString());
if (info.deviceModel) {
Expand Down
24 changes: 24 additions & 0 deletions src/room/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ export function isWeb(): boolean {
return typeof document !== 'undefined';
}

export function isReactNative(): boolean {
// navigator.product is deprecated on browsers, but will be set appropriately for react-native.
return navigator.product == 'ReactNative';
}

export function getReactNativeOs(): string | undefined {
if (!isReactNative()) {
return undefined;
}

// global defined only for ReactNative.
// @ts-ignore
if (global && global.LiveKitReactNativeGlobal) {
// @ts-ignore
return global.LiveKitReactNativeGlobal.platform;
}

return undefined;
}

export function compareVersions(v1: string, v2: string): number {
const parts1 = v1.split('.');
const parts2 = v2.split('.');
Expand Down Expand Up @@ -174,6 +194,10 @@ export function getClientInfo(): ClientInfo {
protocol: protocolVersion,
version,
});

if (isReactNative()) {
info.os = getReactNativeOs() ?? '';
}
return info;
}

Expand Down

0 comments on commit cb380c2

Please sign in to comment.