### Develop Next.js Example Source: https://github.com/jamsocket/y-sweet/blob/main/js-pkg/README.md Execute this command from the repository root to start the development server for the Next.js example. ```bash npm run dev -w examples/nextjs ``` -------------------------------- ### Run Y-Sweet NextJS Demo Source: https://github.com/jamsocket/y-sweet/blob/main/examples/nextjs/README.md Commands to install dependencies and start the development server with a connection string. ```bash npm install ``` ```bash CONNECTION_STRING= npm run dev ``` -------------------------------- ### Start Development Server Source: https://github.com/jamsocket/y-sweet/blob/main/examples/vanilla/README.md Execute this command to start the development server for the VanillaJS project. ```bash npm run dev ``` -------------------------------- ### Install @y-sweet/sdk Source: https://github.com/jamsocket/y-sweet/blob/main/js-pkg/sdk/README.md Use npm to install the SDK package. ```bash npm install @y-sweet/sdk ``` -------------------------------- ### Install Dependencies Source: https://github.com/jamsocket/y-sweet/blob/main/js-pkg/README.md Run this command from the repository root to install all project dependencies. ```bash npm install ``` -------------------------------- ### Sync development environment Source: https://github.com/jamsocket/y-sweet/blob/main/python/README.md Sets up the virtual environment and installs dependencies using uv. ```bash uv sync --dev ``` -------------------------------- ### Install @y-sweet/react Source: https://github.com/jamsocket/y-sweet/blob/main/js-pkg/react/README.md Command to install the library via npm. ```bash npm install @y-sweet/react ``` -------------------------------- ### Start Y-Sweet Server Source: https://github.com/jamsocket/y-sweet/blob/main/examples/vanilla/README.md In a separate terminal, start the Y-Sweet server using your connection string. Replace `` with your actual connection string. ```bash CONNECTION_STRING= npm run server ``` -------------------------------- ### Install uv project manager Source: https://github.com/jamsocket/y-sweet/blob/main/python/README.md Installs the uv project manager on Mac or Linux systems. ```bash curl -LsSf https://astral.sh/uv/install.sh | sh ``` -------------------------------- ### Run Local Y-Sweet Server Source: https://github.com/jamsocket/y-sweet/blob/main/docs/running.md Use this command to quickly start a local Y-Sweet server. It downloads the server if not already present. ```bash npx y-sweet@latest serve ``` -------------------------------- ### Install @y-sweet/client Source: https://github.com/jamsocket/y-sweet/blob/main/js-pkg/client/README.md Use this command to add the @y-sweet/client package to your project dependencies. ```bash npm install @y-sweet/client ``` -------------------------------- ### Run Y-Sweet Server Locally Source: https://context7.com/jamsocket/y-sweet/llms.txt Start a local Y-Sweet development server. Use in-memory storage by default, or specify a path for file system persistence or an S3 URL for S3 storage. ```bash npx y-sweet@latest serve ``` ```bash npx y-sweet@latest serve /path/to/data ``` ```bash npx y-sweet@latest serve s3://my-bucket/y-sweet-data ``` ```bash npx y-sweet@latest serve --auth /path/to/data ``` -------------------------------- ### Manage User Presence with React Hooks Source: https://context7.com/jamsocket/y-sweet/llms.txt Use `usePresence` to get other users' presence data and `usePresenceSetter` to update local user presence. Include self in presence maps with `{ includeSelf: true }`. Requires setup with `useYjsProvider`. ```tsx import { usePresence, usePresenceSetter, useAwareness, useConnectionStatus, useHasLocalChanges, useYjsProvider } from '@y-sweet/react' type UserPresence = { name: string color: string cursor: { x: number; y: number } | null } function CollaborativeCanvas() { // Get other users' presence (excludes self by default) const others = usePresence() // Get presence setter const setPresence = usePresenceSetter() // Include self in presence map const allUsers = usePresence({ includeSelf: true }) // Set local user's presence useEffect(() => { setPresence({ name: 'Alice', color: '#ff0000', cursor: null }) }, []) const handleMouseMove = (e: React.MouseEvent) => { setPresence({ name: 'Alice', color: '#ff0000', cursor: { x: e.clientX, y: e.clientY } }) } return (
{/* Render other users' cursors */} {Array.from(others.entries()).map(([clientId, presence]) => ( presence.cursor && (
{presence.name}
) ))}
) } ``` ```tsx function ConnectionIndicator() { // Get connection status const status = useConnectionStatus() // 'offline' | 'connecting' | 'handshaking' | 'connected' | 'error' // Check for unsaved local changes const hasLocalChanges = useHasLocalChanges() return (
Status: {status} {hasLocalChanges && Saving...}
) } ``` ```tsx function AdvancedUsage() { // Access raw provider for advanced use cases const provider = useYjsProvider() // Access raw awareness const awareness = useAwareness() return ( ) } ``` -------------------------------- ### Run Local Y-Sweet Server with Data Persistence Source: https://github.com/jamsocket/y-sweet/blob/main/docs/running.md Specify a directory to persist Y-Sweet data to disk. If the path starts with `s3://`, it will use S3-compatible storage. ```bash npx y-sweet@latest serve /path/to/data ``` -------------------------------- ### Initialize and Use DocumentManager in TypeScript Source: https://context7.com/jamsocket/y-sweet/llms.txt Demonstrates initializing the DocumentManager with a connection string and performing operations like creating documents, getting client tokens, and reading/writing document content. ```typescript import { DocumentManager } from '@y-sweet/sdk' import * as Y from 'yjs' // Initialize with connection string (ys:// for http, yss:// for https) const manager = new DocumentManager('ys://localhost:8080') // With authentication: 'ys://server-token@localhost:8080' // Create a new document const result = await manager.createDoc() console.log(result.docId) // 'abc123xyz' // Create document with specific ID const doc = await manager.createDoc('my-custom-doc-id') // Get client token for a document const clientToken = await manager.getClientToken('my-doc-id') // { url: 'ws://...', baseUrl: 'http://...', docId: '...', token: '...' } // Get or create document and token in one call const token = await manager.getOrCreateDocAndToken('optional-doc-id') // Get client token with authorization options const readOnlyToken = await manager.getClientToken('my-doc-id', { authorization: 'read-only', validForSeconds: 7200 }) // Read document as Yjs update const update = await manager.getDocAsUpdate('my-doc-id') const doc = new Y.Doc() Y.applyUpdate(doc, update) // Update document with Yjs update const newDoc = new Y.Doc() newDoc.getMap('data').set('key', 'value') const updateBytes = Y.encodeStateAsUpdate(newDoc) await manager.updateDoc('my-doc-id', updateBytes) // Create document with initial content const initialDoc = new Y.Doc() initialDoc.getText('content').insert(0, 'Hello World') const newDocResult = await manager.createDocWithContent( Y.encodeStateAsUpdate(initialDoc) ) // Check storage health const storeStatus = await manager.checkStore() if (!storeStatus.ok) { console.error('Storage error:', storeStatus.error) } ``` -------------------------------- ### Implement Collaborative Editor Component Source: https://context7.com/jamsocket/y-sweet/llms.txt Full example of a collaborative page component using Y-Sweet React hooks for state management and presence tracking. ```tsx // app/collaborative-editor/page.tsx import { YDocProvider } from '@y-sweet/react' import { CollaborativeEditor } from './CollaborativeEditor' function generateId() { return Math.random().toString(36).substring(2, 15) } export default function Page({ searchParams }: { searchParams: { doc?: string } }) { const docId = searchParams.doc ?? generateId() return ( ) } // CollaborativeEditor.tsx 'use client' import { useMap, usePresence, usePresenceSetter, useConnectionStatus, useHasLocalChanges } from '@y-sweet/react' import { useEffect, useState } from 'react' type Presence = { name: string color: string } export function CollaborativeEditor() { const data = useMap('editor-data') const others = usePresence() const setPresence = usePresenceSetter() const status = useConnectionStatus() const hasUnsavedChanges = useHasLocalChanges() const [userName] = useState(`User-${Math.random().toString(36).slice(2, 6)}`) useEffect(() => { setPresence({ name: userName, color: `hsl(${Math.random() * 360}, 70%, 50%)` }) }, [userName, setPresence]) return (
{status === 'connected' ? '🟢' : '🔴'} {status} {hasUnsavedChanges && Saving...} Online: {Array.from(others.values()).map(p => p.name).join(', ')}
data.set('title', e.target.value)} placeholder="Document title" />