|
| 1 | +--- |
| 2 | +sidebar_position: 9 |
| 3 | +title: "Broadcasting" |
| 4 | +description: "How to use Fishjam for broadcasting" |
| 5 | +--- |
| 6 | + |
| 7 | +import Tabs from "@theme/Tabs"; |
| 8 | +import TabItem from "@theme/TabItem"; |
| 9 | +import ConfigurePermissions from "./_components/configure-permissions.mdx"; |
| 10 | + |
| 11 | +# Broadcasting |
| 12 | + |
| 13 | +:::info |
| 14 | + |
| 15 | +This guide assumes you already finished either the [Quick Setup](/react-native/quick-setup) or [Installation](/react-native/installation) guide. |
| 16 | + |
| 17 | +::: |
| 18 | + |
| 19 | +You can also use Fishjam to broadcast your content to users. |
| 20 | + |
| 21 | +In order to do this you need to create two flows. |
| 22 | + |
| 23 | +1. The first flow will be used to broadcast the content. |
| 24 | +2. The second flow will be used to receive the content. |
| 25 | + |
| 26 | +## Broadcasting |
| 27 | + |
| 28 | +<details> |
| 29 | +<summary>Configure permissions before broadcasting</summary> |
| 30 | + |
| 31 | +<ConfigurePermissions /> |
| 32 | + |
| 33 | +</details> |
| 34 | + |
| 35 | +Here is an example of a very simple broadcaster that immediately broadcasts the front camera to all users in a room: |
| 36 | + |
| 37 | +```tsx |
| 38 | +import { useEffect } from "react"; |
| 39 | +import { |
| 40 | + useCamera, |
| 41 | + useConnection, |
| 42 | + VideoPreviewView, |
| 43 | +} from "@fishjam-cloud/react-native-client"; |
| 44 | + |
| 45 | +export function Broadcaster() { |
| 46 | + const { prepareCamera } = useCamera(); |
| 47 | + const { joinRoom, leaveRoom } = useConnection(); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + const startBroadcasting = async () => { |
| 51 | + console.log("Getting room details..."); |
| 52 | + const { url, peerToken } = await getRoomDetails( |
| 53 | + "RoomName", |
| 54 | + "BroadcastingUserName", |
| 55 | + ); |
| 56 | + |
| 57 | + console.log("Preparing camera..."); |
| 58 | + await prepareCamera({ cameraEnabled: true }); |
| 59 | + |
| 60 | + await joinRoom({ url, peerToken }); |
| 61 | + console.log("Broadcasting..."); |
| 62 | + }; |
| 63 | + |
| 64 | + startBroadcasting(); |
| 65 | + return () => { |
| 66 | + leaveRoom(); |
| 67 | + }; |
| 68 | + }, [prepareCamera, joinRoom, leaveRoom]); |
| 69 | + |
| 70 | + return ( |
| 71 | + <VideoPreviewView |
| 72 | + style={{ width: "90%", aspectRatio: 1, alignSelf: "center" }} |
| 73 | + /> |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +async function getRoomDetails(roomName: string, peerName: string) { |
| 78 | + // This will work ONLY for the Sandbox App |
| 79 | + const response = await fetch( |
| 80 | + `https://fishjam.io/api/v1/connect/${YOUR_APP_ID}/room-manager/?roomName=${roomName}&peerName=${peerName}`, |
| 81 | + ); |
| 82 | + return response.json(); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Receiving |
| 87 | + |
| 88 | +Here is an example of a very simple receiver that receives the content from the broadcaster: |
| 89 | + |
| 90 | +```tsx |
| 91 | +import { useEffect } from "react"; |
| 92 | +import { View } from "react-native"; |
| 93 | +import { |
| 94 | + useConnection, |
| 95 | + usePeers, |
| 96 | + VideoRendererView, |
| 97 | +} from "@fishjam-cloud/react-native-client"; |
| 98 | + |
| 99 | +export function Receiver() { |
| 100 | + const { joinRoom, leaveRoom } = useConnection(); |
| 101 | + const { remotePeers } = usePeers(); |
| 102 | + |
| 103 | + const broadcastedTrack = remotePeers.at(0)?.cameraTrack; |
| 104 | + |
| 105 | + useEffect(() => { |
| 106 | + const startReceiving = async () => { |
| 107 | + console.log("Getting room details..."); |
| 108 | + const { url, peerToken } = await getRoomDetails( |
| 109 | + "RoomName", |
| 110 | + "ReceivingUserName", |
| 111 | + ); |
| 112 | + await joinRoom({ url, peerToken }); |
| 113 | + console.log("Receiving..."); |
| 114 | + }; |
| 115 | + |
| 116 | + startReceiving(); |
| 117 | + |
| 118 | + return () => { |
| 119 | + leaveRoom(); |
| 120 | + }; |
| 121 | + }, [joinRoom, leaveRoom]); |
| 122 | + |
| 123 | + if (!broadcastedTrack) { |
| 124 | + return null; |
| 125 | + } |
| 126 | + |
| 127 | + return ( |
| 128 | + <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}> |
| 129 | + <VideoRendererView |
| 130 | + key={broadcastedTrack.id} |
| 131 | + trackId={broadcastedTrack.id} |
| 132 | + videoLayout="FIT" |
| 133 | + style={{ width: "90%", aspectRatio: 1, backgroundColor: "black" }} |
| 134 | + /> |
| 135 | + </View> |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +async function getRoomDetails(roomName: string, peerName: string) { |
| 140 | + // This will work ONLY for the Sandbox App |
| 141 | + const response = await fetch( |
| 142 | + `https://fishjam.io/api/v1/connect/${YOUR_APP_ID}/room-manager/?roomName=${roomName}&peerName=${peerName}`, |
| 143 | + ); |
| 144 | + return response.json(); |
| 145 | +} |
| 146 | +``` |
0 commit comments