Getting Started
Minimal Example
Section titled “Minimal Example”Here’s a complete example of a Teleportal server and client that syncs a simple Y.js document:
Server
Section titled “Server”import { serve } from "crossws/server";import { Server } from "teleportal/server";import { YDocStorage } from "teleportal/storage";import { getWebsocketHandlers } from "teleportal/websocket-server";
// Create a Teleportal server with in-memory storageconst server = new Server({ storage: new YDocStorage(),});
// Set up WebSocket handlersserve({ websocket: getWebsocketHandlers({ server, onUpgrade: async () => { // Extract user context from the request // In production, you'd verify authentication here return { context: { userId: "user-123", room: "workspace-1" }, }; }, }), fetch: () => new Response("Not found", { status: 404 }),});What’s happening:
Server: The core Teleportal server that manages document sessions and client connectionsYDocStorage: In-memory storage for documents (data is lost on restart)getWebsocketHandlers: Creates WebSocket upgrade and message handlersonUpgrade: Called when a client connects - returns the user context for authentication
Client
Section titled “Client”import { Provider } from "teleportal/providers";import * as Y from "yjs";
// Create a provider that connects to the serverconst provider = await Provider.create({ url: "ws://localhost:3000", document: "my-document",});
// Wait for the document to syncawait provider.synced;
// Access the Y.js documentconst ydoc = provider.doc;
// Create a Y.js text type and insert contentconst ytext = ydoc.getText("content");ytext.insert(0, "Hello, world!");
// Listen for updates from other clientsydoc.on("update", () => { console.log("Document updated:", ytext.toString());});What’s happening:
Provider.create(): Creates a provider and automatically connects to the serverprovider.synced: A promise that resolves when the document is fully synchronizedprovider.doc: The Y.js document instance - use it like any Y.js document- Updates: Changes are automatically synced to other clients connected to the same document
How It Works
Section titled “How It Works”- Client connects: The provider establishes a WebSocket connection to the server
- Sync protocol: The client and server exchange state vectors to determine what updates are needed
- Document sync: Missing updates are sent from server to client, and the document is synchronized
- Real-time updates: When you make changes, they’re sent to the server and broadcast to all other clients
- Awareness: Cursor positions and user presence are automatically synchronized
Next Steps
Section titled “Next Steps”- Integration Guide - Learn how to integrate Teleportal into your application
- Core Concepts - Understand the architecture and components
- Guides - Step-by-step guides for common scenarios