In this tutorial, we will guide you through adding real-time mouse pointers to a web application using SuperViz. Real-time mouse pointers are essential for collaborative applications, allowing users to see each other's cursor movements and interactions on a shared screen. We'll also add an optional video huddle feature for enhanced collaboration.
We'll demonstrate how to use SuperViz to implement real-time mouse pointers in a JavaScript application. Although we'll use a <canvas>
element for rendering shared content, the real-time mouse pointers component is versatile and can be used with other HTML elements as well. This flexibility allows developers to integrate real-time pointers in a variety of web application contexts, providing a dynamic and interactive experience similar to collaborative platforms like Google Docs or Figma. Let's get started!
Prerequisite
To follow this tutorial, you will need a SuperViz account and a developer token. If you already have an account and a developer token, you can move on to the next step.
Create an account
To create an account, go to https://dashboard.superviz.com/register and create an account using either Google or an email/password. It's important to note that when using an email/password, you will receive a confirmation link that you'll need to click to verify your account.
Retrieving a Developer Token
To use the SDK, you’ll need to provide a developer token, as this token is essential for associating SDK requests with your account. You can retrieve both development and production SuperViz tokens from the dashboard..
Copy and save the developer token, as you will need it in the next steps of this tutorial.
Step 1: Set Up Your Application
To begin, you'll need to set up a new project where we will integrate the SuperViz packages for real-time mouse pointers and video collaboration.
1. Create a New Project
First, create a new application using Vite with TypeScript.
1npm create vite@latest mouse-pointers-demo -- --template react-ts2cd mouse-pointers-demo
2. Install SuperViz Packages
Next, install the required SuperViz packages which will enable us to add real-time mouse pointer and video features to our application.
1npm install @superviz/room @superviz/collaboration @superviz/video uuid
- @superviz/room: Core package for creating and managing SuperViz rooms.
- @superviz/collaboration: Contains components for collaboration features, including mouse pointers.
- @superviz/video: Provides video conferencing functionality.
- uuid: A library for generating unique identifiers, useful for creating unique participant IDs.
3. Configure tailwind
In this tutorial, we'll use the Tailwind css framework. First, install the tailwind package.
1npm install -D tailwindcss postcss autoprefixer2npx tailwindcss init -p
We then need to configure the template path. Open tailwind.config.js
in the root of the project and insert the following code.
1/** @type {import('tailwindcss').Config} */2export default {3content: [4"./index.html",5"./src/**/*.{js,ts,jsx,tsx}",6],7theme: {8extend: {},9},10plugins: [],11}
Then we need to add the tailwind directives to the global CSS file the src/index.css
.
1@tailwind base;2@tailwind components;3@tailwind utilities;
4. Set Up Environment Variables
Create a .env
file in your project root and add your SuperViz developer key. This key will be used to authenticate your application with SuperViz services.
1VITE_SUPERVIZ_API_KEY=YOUR_SUPERVIZ_DEVELOPER_KEY
Step 2: Implement the Main Application
In this step, we'll implement the main application logic to initialize SuperViz and handle real-time mouse pointers with an optional video huddle feature.
1. Import Required Packages
Open src/App.tsx
and replace its contents with the following code. Let's start with importing all necessary components:
1import { createRoom, ParticipantEvent, Room } from "@superviz/room";2import { v4 as generateId } from "uuid";34import { useCallback, useEffect, useState, useRef } from "react";5import { MousePointers } from "@superviz/collaboration";6import { VideoEvent, VideoHuddle } from "@superviz/video";78// SuperViz developer token ::9const DEVELOPER_TOKEN = import.meta.env.VITE_SUPERVIZ_API_KEY;
Explanation:
- createRoom, ParticipantEvent, Room: From SuperViz room package for creating and managing rooms.
- MousePointers: Component from SuperViz collaboration package for showing mouse pointers.
- VideoEvent, VideoHuddle: From SuperViz video package for implementing video conferencing.
- React hooks: For managing state and component lifecycle.
- DEVELOPER_TOKEN: Environment variable for your SuperViz API key.
2. Create the App Component
Next, implement the main App component with state management:
1const App = () => {2// States ::3const [participantJoined, setParticipantJoined] = useState(false);4const [huddleStarted, setHuddleStarted] = useState(false);56const roomRef = useRef<Room | null>(null);
Explanation:
- participantJoined: State to track when the local participant has joined the room.
- huddleStarted: State to track when the video huddle has started.
- roomRef: Ref to store the room instance for later use.
3. Initialize SuperViz Room and Mouse Pointers
Add the initialization function that will create the room and add the mouse pointers component:
1// Initialize ::2const initialize = useCallback(async () => {3try {4const room = await createRoom({5developerToken: DEVELOPER_TOKEN,6roomId: "ROOM_ID",7participant: {8id: generateId(),9name: "Name " + Math.floor(Math.random() * 10),10},11group: {12id: "GROUP_ID",13name: "GROUP_NAME",14},15});1617// Store the room instance in the ref18roomRef.current = room;1920room.subscribe(ParticipantEvent.MY_PARTICIPANT_JOINED, () =>21setParticipantJoined(true)22);2324const mousePointers = new MousePointers("canvas");25room.addComponent(mousePointers);26} catch (error) {27console.error("Error initializing SuperViz Room:", error);28}29}, []);3031useEffect(() => {32initialize();33}, [initialize]);
Explanation:
- initialize: Async function that sets up the SuperViz room and mouse pointers.
- createRoom: Creates a room with the specified configuration.
- room.subscribe: Listens for when the local participant joins the room and updates state.
- MousePointers: Creates a new instance that will track mouse movements on the canvas element.
- room.addComponent: Adds the mouse pointers to the room for real-time collaboration.
- useEffect: Calls the initialize function when the component mounts.
4. Implement Video Huddle Functionality
Add the function to start a video huddle for enhanced collaboration:
1const startHuddle = async () => {2const video = new VideoHuddle({3participantType: "host",4});56video.subscribe(VideoEvent.MY_PARTICIPANT_JOINED, () =>7setHuddleStarted(true)8);910// Use the room instance from the ref11if (roomRef.current) {12roomRef.current.addComponent(video);13}14};
Explanation:
- startHuddle: Function to create and start a video huddle when called.
- VideoHuddle: Creates a new video component with the local user as the host.
- video.subscribe: Listens for when the local participant joins the video huddle and updates state.
- roomRef.current.addComponent: Adds the video component to the room for real-time video communication.
5. Render the User Interface
Finally, add the render function to display the canvas and video huddle button:
1return (2<div className="w-full h-full bg-gray-200 flex items-center justify-center flex-col relative">3<canvas id="canvas" className="w-full h-full"></canvas>45{participantJoined && !huddleStarted && (6<button7className="bg-[#6210cc] text-white px-5 py-3 text-xs rounded-lg absolute top-5 left-5 z-10"8onClick={startHuddle}9>10START VIDEO HUDDLE11</button>12)}13</div>14);15};1617export default App;
Explanation:
- Canvas Element: A
<canvas>
element that serves as the shared space where mouse pointers will be tracked. - Conditional Button: A button that appears only when the participant has joined but the video huddle hasn't started.
- onClick Handler: Calls the startHuddle function when the button is clicked.
Step 3: Understanding the Application Structure
Here's a quick overview of how the application components work together:
- Room Creation and Management
- The application creates a SuperViz room when it loads.
- It subscribes to events from the room to track when participants join.
- Mouse Pointers
- The MousePointers component tracks cursor movements on the canvas.
- It renders other participants' cursors in real-time on the canvas element.
- Video Huddle
- Optionally, participants can start a video huddle for face-to-face communication.
- The video huddle enhances collaboration alongside mouse pointer tracking.
Step 4: Running the Application
1. Start the Application
To run your application, use the following command in your project directory:
1npm run dev
This command will start the development server and open your application in the default web browser.
2. Test the Application
- Real-Time Mouse Pointers: Open the application in multiple browser windows or tabs to simulate multiple participants and verify that mouse movements are displayed in real-time for all users.
- Video Huddle Feature: Click the "START VIDEO HUDDLE" button in one window to start a video conference and verify that other participants can join the conference.
- Collaborative Interaction: Test how mouse pointers and video conferencing work together to create a complete collaborative environment.
Summary
In this tutorial, we implemented real-time mouse pointers and an optional video huddle feature in a web application using SuperViz. We used the @superviz/room package to create and manage the room, the @superviz/collaboration package for mouse pointers, and the @superviz/video package for video conferencing. This setup provides a powerful foundation for collaborative applications where users need to see each other's interactions and communicate in real-time.
Feel free to explore the full code and further examples in the GitHub repository for more details.