|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +import Tabs from "@theme/Tabs"; |
| 6 | +import TabItem from "@theme/TabItem"; |
| 7 | + |
| 8 | +# Connecting |
| 9 | + |
| 10 | +This article will guide you through the process of connecting to a Fishjam room. |
| 11 | + |
| 12 | +## Getting URL and token |
| 13 | + |
| 14 | +In order to connect, you need to obtain a **Peer Token** (the token that will authenticate the peer in |
| 15 | +your Room). |
| 16 | + |
| 17 | +<Tabs groupId="app-type"> |
| 18 | + |
| 19 | + <TabItem value="sandbox" label="Sandbox App"> |
| 20 | + |
| 21 | +Once you create your account on [Fishjam](https://fishjam.io), you will have access to the Sandbox environment as part of the Mini Jar plan. |
| 22 | +While using the Sandbox environment, [you can use the Sandbox API](../features/sandbox-api-testing) to generate peer tokens for testing or development purposes. |
| 23 | +This is basically a service that will create a Room, add your app as |
| 24 | +the Room's Peer, and return the token required to use that Room. |
| 25 | + |
| 26 | +<Tabs groupId="platform"> |
| 27 | + <TabItem value="web" label="React (Web)"> |
| 28 | + |
| 29 | +```ts |
| 30 | +import { useSandbox } from "@fishjam-cloud/react-client"; |
| 31 | +const roomName = "room"; |
| 32 | +const peerName = "user"; |
| 33 | +// ---cut--- |
| 34 | + |
| 35 | +// The `useSandbox` hook gets the fishjamId from FishjamProvider |
| 36 | +// It will work ONLY with the FISHJAM_ID of the Sandbox environment |
| 37 | +const { getSandboxPeerToken } = useSandbox(); |
| 38 | +const peerToken = await getSandboxPeerToken(roomName, peerName); |
| 39 | +``` |
| 40 | + |
| 41 | + </TabItem> |
| 42 | + <TabItem value="mobile" label="React Native (Mobile)"> |
| 43 | + |
| 44 | +```ts |
| 45 | +import { useSandbox } from "@fishjam-cloud/mobile-client"; |
| 46 | +const roomName = "room"; |
| 47 | +const peerName = "user"; |
| 48 | +// ---cut--- |
| 49 | + |
| 50 | +// The `useSandbox` hook gets the fishjamId from FishjamProvider |
| 51 | +// It will work ONLY with the FISHJAM_ID of the Sandbox environment |
| 52 | +const { getSandboxPeerToken } = useSandbox(); |
| 53 | +const peerToken = await getSandboxPeerToken(roomName, peerName); |
| 54 | +``` |
| 55 | + |
| 56 | + </TabItem> |
| 57 | +</Tabs> |
| 58 | + |
| 59 | + </TabItem> |
| 60 | + <TabItem value="production" label="Production App"> |
| 61 | + |
| 62 | +For the production app, you need to implement your own backend service that will provide the user with a **Peer Token**. To do that, |
| 63 | +follow our [server setup instructions](../backend/server-setup). |
| 64 | + |
| 65 | + </TabItem> |
| 66 | +</Tabs> |
| 67 | + |
| 68 | +## Connecting |
| 69 | + |
| 70 | +Use the [`useConnection`](../../api/web/functions/useConnection) hook to get |
| 71 | +the [`joinRoom`](../../api/web/functions/useConnection#joinroom) function. |
| 72 | + |
| 73 | +<Tabs groupId="platform"> |
| 74 | + <TabItem value="web" label="React (Web)"> |
| 75 | + |
| 76 | +```tsx |
| 77 | +const PEER_TOKEN = "some-peer-token"; |
| 78 | +// ---cut-before--- |
| 79 | +import { useConnection, useSandbox } from "@fishjam-cloud/react-client"; |
| 80 | +import React, { useCallback } from "react"; |
| 81 | + |
| 82 | +export function JoinRoomButton() { |
| 83 | + const { joinRoom } = useConnection(); // [!code highlight] |
| 84 | + // get the peer token from sandbox or your backend |
| 85 | + const { getSandboxPeerToken } = useSandbox(); |
| 86 | + |
| 87 | + const onJoinRoomPress = useCallback(async () => { |
| 88 | + // [!code highlight:5] |
| 89 | + const peerToken = await getSandboxPeerToken("Room", "User"); |
| 90 | + await joinRoom({ peerToken }); |
| 91 | + }, [joinRoom]); |
| 92 | + |
| 93 | + return <button onClick={onJoinRoomPress}>Join room</button>; |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | + </TabItem> |
| 98 | + <TabItem value="mobile" label="React Native (Mobile)"> |
| 99 | + |
| 100 | +```tsx |
| 101 | +import React, { useCallback } from "react"; |
| 102 | +import { Button } from "react-native"; |
| 103 | +import { useConnection, useSandbox } from "@fishjam-cloud/mobile-client"; |
| 104 | + |
| 105 | +export function JoinRoomButton() { |
| 106 | + const { joinRoom } = useConnection(); // [!code highlight] |
| 107 | + // fishjamId is provided through FishjamProvider |
| 108 | + const { getSandboxPeerToken } = useSandbox(); |
| 109 | + |
| 110 | + const onPressJoin = useCallback(async () => { |
| 111 | + // in production environment, get the peerToken from your backend |
| 112 | + const peerToken = await getSandboxPeerToken("Room", "User"); |
| 113 | + |
| 114 | + await joinRoom({ peerToken }); // [!code highlight] |
| 115 | + }, [joinRoom, getSandboxPeerToken]); |
| 116 | + |
| 117 | + return <Button onPress={onPressJoin} title="Join Room" />; |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | + </TabItem> |
| 122 | +</Tabs> |
| 123 | + |
| 124 | +## Disconnecting |
| 125 | + |
| 126 | +In order to close connection, use the [`leaveRoom`](../../api/web/functions/useConnection#leaveroom) method |
| 127 | +from [`useConnection`](../../api/web/functions/useConnection) hook. |
| 128 | + |
| 129 | +<Tabs groupId="platform"> |
| 130 | + <TabItem value="web" label="React (Web)"> |
| 131 | + |
| 132 | +```tsx |
| 133 | +import { useConnection } from "@fishjam-cloud/react-client"; |
| 134 | +import React, { useCallback } from "react"; |
| 135 | + |
| 136 | +export function LeaveRoomButton() { |
| 137 | + const { leaveRoom } = useConnection(); // [!code highlight] |
| 138 | + |
| 139 | + return <button onClick={leaveRoom}>Leave room</button>; |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | + </TabItem> |
| 144 | + <TabItem value="mobile" label="React Native (Mobile)"> |
| 145 | + |
| 146 | +```tsx |
| 147 | +import React, { useCallback } from "react"; |
| 148 | +import { Button } from "react-native"; |
| 149 | +import { useConnection } from "@fishjam-cloud/mobile-client"; |
| 150 | + |
| 151 | +export function LeaveRoomButton() { |
| 152 | + const { leaveRoom } = useConnection(); // [!code highlight] |
| 153 | + |
| 154 | + const onPressLeave = useCallback(async () => { |
| 155 | + await leaveRoom(); // [!code highlight] |
| 156 | + }, [leaveRoom]); |
| 157 | + |
| 158 | + return <Button onPress={onPressLeave} title="Leave Room" />; |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | + </TabItem> |
| 163 | +</Tabs> |
| 164 | + |
| 165 | +## Next Steps |
| 166 | + |
| 167 | +Now that you're connected to a room, you can explore additional features: |
| 168 | + |
| 169 | +- [Start Streaming](./start-streaming) - Enable your camera and microphone |
| 170 | +- [List Other Peers](./list-other-peers) - Display video from other participants |
| 171 | +- [Picture in Picture](./picture-in-picture) - Allow users to watch video in a floating window (Mobile) |
| 172 | +- [Background Streaming](./background-streaming) - Keep calls active when the app is backgrounded (Mobile) |
0 commit comments