RealtimeCursor is a powerful real-time collaboration platform that allows you to add live cursor tracking, approval workflows, and GitHub-style history to your applications.
This documentation will guide you through the process of integrating RealtimeCursor into your React applications.
Install the RealtimeCursor SDK using npm or yarn:
npm install realtimecursor-sdk # or yarn add realtimecursor-sdk
To use RealtimeCursor, you need to authenticate your users. The SDK provides a simple hook for authentication:
import React from 'react';
import { useRealtimeCursor } from 'realtimecursor-sdk';
function App() {
const {
client,
user,
login,
logout,
isAuthenticated,
loading
} = useRealtimeCursor({
apiUrl: 'https://your-realtimecursor-api.com',
socketUrl: 'https://your-realtimecursor-api.com'
});
const handleLogin = async () => {
try {
await login('user@example.com', 'password');
} catch (error) {
console.error('Login failed:', error);
}
};
return (
{!isAuthenticated ? (
) : (
Welcome, {user?.fullName}
)}
);
}
Once authenticated, you can work with projects using the useProject hook:
import React, { useRef } from 'react';
import { useProject } from 'realtimecursor-sdk';
function ProjectEditor({ client, projectId }) {
const editorRef = useRef(null);
const {
project,
content,
comments,
stagedChanges,
history,
collaborators,
cursors,
updateContent,
addComment,
reviewChange
} = useProject(client, projectId);
const handleContentChange = (newContent) => {
updateContent(newContent);
};
return (
{project?.name}
);
}
To enable cursor tracking, use the useCursorTracking hook and CursorOverlay component:
import { useCursorTracking, CursorOverlay } from 'realtimecursor-sdk';
function ProjectEditor({ client, projectId }) {
// ... other code from previous example
// Set up cursor tracking
useCursorTracking(editorRef, client);
return (
);
}
Display project history with the HistoryList component:
import { HistoryList } from 'realtimecursor-sdk';
function ProjectHistory({ history }) {
return (
History
);
}
For admin users, you can review and approve/reject changes:
import { ReviewChangesList } from 'realtimecursor-sdk';
function ReviewChanges({ stagedChanges, reviewChange }) {
return (
Pending Changes ({stagedChanges.length})
reviewChange(changeId, true, feedback)}
onReject={(changeId, feedback) => reviewChange(changeId, false, feedback)}
/>
);
}
You can self-host RealtimeCursor by following these steps:
git clone https://github.com/yourusername/realtimecursor.gitnpm install./start-dev.shFor production deployment, see the Deployment Guide.
RealtimeCursorSDK: Main SDK classcreateRealtimeCursorClient(config): Helper to create SDK instanceuseRealtimeCursor(config): Main hook for authenticationuseProject(client, projectId): Hook for project operationsuseCursorTracking(editorRef, client): Hook for cursor trackingCollaborativeEditor: Textarea with collaboration featuresCursorOverlay: Displays other users' cursorsCollaboratorsList: Shows active collaboratorsHistoryList: Displays project historyReviewChangesList: UI for reviewing changes
Comments System
You can add comments to specific text selections:
function addComment() { const selectedText = getSelectedText(); const commentText = "This is a comment"; client.addComment(projectId, { text: commentText, selectedText: selectedText, startPosition: selectionStart, endPosition: selectionEnd }); }