xonecas.io
4828
Sr

stream-together / commit add32be

Adds AgoraIO SDK and basic host usage

Added two new components to wrap AgoraIO provider and video to avoid
errors when trying to server side render.  These are now imported only
on the client.
Added basic start broadcast flow for the host.
Added a ESLint plugin to auto sort imports

sean2024-06-27 add32be216baa336fe619d291111bef802809fc3 parent 20bb348

11 files changed

.eslintrc.json+10−1
@@ -1,3 +1,12 @@
11 {
2- "extends": "next/core-web-vitals"
2+ "extends": "next/core-web-vitals",
3+ "plugins": ["simple-import-sort"],
4+ "rules": {
5+ "simple-import-sort/imports": "error",
6+ "simple-import-sort/exports": "error"
7+ },
8+ "parserOptions": {
9+ "sourceType": "module",
10+ "ecmaVersion": "latest"
11+ }
312 }
app/[slug]/layout.tsx+9−0
@@ -0,0 +1,9 @@
1+import AppLayout from "@/components/app-layout";
2+
3+export default function StreamLayout({
4+ children,
5+}: Readonly<{
6+ children: React.ReactNode;
7+}>) {
8+ return <AppLayout>{children}</AppLayout>;
9+}
app/[slug]/page.tsx+36−0
@@ -0,0 +1,36 @@
1+"use client";
2+
3+import dynamic from "next/dynamic";
4+import { useLocalStorage } from "usehooks-ts";
5+
6+const AgoraProvider = dynamic(() => import("@/components/agora-provider"), {
7+ ssr: false,
8+});
9+
10+const AgoraHost = dynamic(() => import("@/components/agora-host-video"), {
11+ ssr: false,
12+});
13+
14+export default function StreamSetupPage({
15+ params,
16+}: {
17+ params: { slug: string };
18+}) {
19+ const [appId] = useLocalStorage("app-id", "");
20+ const [token] = useLocalStorage("token", "");
21+ const [title] = useLocalStorage("title", "");
22+
23+ return (
24+ <AgoraProvider>
25+ <div className="flex flex-col items-center gap-16">
26+ <h2 className="text-2xl text-rose-600">
27+ {title} ({params.slug})
28+ </h2>
29+
30+ <div>
31+ <AgoraHost config={{ appId, channel: title, token }} />
32+ </div>
33+ </div>
34+ </AgoraProvider>
35+ );
36+}
app/new/page.tsx+101−1
@@ -1,3 +1,103 @@
1+"use client";
2+
3+import Link from "next/link";
4+import { useRouter } from "next/navigation";
5+import { useCallback, useState } from "react";
6+import { useLocalStorage } from "usehooks-ts";
7+
8+import { pacifico } from "@/components/pacifico-font";
9+import PrimaryButton from "@/components/primary-button";
10+import slugify from "@/lib/slugify";
11+
112 export default function NewStreamPage() {
2- return <div>We need a title for the stream</div>;
13+ // TODO: Allow user to forget their stored details
14+ const [appId, setAppId] = useLocalStorage("app-id", "");
15+ const [token, setToken] = useLocalStorage("token", "");
16+ const [title, setTitle] = useLocalStorage("title", "");
17+ const [error, setError] = useState("");
18+ const router = useRouter();
19+
20+ const handleChange = useCallback(
21+ (e: React.ChangeEvent<HTMLInputElement>) => {
22+ const name: string = e.target.name;
23+
24+ if (name === "app-id") setAppId(e.target.value);
25+ if (name === "token") setToken(e.target.value);
26+ if (name === "title") setTitle(e.target.value);
27+
28+ // Clear the error if it exists after a new change
29+ if (error) setError("");
30+ },
31+ [error, setAppId, setToken, setTitle],
32+ );
33+
34+ const handleNextClick = useCallback(() => {
35+ if (!appId || !token || !title) setError("All fields are required.");
36+ else {
37+ const slug = slugify(title);
38+ router.push("/" + slug);
39+ }
40+ }, [appId, token, title, router]);
41+
42+ return (
43+ <div className="flex flex-col items-center gap-8">
44+ <h3
45+ className={`text-2xl font-bold p-16 text-rose-300 ${pacifico.className}`}
46+ >
47+ Please fill in the details bellow
48+ </h3>
49+ <div className="w-1/2 flex flex-col gap-4">
50+ <label>
51+ Tell us your App ID from AgoraIO (get one{" "}
52+ <Link
53+ className="text-rose-600 active:text-rose-300 visited:text-rose-800"
54+ target="_blank"
55+ href="https://console.agora.io/v2/project-management"
56+ >
57+ here
58+ </Link>
59+ )
60+ </label>
61+ <input
62+ className="text-stone-200 bg-stone-800 rounded-xl p-4 w-full placeholder:text-stone-700"
63+ name="app-id"
64+ type="text"
65+ value={appId}
66+ onChange={handleChange}
67+ placeholder="Your app id here..."
68+ />
69+ </div>
70+
71+ <div className="w-1/2 flex flex-col gap-4">
72+ <label>Tell us your AgoraIO token</label>
73+ <input
74+ className="text-stone-200 bg-stone-800 rounded-xl p-4 w-full placeholder:text-stone-700"
75+ name="token"
76+ type="text"
77+ value={token}
78+ onChange={handleChange}
79+ placeholder="Your token here..."
80+ />
81+ </div>
82+
83+ <div className="w-1/2 flex flex-col gap-4">
84+ <label>What is your steam title?</label>
85+ <input
86+ className="text-stone-200 bg-stone-800 rounded-xl p-4 w-full placeholder:text-stone-700"
87+ name="title"
88+ type="text"
89+ value={title}
90+ onChange={handleChange}
91+ placeholder="Your token here..."
92+ />
93+ </div>
94+
95+ <div className="w-1/2 flex flex-col gap-4">
96+ <button onClick={handleNextClick}>
97+ <PrimaryButton>Next</PrimaryButton>
98+ </button>
99+ {error && <p className="text-rose-600">{error}</p>}
100+ </div>
101+ </div>
102+ );
3103 }
components/agora-host-video.tsx+60−0
@@ -0,0 +1,60 @@
1+"use client";
2+
3+import {
4+ LocalUser,
5+ useJoin,
6+ useLocalCameraTrack,
7+ useLocalMicrophoneTrack,
8+ usePublish,
9+} from "agora-rtc-react";
10+import { useCallback, useState } from "react";
11+
12+import PrimaryButton from "./primary-button";
13+
14+export default function AgoraHost({
15+ config: { appId, channel, token },
16+}: {
17+ config: { appId: string; channel: string; token: string };
18+}) {
19+ const [callStarted, setCallStarted] = useState(false);
20+ const [videoStarted, setVideoStarted] = useState(false);
21+ const [muted, setMuted] = useState(false);
22+
23+ const { localMicrophoneTrack } = useLocalMicrophoneTrack(!muted);
24+ const { localCameraTrack } = useLocalCameraTrack(videoStarted);
25+
26+ const handleCall = useCallback(() => {
27+ setCallStarted(true);
28+ setVideoStarted(true);
29+ }, []);
30+
31+ useJoin(
32+ {
33+ appid: appId,
34+ channel: "stream-together",
35+ token,
36+ },
37+ callStarted,
38+ );
39+
40+ usePublish([localMicrophoneTrack, localCameraTrack]);
41+
42+ return (
43+ <div className="flex flex-col gap-8">
44+ <div style={{ width: 640, height: 480 }}>
45+ <LocalUser
46+ audioTrack={localMicrophoneTrack}
47+ videoTrack={localCameraTrack}
48+ cameraOn={videoStarted}
49+ micOn={!muted}
50+ >
51+ You
52+ </LocalUser>
53+ </div>
54+
55+ <button onClick={handleCall}>
56+ <PrimaryButton>Call</PrimaryButton>
57+ </button>
58+ </div>
59+ );
60+}
components/agora-provider.tsx+31−0
@@ -0,0 +1,31 @@
1+"use client";
2+
3+import { AgoraRTCProvider, IAgoraRTCClient } from "agora-rtc-react";
4+import { useEffect, useState } from "react";
5+
6+export default function AgoraProvider({
7+ children,
8+}: Readonly<{
9+ children: React.ReactNode;
10+}>) {
11+ const [client, setClient] = useState<IAgoraRTCClient | null>(null);
12+
13+ // Because of server side rendering, we need to initialize agoraIO only on the client
14+ useEffect(() => {
15+ const initializeAgoraSDK = async () => {
16+ const AgoraRTC = (await import("agora-rtc-react")).default;
17+ // TODO: I think I really need `mode: "live"` in the config
18+ const agoraClient = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });
19+
20+ setClient(agoraClient);
21+ };
22+
23+ initializeAgoraSDK();
24+ }, []);
25+
26+ return client ? (
27+ <AgoraRTCProvider client={client}>{children}</AgoraRTCProvider>
28+ ) : (
29+ <p>There was an error creating agoraio</p>
30+ );
31+}
components/logo-sm.tsx+1−1
@@ -14,7 +14,7 @@ export default function LogoSmall() {
1414 priority
1515 />
1616 <h2 className={`${pacifico.className} text-4xl text-stone-500`}>
17- Steam Together
17+ Stream Together
1818 </h2>
1919 </div>
2020 </Link>
components/primary-button.tsx+1−1
@@ -4,7 +4,7 @@ export default function PrimaryButton({
44 children: React.ReactNode;
55 }>) {
66 return (
7- <div className="text-rose-100 bg-rose-600 hover:bg-rose-900 border-b-8 active:border-b-0 active:border-t-8 border-t-rose-950 border-b-rose-950 font-bold text-2xl py-4 px-8 rounded-xl shadow-sm shadow-stone-800 cursor-pointer">
7+ <div className="text-rose-100 bg-rose-700 hover:bg-rose-900 border-b-8 active:border-b-0 active:border-t-8 border-t-rose-950 border-b-rose-950 font-bold text-2xl py-4 px-8 rounded-t-lg active:rounded-t-xl rounded-b-xl shadow-sm shadow-stone-800 cursor-pointer text-center">
88 {children}
99 </div>
1010 );
lib/slugify.ts+7−0
@@ -0,0 +1,7 @@
1+export default function slugify(str: string): string {
2+ return str
3+ .toLocaleLowerCase()
4+ .trim()
5+ .replace(/[^\w\s-]/g, "")
6+ .replace(/[\s_-]+/g, "-");
7+}
package-lock.json+34−1
@@ -12,7 +12,8 @@
1212 "agora-rtc-sdk-ng": "^4.21.0",
1313 "next": "14.2.4",
1414 "react": "^18",
15- "react-dom": "^18"
15+ "react-dom": "^18",
16+ "usehooks-ts": "^3.1.0"
1617 },
1718 "devDependencies": {
1819 "@types/node": "^20",
@@ -20,6 +21,7 @@
2021 "@types/react-dom": "^18",
2122 "eslint": "^8",
2223 "eslint-config-next": "14.2.4",
24+ "eslint-plugin-simple-import-sort": "^12.1.0",
2325 "postcss": "^8",
2426 "tailwindcss": "^3.4.1",
2527 "typescript": "^5"
@@ -1975,6 +1977,16 @@
19751977 "semver": "bin/semver.js"
19761978 }
19771979 },
1980+ "node_modules/eslint-plugin-simple-import-sort": {
1981+ "version": "12.1.0",
1982+ "resolved": "https://registry.npmjs.org/eslint-plugin-simple-import-sort/-/eslint-plugin-simple-import-sort-12.1.0.tgz",
1983+ "integrity": "sha512-Y2fqAfC11TcG/WP3TrI1Gi3p3nc8XJyEOJYHyEPEGI/UAgNx6akxxlX74p7SbAQdLcgASKhj8M0GKvH3vq/+ig==",
1984+ "dev": true,
1985+ "license": "MIT",
1986+ "peerDependencies": {
1987+ "eslint": ">=5.0.0"
1988+ }
1989+ },
19781990 "node_modules/eslint-scope": {
19791991 "version": "7.2.2",
19801992 "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-7.2.2.tgz",
@@ -3223,6 +3235,12 @@
32233235 "url": "https://github.com/sponsors/sindresorhus"
32243236 }
32253237 },
3238+ "node_modules/lodash.debounce": {
3239+ "version": "4.0.8",
3240+ "resolved": "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz",
3241+ "integrity": "sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==",
3242+ "license": "MIT"
3243+ },
32263244 "node_modules/lodash.merge": {
32273245 "version": "4.6.2",
32283246 "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
@@ -4921,6 +4939,21 @@
49214939 "punycode": "^2.1.0"
49224940 }
49234941 },
4942+ "node_modules/usehooks-ts": {
4943+ "version": "3.1.0",
4944+ "resolved": "https://registry.npmjs.org/usehooks-ts/-/usehooks-ts-3.1.0.tgz",
4945+ "integrity": "sha512-bBIa7yUyPhE1BCc0GmR96VU/15l/9gP1Ch5mYdLcFBaFGQsdmXkvjV0TtOqW1yUd6VjIwDunm+flSciCQXujiw==",
4946+ "license": "MIT",
4947+ "dependencies": {
4948+ "lodash.debounce": "^4.0.8"
4949+ },
4950+ "engines": {
4951+ "node": ">=16.15.0"
4952+ },
4953+ "peerDependencies": {
4954+ "react": "^16.8.0 || ^17 || ^18"
4955+ }
4956+ },
49244957 "node_modules/util-deprecate": {
49254958 "version": "1.0.2",
49264959 "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
package.json+3−1
@@ -13,7 +13,8 @@
1313 "agora-rtc-sdk-ng": "^4.21.0",
1414 "next": "14.2.4",
1515 "react": "^18",
16- "react-dom": "^18"
16+ "react-dom": "^18",
17+ "usehooks-ts": "^3.1.0"
1718 },
1819 "devDependencies": {
1920 "@types/node": "^20",
@@ -21,6 +22,7 @@
2122 "@types/react-dom": "^18",
2223 "eslint": "^8",
2324 "eslint-config-next": "14.2.4",
25+ "eslint-plugin-simple-import-sort": "^12.1.0",
2426 "postcss": "^8",
2527 "tailwindcss": "^3.4.1",
2628 "typescript": "^5"