realtimejul22
v1.2.1
Published
Real-time cursor tracking and collaboration for web applications
Downloads
9
Maintainers
Readme
RealtimeJul22
A lightweight library for real-time cursor tracking and collaboration in web applications.
Installation
npm install realtimejul22Quick Start
import { RealtimeCursor } from 'realtimejul22';
const cursor = new RealtimeCursor({
projectId: 'your-project-id',
user: {
id: 'user-123',
name: 'John Doe',
color: '#3b82f6'
}
});
cursor.connect();CDN Usage
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.js"></script>
<script>
const cursor = new realtimejul22.RealtimeCursor({
projectId: 'your-project-id',
user: {
id: 'user-123',
name: 'John Doe',
color: '#3b82f6'
}
});
cursor.connect();
</script>Features
- 🖱️ Real-time cursor tracking
- 👥 Collaborator presence
- 📝 Content synchronization
- ⌨️ Typing indicators
- 🔄 Automatic reconnection
- 🎨 Customizable cursors and colors
API Reference
RealtimeCursor
const cursor = new RealtimeCursor(options);Options
apiUrl(string): URL of the RealtimeCursor API server (defaults to hosted service)projectId(string): Unique identifier for the projectuser(object): User informationid(string): Unique identifier for the username(string): Display name for the usercolor(string, optional): Color for the user's cursor (hex code)
debug(boolean, optional): Enable debug logging
Methods
connect(): Connect to the real-time servicedisconnect(): Disconnect from the real-time serviceupdateCursor(position): Update cursor positionupdateContent(content): Update contentupdateTypingStatus(isTyping): Update typing statuson(event, callback): Add event listeneroff(event, callback): Remove event listenergetCollaborators(): Get all collaboratorsgetCursors(): Get all cursorsgetTypingStatus(): Get typing statusisConnected(): Get connection status
Events
connect: Emitted when connected to the servicedisconnect: Emitted when disconnected from the servicecursors-changed: Emitted when cursors are updatedcollaborators-changed: Emitted when collaborators list changestyping-status-changed: Emitted when typing status changescontent-updated: Emitted when content is updated by another useruser-joined: Emitted when a user joinsuser-left: Emitted when a user leaveserror: Emitted when an error occurs
React Hook
import { useRealtimeCursor } from 'realtimejul22';
function Editor() {
const {
cursors,
collaborators,
connected,
typingStatus,
connect,
disconnect,
updateCursor,
updateContent,
updateTypingStatus
} = useRealtimeCursor({
projectId: 'your-project-id',
user: {
id: 'user-123',
name: 'John Doe',
color: '#3b82f6'
}
});
// Use the hook...
}Example
// Initialize the cursor client
const cursor = new RealtimeCursor({
projectId: 'my-project',
user: {
id: 'user-123',
name: 'John Doe',
color: '#3b82f6'
}
});
// Connect to the real-time service
cursor.connect();
// Set up event handlers
cursor.on('connect', () => {
console.log('Connected to real-time service');
});
cursor.on('collaborators-changed', (collaborators) => {
console.log('Active collaborators:', collaborators);
});
cursor.on('cursors-changed', (cursors) => {
console.log('Cursor positions:', cursors);
});
// Update cursor position on mouse move
document.addEventListener('mousemove', (e) => {
const editorElement = document.getElementById('editor');
const rect = editorElement.getBoundingClientRect();
cursor.updateCursor({
x: e.clientX,
y: e.clientY,
relativeX: e.clientX - rect.left,
relativeY: e.clientY - rect.top
});
});
// Update content
document.getElementById('editor').addEventListener('input', (e) => {
cursor.updateContent(e.target.value);
cursor.updateTypingStatus(true);
// Reset typing status after 2 seconds
setTimeout(() => {
cursor.updateTypingStatus(false);
}, 2000);
});
// Disconnect when done
window.addEventListener('beforeunload', () => {
cursor.disconnect();
});License
MIT
