Skip to content

Commit

Permalink
[FCE-890] Update documentation to use new useConnection hook (#54)
Browse files Browse the repository at this point in the history
## Description

Changed `usePeerStatus` and `useReconnection` to `useConnection`.
  • Loading branch information
maksg authored Nov 25, 2024
1 parent 363da62 commit 81a6a55
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
8 changes: 6 additions & 2 deletions docs/react-native/connecting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ follow our [server setup instructions](/production/server).
In order to connect, you just need to call `joinRoom` method with data from previous step:

```ts
import { joinRoom } from "@fishjam-cloud/react-native-client";
import { useConnection } from "@fishjam-cloud/react-native-client";

const { joinRoom } = useConnection();

await joinRoom(fishjamUrl, peerToken);
```
Expand All @@ -56,7 +58,9 @@ await joinRoom(fishjamUrl, peerToken);
In order to close connection, you have to call `leaveRoom` method.

```ts
import { leaveRoom } from "@fishjam-cloud/react-native-client";
import { useConnection } from "@fishjam-cloud/react-native-client";

const { leaveRoom } = useConnection();

await leaveRoom();
```
14 changes: 8 additions & 6 deletions docs/react-native/quick-setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function StartStreamingButton({
userName: string;
}) {
const { prepareCamera } = useCamera();
const { joinRoom } = useConnection();

const startStreaming = async () => {
const response = await fetch(
Expand All @@ -89,10 +90,10 @@ function StartStreamingButton({

### 3. Check if you are connected

Once you are connected, you can check connection status with `usePeerStatus` hook
Once you are connected, you can check connection status with `useConnection` hook

```ts
const { peerStatus } = usePeerStatus();
const { connectionStatus } = useConnection();
```

### 4. Show other peers
Expand Down Expand Up @@ -161,7 +162,7 @@ npx expo install expo-camera && npx expo prebuild
import { useCallback, useEffect } from "react";
import { Button, StyleSheet, View } from "react-native";
import {
joinRoom,
useConnection,
useCamera,
usePeers,
VideoRendererView,
Expand All @@ -171,7 +172,7 @@ import { useCameraPermissions } from "expo-camera";

function TracksView() {
const { peers } = usePeers();
const { peerStatus } = usePeerStatus();
const { connectionStatus } = useConnection();

const videoTracks = peers.flatMap((peer) =>
peer.tracks.filter((track) => track.type === "Video" && track.isActive),
Expand Down Expand Up @@ -199,6 +200,7 @@ function StartStreamingButton({
userName: string;
}) {
const { prepareCamera } = useCamera();
const { joinRoom } = useConnection();

const startStreaming = async () => {
const response = await fetch(
Expand All @@ -216,7 +218,7 @@ function StartStreamingButton({

export default function HomeScreen() {
const [permission, requestPermission] = useCameraPermissions();
const { peerStatus } = usePeerStatus();
const { connectionStatus } = useConnection();

useEffect(() => {
requestPermission();
Expand All @@ -228,7 +230,7 @@ export default function HomeScreen() {

return (
<SafeAreaView style={styles.container}>
{peerStatus !== "connected" && (
{connectionStatus !== "connected" && (
<StartStreamingButton roomName="*roomName*" userName="*username*" />
)}
<TracksView />
Expand Down
20 changes: 10 additions & 10 deletions docs/react-native/reconnect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ sidebar_position: 7

# Reconnect

If your connection is lost while you are connected to a room, the app will automatically handle the reconnection process. You can monitor these events by utilizing the `useReconnection` hook.
If your connection is lost while you are connected to a room, the app will automatically handle the reconnection process. You can monitor these events by utilizing the `useConnection` hook.

### Example hook that logs the current status to the console:

```ts
import { useEffect, useRef } from "react";
import {
ReconnectionStatus,
useReconnection,
ConnectionStatus,
useConnection,
} from "@fishjam-cloud/react-native-client";

function useLogReconnectionStatus() {
const prevStatus = useRef<ReconnectionStatus>("idle");
const { reconnectionStatus } = useReconnection();
function useLogConnectionStatus() {
const prevStatus = useRef<ConnectionStatus>("idle");
const { connectionStatus } = useConnection();

useEffect(() => {
if (prevStatus.current === reconnectionStatus) return;
prevStatus.current = reconnectionStatus;
if (prevStatus.current === connectionStatus) return;
prevStatus.current = connectionStatus;

switch (reconnectionStatus) {
switch (connectionStatus) {
case "error":
return console.log("Failed to reconnect");
case "reconnecting":
return console.log("Connection is broken, reconnecting...");
default:
return console.log("Connected succesfully");
}
}, [reconnectionStatus]);
}, [connectionStatus]);
}
```

0 comments on commit 81a6a55

Please sign in to comment.