Skip to content

s2-streamstore/y-s2

Repository files navigation

Yjs - S2 Cloudflare Worker

Try Demo

Overview

Y-S2 is a Cloudflare Worker that provides real-time collaborative document editing using Yjs with S2.dev as the distribution channel and an R2 bucket as the storage provider. It provides scalable WebSocket-based document synchronization where document updates are made durable on an S2 stream and distributed to connected clients and reactively persisted to the R2 bucket.

Getting Started

Prerequisites

  • Cloudflare account with Workers enabled.
  • S2.dev account, a basin with Create stream on append/read enabled, and a scoped access token to the basin you want to use.
  • R2 bucket for snapshot storage.

Environment Variables

S2_ACCESS_TOKEN=your_s2_access_token
S2_BASIN=your_s2_basin_name
R2_BUCKET=your_r2_bucket_name
LOG_MODE=CONSOLE|S2_SINGLE|S2_SHARED  # Optional
SNAPSHOT_BACKLOG_SIZE=100             # Optional

Deployment

# Install dependencies
npm install

# Deploy to Cloudflare Workers
npm run deploy

# Or run locally for development
npm run dev

Client Integration

Connect to the deployed worker using the Yjs WebSocket provider:

import * as Y from 'yjs'
import { WebsocketProvider } from 'y-websocket'

const doc = new Y.Doc()
const provider = new WebsocketProvider('wss://your-worker.your-subdomain.workers.dev', 'room-name', doc, {
  params: { authToken: 'your-auth-token' }
})

Credits

Portions of this project are derived from y-redis, licensed under the GNU Affero General Public License v3.0.

About

A Cloudflare worker to create durable and unlimited collaborative rooms using Yjs and S2

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published