In this tutorial, we will guide you through adding real-time synchronization to a productivity tool using SuperViz Collaboration and SuperViz Real-time. Real-time synchronization is a crucial feature for collaborative applications, allowing multiple users to interact with shared content simultaneously and see each other's changes as they happen. With SuperViz, you can build interactive tools that update live, providing a seamless collaborative experience for users.
We'll demonstrate how to integrate real-time synchronization into a notes application, enabling users to collaborate on notes with real-time updates. This setup allows multiple participants to edit notes, move elements, and see changes instantly as they happen. 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 SuperViz for real-time synchronization.
1. Create a New React Project
First, create a new React application using Vite with TypeScript.
1npm create vite@latest realtime-notes-app -- --template react-ts2cd realtime-notes-app
2. Install SuperViz Packages
Next, install the necessary SuperViz packages, which will enable us to add real-time synchronization features to our application.
1npm install @superviz/room @superviz/collaboration @superviz/realtime uuid
- @superviz/room: Core package for creating and managing SuperViz rooms.
- @superviz/collaboration: Package containing components for collaboration features like mouse pointers and who-is-online.
- @superviz/realtime: SuperViz Real-Time library for integrating real-time synchronization into your application.
- 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. (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 synchronization in a notes application.
1. Implement the App Component
Open src/App.tsx
and set up the main application component with necessary imports:
1import { useCallback, useEffect, useRef, useState } from "react";2import { createRoom } from "@superviz/room";3import {4MousePointers,5WhoIsOnline,6} from "@superviz/collaboration";7import { Realtime, type Channel } from "@superviz/realtime/client";8import { v4 as generateId } from "uuid";9import { NoteNode } from "./components/note-node";10import { Note } from "./common/types";1112// SuperViz developer token ::13const DEVELOPER_TOKEN = import.meta.env.VITE_SUPERVIZ_API_KEY;1415const ROOM_ID = "video-huddle-application";16const PARTICIPANT_ID = generateId();
Explanation:
- Imports: Import necessary components from React, SuperViz packages, and UUID for managing state, initializing SuperViz, and generating unique identifiers.
- Constants: Define the developer token, room ID, and a unique participant ID.
2. Create the App Component
Set up the main App
component and initialize state variables.
1export default function App() {2const [initialized, setInitialized] = useState(false);3const [notes, setNotes] = useState<Note[]>([]);4const channel = useRef<Channel | null>(null);
Explanation:
- initialized: A state variable to track whether the SuperViz environment has been set up.
- notes: A state variable to manage the list of notes in the application.
- channel: A ref to store the real-time communication channel.
3. Initialize SuperViz and Real-Time Components
Create an initialize
function to set up the SuperViz environment and configure real-time synchronization.
1const initialize = useCallback(async () => {2if (initialized) return;34try {5const room = await createRoom({6developerToken: DEVELOPER_TOKEN,7roomId: ROOM_ID,8participant: {9id: PARTICIPANT_ID,10name: "Participant",11},12group: {13id: "GROUP_ID",14name: "GROUP_NAME",15},16});171819const mousePointers = new MousePointers("mouse-container");20const whoIsOnline = new WhoIsOnline();21room.addComponent(mousePointers);22room.addComponent(whoIsOnline);
Explanation:
- initialize: An asynchronous function that checks if already initialized to prevent duplicate setups.
- createRoom: Creates a SuperViz room with participant and group details.
- Components: Creates and adds mouse pointers and who-is-online components to the room.
4. Set Up Real-Time Synchronization
Within the initialize
function, set up real-time synchronization for notes.
1const realtime = new Realtime(DEVELOPER_TOKEN, {2participant: {3id: PARTICIPANT_ID,4},5});67channel.current = await realtime.connect("realtime-sync");8channel.current.subscribe<Note>("note-change", (event) => {9const note = event.data;1011if (event.participantId === PARTICIPANT_ID || !note) return;1213setNotes((previous) => {14return previous.map((n) => {15if (n.id === note.id) {16return note;17}1819return n;20});21});22});
Explanation:
- Realtime: Initializes the real-time component for synchronization with participant information.
- connect: Connects to a specific channel named "realtime-sync".
- subscribe: Listens for "note-change" events and updates the local state accordingly, ignoring changes from the current participant to avoid redundancy.
5. Initialize Notes
Set the initial state of notes with example content and handle errors.
1setInitialized(true);2setNotes([3{4id: "note-1",5title: `Unicorn's Shopping List`,6content: "Rainbow sprinkles, cloud fluff, and pixie dust",7x: 20,8y: 50,9},10{11id: "note-2",12title: `Zombie's To-Do List`,13content: "Find brains, practice groaning, shuffle aimlessly",14x: 20,15y: 50,16},17{18id: "note-3",19title: `Alien's Earth Observations`,20content:21"Humans obsessed with cat videos and avocado toast. Fascinating!",22x: 20,23y: 50,24},25]);26} catch (error) {27console.error("Error initializing SuperViz Room:", error);28}29}, [initialized, setNotes]);
Explanation:
- setInitialized: Marks the application as initialized to prevent reinitialization.
- setNotes: Sets the initial notes with predefined content and positioning.
- Error Handling: Catches and logs any errors that occur during initialization.
6. Handle Note Changes
Implement the handleNoteChange
function to manage updates to notes.
1const handleNoteChange = useCallback((note: Note) => {2setNotes((prevNotes) => {3return prevNotes.map((n) => {4if (n.id === note.id) {5return note;6}78return n;9});10});1112if (channel.current) {13channel.current.publish("note-change", note);14}15}, []);
Explanation:
- handleNoteChange: Updates the local state of notes when a note is edited and publishes the change to other participants.
- channel.current.publish: Sends the updated note to all participants through the real-time channel.
7. Use Effect Hook for Initialization
Use the useEffect
hook to trigger the initialize
function on component mount.
1useEffect(() => {2initialize();3});
Explanation:
- useEffect: Calls the
initialize
function when the component mounts, setting up the SuperViz environment and real-time synchronization.
8. Render the Application
Return the JSX structure for rendering the application, including notes and collaboration features.
1return (2<>3<div className="w-full h-full bg-gray-200 flex items-center justify-center flex-col">4<header className="w-full p-5 bg-purple-400 flex items-center justify-between">5<h1 className="text-white text-2xl font-bold">Real-Time Sync</h1>6</header>7<main8id="mouse-container"9className="flex-1 p-20 flex w-full gap-2 items-center justify-center overflow-hidden bg-canvas-background"10>11{notes.map((note, index) => (12<NoteNode key={index} note={note} onChange={handleNoteChange} />13))}14</main>15</div>16</>17);
Explanation:
- Header: Displays the title of the application.
- Main Container: Has the ID "mouse-container" which matches the element ID used for mouse pointers.
- Notes Rendering: Maps through the notes array to render each note using the NoteNode component.
Step 3: Create the NoteNode Component
The NoteNode
component is responsible for displaying individual notes and handling edits.
1. Create the NoteNode Component
Create a new file named src/components/note-node.tsx
and add the following implementation:
1import React, { useState } from "react";2import { Note } from "../common/types";34interface NoteNodeProps {5note: Note;6onChange: (note: Note) => void;7}89export const NoteNode: React.FC<NoteNodeProps> = ({ note, onChange }) => {10const [title, setTitle] = useState(note.title);11const [content, setContent] = useState(note.content);1213const handleTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => {14const newTitle = event.target.value;15setTitle(newTitle);16onChange({ ...note, title: newTitle });17};1819const handleContentChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {20const newContent = event.target.value;21setContent(newContent);22onChange({ ...note, content: newContent });23};2425return (26<div className="note-node p-4 bg-white rounded shadow-lg" style={{ position: "absolute", left: note.x, top: note.y }}>27<input28type="text"29value={title}30onChange={handleTitleChange}31className="font-bold mb-2 w-full border-none focus:outline-none"32/>33<textarea34value={content}35onChange={handleContentChange}36className="w-full border-none focus:outline-none"37rows={5}38/>39</div>40);41}
Explanation:
- NoteNode Component: Displays each note with editable fields for title and content.
- handleTitleChange & handleContentChange: Updates the state and calls the
onChange
callback to propagate changes.
2. Define the Note Type
Create a new file named src/common/types.ts
and define the Note
type.
1export interface Note {2id: string;3title: string;4content: string;5x: number;6y: number;7}
Explanation:
- Note Type: Defines the structure of a note, including ID, title, content, and position.
Step 4: Understanding the Project Structure
Here's a quick overview of how the project structure supports real-time synchronization:
App.tsx
- Creates a SuperViz room using the createRoom function.
- Sets up collaboration components like mouse pointers and who-is-online.
- Handles real-time synchronization using the Realtime client.
NoteNode.tsx
- Displays individual notes with editable fields.
- Handles local state updates and propagates changes to the parent component.
types.ts
- Defines the data structure for notes used throughout the application.
Step 5: Running the Application
1. Start the React 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. You can interact with the notes and see updates in real-time as other participants join the session.
2. Test the Application
- Real-Time Notes: Open the application in multiple browser windows or tabs to simulate multiple participants and verify that changes made to notes are reflected in real-time for all users.
- Collaborative Interaction: Test the responsiveness of the application by editing notes and observing how changes appear for other participants.
Summary
In this tutorial, we built a collaborative notes application using SuperViz for real-time synchronization. We used the @superviz/room package to create a room, the @superviz/collaboration package for mouse pointers and who-is-online features, and the @superviz/realtime package for synchronizing note changes between participants. This setup enables multiple users to collaborate seamlessly on shared notes, seeing each other's edits in real-time.
Feel free to explore the full code and further examples in the GitHub repository for more details.