GithubHelp home page GithubHelp logo

napolab / y-durableobjects Goto Github PK

View Code? Open in Web Editor NEW
54.0 0.0 5.0 231 KB

Real-time collaboration with Yjs on Cloudflare Workers using Durable Objects, eliminating Node.js dependencies. Inspired by y-websocket

Home Page: https://yjs.napochaan.dev/

License: MIT License

HTML 1.09% CSS 1.90% TypeScript 96.33% JavaScript 0.68%
durable-objects yjs cloudflareworkers hono

y-durableobjects's Introduction

y-durableobjects

Yjs on Cloudflare Workers with Durable Objects Demo Movie

The y-durableobjects library is designed to facilitate real-time collaboration in Cloudflare Workers environment using Yjs and Durable Objects. It provides a straightforward way to integrate Yjs for decentralized, scalable real-time editing features.

Requirements

  • Hono version 4.2 or higher is required.

Installation

To use y-durableobjects, you need to install the package along with hono, as it is a peer dependency.

npm install y-durableobjects hono

or using yarn:

yarn add y-durableobjects hono

or pnpm:

pnpm add y-durableobjects hono

Below is an updated section for your README, including the recommended wrangler.toml configuration for Durable Objects, followed by the new section providing guidance on configuring Durable Objects:


Configuring Durable Objects

To use Durable Objects with y-durableobjects, include the following configuration in your wrangler.toml file. This setup is essential for defining the Durable Object bindings that your Cloudflare Worker will use.

name = "your-worker-name"
main = "index.js"
compatibility_date = "2021-10-01"

account_id = "your-account-id"
workers_dev = true
route = ""
zone_id = ""

# Durable Objects binding
[durable_objects]
bindings = [
  { name = "Y_DURABLE_OBJECTS", class_name = "YDurableObjects" }
]

# Add your KV Namespaces and other bindings here
# [kv_namespaces]
# ...

# Your environment variables
# [vars]
# ...

Configuration for Durable Objects

To properly utilize Durable Objects, you need to configure bindings in your wrangler.toml file. This involves specifying the name of the Durable Object binding and the class name that represents your Durable Object. For detailed instructions on how to set up your wrangler.toml for Durable Objects, including setting up environment variables and additional resources, refer to Cloudflare's Durable Objects documentation.

This configuration ensures that your Cloudflare Worker can correctly instantiate and interact with Durable Objects, allowing y-durableobjects to manage real-time collaboration sessions.

Extending Hono with Bindings

To integrate y-durableobjects with Hono, extend the Env interface to include the Bindings type for better type safety and IntelliSense support in your editor.

export type Bindings = {
  Y_DURABLE_OBJECTS: DurableObjectNamespace;
};

declare module "hono" {
  interface Env {
    Bindings: Bindings;
  }
}

This allows you to use Y_DURABLE_OBJECTS directly in your Hono application with full type support.

Usage

With Hono shorthand

import { Hono } from "hono";
import { cors } from "hono/cors";
import { YDurableObjects, yRoute } from "y-durableobjects";

const app = new Hono();
app.use("*", cors());

const route = app.route(
  "/editor",
  yRoute((env) => env.Y_DURABLE_OBJECTS),
);

export default route;
export { YDurableObjects };

Without the shorthand

import { Hono } from "hono";
import { cors } from "hono/cors";
import { YDurableObjects } from "y-durableobjects";

const app = new Hono();
app.use("*", cors());
app.get("/editor/:id", async (c) => {
  const id = c.env.Y_DURABLE_OBJECTS.idFromName(c.req.param("id"));
  const obj = c.env.Y_DURABLE_OBJECTS.get(id);

  // get websocket connection
  const url = new URL("/", c.req.url);
  const res = await obj.fetch(url.href, {
    headers: c.req.raw.headers,
  });
  if (res.webSocket === null) return c.body(null, 500);

  return new Response(null, { webSocket: res.webSocket, status: res.status });
});

export default app;
export { YDurableObjects };

Hono RPC support

  • Utilizes Hono's WebSocket Helper, making the $ws method available in hono/client for WebSocket communications.

Server Implementation

import { Hono } from "hono";
import { cors } from "hono/cors";
import { YDurableObjects, yRoute } from "y-durableobjects";

const app = new Hono();
app.use("*", cors());

const route = app.route(
  "/editor",
  yRoute((env) => env.Y_DURABLE_OBJECTS),
);

export default route;
export type AppType = typeof route;
export { YDurableObjects };

Client Implementation

import { hc } from "hono/client";
import type { AppType } from "./server"; // Adjust the import path as needed

const API_URL = "http://localhost:8787";

export const client = hc<AppType>(API_URL);
const ws = client.editor[":id"].$ws({ param: { id: "example" } });
//    ^?const ws: WebSocket

y-durableobjects's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

y-durableobjects's Issues

implement ping/pong

Ping/pong the currently connected websocket connection.

I found that even if all the connections are gone, a canceled get request is sent and new connection 1 is created.

Image from Gyazo

yDoc must be saved with each change.

Problems with existing implementation

In the Hibernation API, instances of DurableObjects go dormant as soon as no data is sent from the client, so yDocs stored in-memory are quickly volatile.

Therefore, the existing implementation of storing the in-memory state in transaction storage when the number of connections drops below a certain number is not appropriate.

Reason why it seems to work in the demo site

However, https://yjs.napochaan.dev, which is now available on the demo site, seems to work fine.
This is because the client implementation of y-websocket, WebSocketProvider, wrote a constructor that periodically sends local yDoc when the connection becomes Open, so it does not go into hibernation state even if Hibernation API is used, It is assumed that the server-side yDoc remained unvolatilized until the client communication was disconnected.

https://github.com/yjs/y-websocket/blob/master/src/y-websocket.js#L302-L311

What should be changed

  • Store changes received via webSocketMessage in transaction storage on a per-snapshot basis
    • Introduce state with the following key to guarantee order.
      • snapshot_size.
      • snapshot:${count}
        • count: the total number of currently stored snapshots +1
  • Join snapshots with constructor and restore yDoc.
  • Control when to merge snapshot.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.