In this tutorial, we will guide you through adding real-time mouse pointers to a web application using the SuperViz React SDK. Real-time mouse pointers are essential for collaborative applications, allowing users to see each other's cursor movements and interactions on a shared screen. This feature enhances communication and collaboration, making it easier for participants to follow along with each other's actions.

We'll demonstrate how to use SuperViz to implement real-time mouse pointers in a React 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 React Application

To begin, you'll need to set up a new React project where we will integrate the SuperViz SDK for real-time mouse pointers.

1. Create a New React Project

First, create a new React application using Create React App with TypeScript.

1
npm create vite@latest mouse-pointers-demo -- --template react-ts
2
cd mouse-pointers-demo

2. Install SuperViz SDK

Next, install SuperViz, which will enable us to add real-time mouse pointer features to our application.

1
npm install @superviz/react-sdk uuid
  • @superviz/react-sdk: SDK for integrating real-time collaboration features, including mouse pointers.
  • 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.

1
npm install -D tailwindcss postcss autoprefixer
2
npx 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} */
2
export default {
3
content: [
4
"./index.html",
5
"./src/**/*.{js,ts,jsx,tsx}",
6
],
7
theme: {
8
extend: {},
9
},
10
plugins: [],
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.

1
VITE_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.

1. Implement the App Component

Open src/App.tsx and set up the main application component using the SuperVizRoomProvider to manage the collaborative environment.

1
import { SuperVizRoomProvider } from '@superviz/react-sdk'
2
import { v4 as generateId } from 'uuid'
3
import Room from './Room'
4
5
const developerKey = import.meta.env.VITE_SUPERVIZ_API_KEY;
6
const participantId = generateId()
7
8
export default function App() {
9
return (
10
<SuperVizRoomProvider
11
developerKey={developerKey}
12
group={{
13
id: 'realtime-mouse-pointers',
14
name: 'realtime-mouse-pointers',
15
}}
16
participant={{
17
id: participantId,
18
name: 'Participant',
19
}}
20
roomId='realtime-mouse-pointers'
21
>
22
<Room />
23
</SuperVizRoomProvider>
24
)
25
}

Explanation:

  • SuperVizRoomProvider: This component wraps the application to enable real-time features and provides configuration for group and participant details.
  • developerKey: Retrieves the developer key from environment variables to authenticate with SuperViz.
  • participantId: Generates a unique ID for each participant using the uuid library.
  • Room Component: Contains the logic for rendering the canvas and handling mouse pointers.

2. Implement the Room Component

Create a new file named src/Room.tsx and add the following implementation:

1
import { MousePointers } from "@superviz/react-sdk";
2
3
export default function Room() {
4
return (
5
<div className='w-full h-full bg-gray-200 flex items-center justify-center flex-col'>
6
<header className='w-full p-5 bg-purple-400 flex items-center justify-between'>
7
<h2 className='text-white text-2xl font-bold'>SuperViz Real-Time Mouse Pointers</h2>
8
</header>
9
<main className='flex-1 w-full h-full'>
10
<div className='w-full h-full'>
11
<canvas id="canvas" className='w-full h-full'></canvas>
12
</div>
13
14
<MousePointers elementId="canvas" />
15
</main>
16
</div>
17
)
18
}

Explanation:

  • MousePointers Component: Displays the real-time location of each participant's mouse cursor on the canvas, allowing users to see where others are interacting.
  • elementId: Specifies the ID of the element (canvas) where the mouse pointers will be shown. The component can also be used with other HTML elements such as div or svg, offering flexibility in its application.
  • Canvas Element: A <canvas> element is used to render the shared content, with an ID that matches the elementId specified in MousePointers.

Step 3: Understanding the Project Structure

Here's a quick overview of how the project structure supports real-time mouse pointers:

  1. App.tsx
    • Initializes the SuperViz environment.
    • Sets up participant information and room details.
    • Renders the Room component within the SuperVizRoomProvider.
  2. Room.tsx
    • Contains the main UI elements, including the canvas.
    • Integrates the MousePointers component to show real-time cursor movements.

Step 4: Running the Application

1. Start the React Application

To run your application, use the following command in your project directory:

1
npm run dev

This command will start the development server and open your application in the default web browser. You can interact with the canvas and see mouse pointers in real-time as other participants join the session.

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.
  • Collaborative Interaction: Test the responsiveness of the application by moving the mouse around the canvas and observing the pointers of other participants.

Summary

In this tutorial, we implemented real-time mouse pointers in a web application using SuperViz and React. We configured a React application to display mouse pointers for all participants, enabling seamless collaboration and interaction. This setup can be extended and customized to fit various scenarios where real-time collaboration and visual feedback are required.

Feel free to explore the full code and further examples in the GitHub repository for more details.