LogoLogo
WherebyStatusCommunity
  • 📹Whereby 101
    • Create Your Video Experience
      • Get started in 3 steps
      • Embedding Whereby in a web app
        • Using Whereby's Web Component & Pre-built UI
          • Script Tags
          • With Low Code
            • Embedding in Squarespace or Wordpress
            • No code video conferencing in Bubble
        • Using Whereby's Browser SDK with React Hooks for a fully custom UI
      • Embedding Whereby in a mobile app
        • Embedding Whereby in iOS
          • Using Whereby's Native iOS SDK
        • Embedding Whereby in Android
          • Using Whereby's Native SDK
        • Using Flutter
        • Using React Native
      • Github SDK Examples
      • Meeting scheduling with Cronofy
    • Generating Room URLs
      • Name prefixes
      • Using “Create a room”
      • Using Postman
    • Customize Your Video Experience
      • During room creation
      • Using Attributes/URL Params
      • Global preferences
      • Branding elements
      • Dial-In
      • File sharing
      • Breakout Groups with Embedded
      • Waiting Rooms
    • User roles & Meeting Permissions
    • FAQ
      • Accessibility
      • Whereby Words
      • Firewall & Security
      • HIPAA compliant setup
      • Allowed Domains & Localhost
      • Whereby Embedded Feature Comparison
  • 🔍Meeting Content & Quality
    • Recording
      • Cloud Recording
      • Local Recording
    • Transcribing
      • Session Transcription
      • Recording Transcription
    • Live Captions
    • Session summaries
    • Live streaming RTMP
    • Quality Insights
      • Real-time troubleshooting
      • Using the Insights dashboard
      • Improving call quality
      • Tracking room events with Webhooks
  • 🤷End User
    • End User Support Guides
      • Supported Browsers & Devices
      • Screen Sharing Setup & Usage
      • Using Breakout Groups
      • Troubleshooting & Basics
  • 🚚Developer Guides
    • Quickly deploy Whereby to your domain
    • Tracking Customer Usage
    • Migrating from Twilio
      • Twilio JS SDK Quick Migration
      • Twilio JS SDK Direct Migration
  • 🖥️Reference
    • REST API Reference
      • /meetings
      • /insights
      • /recordings
      • /transcriptions
      • /summaries
      • /rooms
    • Web Component Reference
    • React Hooks Reference
      • Quick Start
        • Getting started with the Browser SDK
      • Guides & Tutorials
        • Migrate from version 2.x to 3
        • Grid logic
        • Custom Video Tiles with React
        • Usage with Next.js
        • How to customize the toolbar
      • API Reference
        • WherebyProvider
        • VideoView
        • VideoGrid
        • useLocalMedia
        • useRoomConnection
      • Types
    • React Native Reference
      • Quick Start
      • WherebyEmbed
    • Webhooks Reference
Powered by GitBook
On this page
  • Provider
  • The room component
  • Using the room component

Was this helpful?

Edit on GitHub
  1. Reference
  2. React Hooks Reference
  3. Guides & Tutorials

Usage with Next.js

Last updated 6 months ago

Was this helpful?

Next.js is a popular framework for React and is rendered server-side. The Whereby browser-sdk relies on browser specific API’s to work and because of this, usage with Next.js has some challenges.

The default way of building Next.js app currently uses an . Conceptually, the in-room functionality of a Whereby call happens in a single route (there should be no reloads in the middle of a call). This means:

  1. We need to dedicate a single page route for the call

  2. Make sure the page is running on the client, in order have access to the necessary browser API’s.

In Next.js, every component is a server component by default. You need to opt-out to make use of browser specific API’s. This can be done by adding “use client” directive at the top of a file, above the imports.

Read more about client components in the Next.js documentation . Unfortunately, this is not enough for our use case. Client components in Next.js are still pre-rendered by default, and we need to opt-out of that as well, to be able to access all the browser API’s we need. We can do this with next/dynamic. See more info .

Provider

We need a “client” component that will setup our connection to Whereby for us, and provide us with the global Whereby state.

components/provider.tsx

"use client";

import * as React from "react";

import { WherebyProvider } from "@whereby.com/browser-sdk/react";

function Provider({ children }: { children: React.ReactNode }) {
    return (
        <WherebyProvider>
            <>{children}</>
        </WherebyProvider>
    );
}

export default Provider;

The next step is to include this provider anywhere in the app, but it needs to be above (a parent of), any component that will utilize the Whereby SDK. The root level layout.tsx is a natural place to put it, and that gives all pages and components access to it. We need to import it using Next.js dynamic import, like this:

app/layout.tsx

const Provider = dynamic(() => import("../components/provider"), { ssr: false });

The room component

We can now create a room component, this is a minimal example:

components/room.tsx

"use client";

import * as React from "react";

import { useRoomConnection, VideoGrid } from "@whereby.com/browser-sdk/react";

interface Props {
    roomUrl: string;
}

function Room({ roomUrl }: Props) {
    const { actions } = useRoomConnection(roomUrl, { localMediaOptions: { audio: true, video: true } });
    const { joinRoom, leaveRoom } = actions;

    React.useEffect(() => {
        joinRoom();
        return () => leaveRoom();
    }, [joinRoom, leaveRoom]);

    return (
        <div className={"w-full h-full"}>
            <VideoGrid />
        </div>
    );
}

export default Room;

Using the room component

Now we can import and use the room component anywhere in our app. The important thing to remember is that we need to use Next.js’s dynamic import, as explained in the beginning of this guide.

app/page.tsx

import * as React from "react";
import dynamic from "next/dynamic";

const Room = dynamic(() => import("../components/room"), { ssr: false });

// Replace this with your own Whereby room URL
const roomUrl = "";

export default function Home() {
    return (
        <main className="h-screen w-screen">
            <Room roomUrl={roomUrl} />
        </main>
    );
}

As with the provider, this also needs to be a client component. To see more examples of how to customize the room layout, you can see a more in-depth guide on that

That should be all that’s needed to run the Whereby browser-sdk in Next.js. We also have an example implementation of this in our , where you can see a working example of a setup with Next.js and the Whereby browser-sdk.

🖥️
app router architecture
here
here
here.
repository