npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webxdc-yjs-provider

v5.1.0

Published

Sync webxdc app state automatically with Yjs

Downloads

12

Readme

webxdc-yjs-provider

An easy way to get started with making a webxdc app. If your app is state-based rather than event-based (which is most likely the case), this may be a good fit for you.

Usage

import * as Y from "yjs";
import { WebxdcSyncProvider } from "webxdc-yjs-provider";

async function init() {
  const ydoc = new Y.Doc();
  const provider = new WebxdcSyncProvider(ydoc);
  await provider.initialStateRestored;
  // From now on the state of the `ydoc` is automatically synced between
  // all the chat members (peers).

  const todoItems = ydoc.getArray("todoItems");
  todoItems.observe(e => {
    // When another peer (or you) modifies the document,
    // this callback will get executed.
    console.log(`to-do items:\n${todoItems.toArray().join("\n")}`);
  });

  todoItems.push(["wash dishes"]);
}
init();

On how to use the Y.Doc object, consult Yjs docs.

How to use webxdc.sendUpdate() and webxdc.setUpdateListener()

In the above example it's impossible to use the webxdc.sendUpdate() and webxdc.setUpdateListener() manually (there are gonna be errors if you try). If you need those functions, use the following example:

import * as Y from "yjs";
import { serializeUpdate, deserializeUpdate } from "webxdc-yjs-provider";
// Note the different file.
import { WebxdcSyncProvider } from "webxdc-yjs-provider/WebxdcSyncProviderGeneric";

const ydoc = new Y.Doc();
const provider = new WebxdcSyncProvider(
  ydoc,
  serializeUpdate,
  deserializeUpdate,
  (outgoingSerializedYjsUpdate) => {
    webxdc.sendUpdate({
      payload: {
        serializedYjsUpdate: outgoingSerializedYjsUpdate,
        myPayload: undefined,
      },
    }, "Document changed");
  },
);
const initialStateRestored = webxdc.setUpdateListener(update => {
  if (update.payload?.serializedYjsUpdate) {
    provider.onIncomingYjsUpdate(update.payload.serializedYjsUpdate);
  }
  if (update.payload?.myPayload) {
    // handleMyPayload(update.payload.myPayload);
  }
});
// Reassign this in order to not send each update immediately
// provider.onNeedToSendLocalUpdates = () => {};
// sendButton.addEventListener("click", () => {
//   provider.sendUnsentLocalUpdates();
// });

// ...
webxdc.sendUpdate({
  payload: {
    serializedYjsUpdate: undefined,
    myPayload: "some data",
  },
  info: "some info",
  summary: "some summary",
  document: "some document name",
}, "My update");

Or consider inlining the library (i.e. writing its functionality yourself).

Also run your app as a regular website

As we know, webxdc apps are very much just regular web apps, except that they may also utilize the webxdc API. This means that it's very easy to make a web version of your webxdc app (or vice versa, if the app is P2P-ready).

 import * as Y from "yjs";
 import { WebxdcSyncProvider } from "webxdc-yjs-provider";
+import { WebrtcProvider } from "y-webrtc";
 
 async function init() {
   const ydoc = new Y.Doc();
+  if (globalThis.webxdc) {
     const provider = new WebxdcSyncProvider(ydoc);
     await provider.initialStateRestored;
+  } else {
+    // TODO don't forget to change the password and the room name,
+    // and other params (see https://github.com/yjs/y-webrtc/#readme).
+    const provider = new WebrtcProvider("testing-webxdc-yjs-provider", ydoc);
+  }
   // From now on the state of the `ydoc` is automatically synced between
   // all the chat members (peers).

Now, if you deploy the app as a static (yep!) website, every visitor's ydoc state will be synced! (unless the default signaling servers are down again, in which case use a different one, perhaps a self-hosted one).

If necessary, add persistence in order to not lose the state when all peers close the website. WebxdcSyncProvider already includes persistence, so you only need to add it in combination with the WebrtcProvider.

For tree-shaking replace globalThis.webxdc with a build-time variable in the above code snippet.