DEV Community

Cover image for How to add real-time synchronization to your productivity-tool
Pachi 🥑 for SuperViz

Posted on • Updated on • Originally published at superviz.com

How to add real-time synchronization to your productivity-tool

Hello everyone,
Today we bring to you a tutorial teaching how to add real-time synchronization to a productivity tool using SuperViz.

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 the SuperViz SDK for real-time synchronization.

1. Create a New React Project

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


npm create vite@latest realtime-notes-app -- --template react-ts
cd realtime-notes-app

Enter fullscreen mode Exit fullscreen mode

2. Install SuperViz SDK

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


npm install @superviz/sdk uuid

Enter fullscreen mode Exit fullscreen mode
  • @superviz/sdk: SDK for integrating real-time collaboration features, including synchronization.
  • 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.


npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

Enter fullscreen mode Exit fullscreen mode

We then need to configure the template path. Open tailwind.config.js in the root of the project and insert the following code.


/** @type  {import('tailwindcss').Config} */
export  default  {
content:  [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
theme:  {
extend:  {},
},
plugins:  [],
}

Enter fullscreen mode Exit fullscreen mode

Then we need to add the tailwind directives to the global CSS file. (src/index.css)


@tailwind base;
@tailwind components;
@tailwind utilities;

Enter fullscreen mode Exit fullscreen mode

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.


VITE_SUPERVIZ_API_KEY=YOUR_SUPERVIZ_DEVELOPER_KEY

Enter fullscreen mode Exit fullscreen mode

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 using SuperViz to manage the collaborative environment.


import { useCallback, useEffect, useRef, useState } from "react";
import SuperVizRoom, {
  LauncherFacade,
  MousePointers,
  WhoIsOnline,
} from "@superviz/sdk";
import { Realtime, type Channel } from "@superviz/realtime/client";
import { v4 as generateId } from "uuid";
import { NoteNode } from "./components/note-node";
import { Note } from "./common/types";

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Imports: Import necessary components from React, SuperViz SDK, and UUID for managing state, initializing SuperViz, and generating unique identifiers.

2. Define Constants

Define constants for the API key, room ID, and participant ID.


const apiKey =  import.meta.env.VITE_SUPERVIZ_API_KEY  as  string;
const  ROOM_ID  =  'video-huddle-application';
const  PARTICIPANT_ID  =  generateId();

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • apiKey: Retrieves the SuperViz API key from environment variables.
  • ROOM_ID: Defines the room ID for the SuperViz session.
  • PARTICIPANT_ID: Generates a unique participant ID using the uuid library.

3. Create the App Component

Set up the main App component and initialize state variables.


export  default  function  App()  {
    const  [initialized, setInitialized]  =  useState(false);
    const  [notes, setNotes]  =  useState<Note[]>([]);
    const superviz =  useRef<LauncherFacade |  null>(null);
    const channel =  useRef<any  |  null>(null);

Enter fullscreen mode Exit fullscreen mode

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.
  • superviz: A ref to store the SuperViz room instance.
  • channel: A ref to store the real-time communication channel.

4. Initialize SuperViz and Real-Time Components

Create an initialize function to set up the SuperViz environment and configure real-time synchronization.


const initialize = useCallback(async () => { 
    if (initialized) return; 

    superviz.current = await SuperVizRoom(apiKey, { 
        roomId: ROOM_ID, 
        participant: { 
            id: PARTICIPANT_ID, 
            name: 'my participant', 
        }, 
        group: { 
            id: 'video-huddle-application', 
            name: 'video-huddle-application', 
        }, 
    })

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • initialize: An asynchronous function that initializes the SuperViz room and checks if it's already initialized to prevent duplicate setups.
  • SuperVizRoom: Configures the room, participant, and group details for the session.

5. Set Up Real-Time Synchronization

Within the initialize function, set up real-time synchronization for notes.


const realtime = new Realtime(apiKey, {
  participant: {
    id: PARTICIPANT_ID,
    name,
  },
});

channel.current = await realtime.connect("realtime-sync");

channel.current.subscribe<Note>("note-change", (event) => {
  const note = event.data;

  if (event.participantId === PARTICIPANT_ID || !note) return;

  setNotes((previous) => {
    return previous.map((n) => {
      if (n.id === note.id) {
        return note;
      }

      return n;
    });
  });
});

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Realtime: Initializes the real-time component for synchronization.
  • RealtimeComponentEvent.REALTIME_STATE_CHANGED: Subscribes to changes in the real-time component state.
  • note-change Subscription: Listens for note changes and updates the local state accordingly, ignoring changes from the current participant to avoid redundancy.

6. Add Mouse Pointers and Online Status

Enhance the application with mouse pointers and online status indicators.


const mousePointers = new MousePointers('mouse-container');
const whoIsOnline = new WhoIsOnline();
superviz.current.addComponent(mousePointers);
superviz.current.addComponent(whoIsOnline)

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • MousePointers: Displays real-time mouse pointers to show where other participants are interacting.
  • WhoIsOnline: Shows a list of currently online participants in the session.

7. Initialize Notes

Set the initial state of notes with example content.


setInitialized(true);
setNotes([
  { id:  'note-1', title:  `Unicorn's Shopping List`, content:  'Rainbow sprinkles, cloud fluff, and pixie dust', x:  20, y:  50  },
  { id:  'note-2', title:  `Zombie's To-Do List`, content:  'Find brains, practice groaning, shuffle aimlessly', x:  20, y:  50  },
  { id:  'note-3', title:  `Alien's Earth Observations`, content:  'Humans obsessed with cat videos and avocado toast. Fascinating!', x:  20, y:  50  },

  ]);

},  [initialized, setNotes]);

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • setInitialized: Marks the application as initialized to prevent reinitialization.
  • setNotes: Sets the initial notes with predefined content, positioning them on the screen.

8. Handle Note Changes

Implement the handleNoteChange function to manage updates to notes.


const handleNoteChange = useCallback((note: Note) => { 
    setNotes((prevNotes) => { 
        return prevNotes.map(n => { 
            if (n.id === note.id) { 
                return note; 
            } 

            return n; 
        }); 
    }); 

    if (channel.current) { 
        channel.current.publish('note-change', note); 
    } 
}, []);

Enter fullscreen mode Exit fullscreen mode

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.

9. Use Effect Hook for Initialization

Use the useEffect hook to trigger the initialize function on component mount.


useEffect(()  =>  {
  initialize();
},  [initialize]);

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • useEffect: Calls the initialize function once when the component mounts, setting up the SuperViz environment and real-time synchronization.

10. Render the Application

Return the JSX structure for rendering the application, including notes and collaboration features.


return ( 
    <> 
        <div className='w-full h-full bg-gray-200 flex items-center justify-center flex-col'>   
            <header className='w-full p-5 bg-purple-400 flex items-center justify-between'> 
                <h2 className='text-white text-2xl font-bold'>Real-Time Sync</h2> 
            </header> 
            <main id="mouse-container" className='flex-1 p-20 flex w-full gap-2 items-center justify-center overflow-hidden'> 
                { 
                    notes.map((note, index) => ( 
                        <NoteNode key={index} note={note} onChange={handleNoteChange} /> 
                    )) 
                }   
            </main> 
        </div> 
    </> 
)

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Header: Displays the title of the application.
  • Main Container: Wraps the notes and collaboration elements, providing a space for interaction.
  • NoteNode Component: Renders each note, allowing for real-time updates and changes.

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:


import React, { useState } from "react"; 
import { Note } from "../common/types";

interface NoteNodeProps { 
    note: Note; 
    onChange: (note: Note) => void; 
} 

export const NoteNode: React.FC<NoteNodeProps> = ({ note, onChange }) => { 
    const [title, setTitle] = useState(note.title); 
    const [content, setContent] = useState(note.content); 

    const handleTitleChange = (event: React.ChangeEvent<HTMLInputElement>) => { 
        const newTitle = event.target.value; 
        setTitle(newTitle); 
        onChange({ ...note, title: newTitle }); 
    }; 
const handleContentChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => { 
    const newContent = event.target.value; 
    setContent(newContent); 
    onChange({ ...note, content: newContent }); 
}; 

    return ( 
        <div className="note-node p-4 bg-white rounded shadow-lg" style={{ position: "absolute", left: note.x, top: note.y }}> 
            <input 
                type="text" 
                value={title} 
                onChange={handleTitleChange} 
                className="font-bold mb-2 w-full border-none focus:outline-none" 
            /> 
            <textarea 
                value={content} 
                onChange={handleContentChange} 
                className="w-full border-none focus:outline-none" 
                rows={5} 
            /> 
        </div> 
    ); 
}

Enter fullscreen mode Exit fullscreen mode

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.


export  interface  Note  {
     id:  string;
     title:  string;
     content:  string;
     x:  number;
     y:  number;

}

Enter fullscreen mode Exit fullscreen mode

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:

  1. App.tsx
    • Initializes the SuperViz environment.
    • Sets up participant information and room details.
    • Handles real-time synchronization and mouse pointers.
  2. NoteNode.tsx
    • Displays individual notes with editable fields.
    • Handles local state updates and propagates changes to the parent component.
  3. 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:


npm run dev

Enter fullscreen mode Exit fullscreen mode

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 configured a React application to handle note editing, enabling multiple users to collaborate seamlessly on shared notes. This setup can be extended and customized to fit various scenarios where real-time collaboration and document editing are required.

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


Join us: SuperViz Hackathon 2.0: AI meets RealTime

Get ready to participate in our Super Hackathon 2.0 this October to showcase your innovative real-time AI communication solutions! Hosted by SuperViz and with $5,000 in prizes up for grabs, we challenge you to integrate AI with SuperViz's real-time platform, pushing the boundaries of interactive communication.

Ready to join? Visit the Super Hackathon 2.0 website to register and stay updated.

Top comments (0)