Weave.js

WeaveStoreWebsockets

API reference for WeaveStoreWebsockets class

Overview

The WeaveStoreWebsockets class is a client-side store implementation in the Weave.js ecosystem that enables real-time collaboration using WebSockets. It connects to a compatible WebSocket server—such as WeaveWebsocketsServer to sync shared canvas state across all connected users.

Built on top of Yjs and y-websocket, it acts as a bridge between the client-side Weave.js app and the remote backend, handling updates, awareness (presence), and document syncing with minimal setup.

Import

import { WeaveStoreWebsockets } from "@inditextech/weave-store-websockets/client";

Instantiation

const store = new WeaveStoreWebsockets(initialRoomData: Uint8Array | FetchInitialState | undefined, storeOptions: WeaveStoreOptions, websocketOptions: WeaveStoreWebsocketsOptions);

Parameters

Property Type Required Default Description

initialRoomData

Uint8Array | FetchInitialState | undefined

Required

If the room is not new, the actual data (snapshot) of the room in binary form.

storeOptions.getUser

() ⇒ WeaveUser

Required

A function that returns a WeaveUser as definition of the metadata of the user that connects to a room

storeOptions.undoManagerOptions

WeaveUndoManagerOptions

An object that defines the Undo-Redo manager options

websocketOptions.roomId

string

Required

Room id that we want to join

websocketOptions.wsOptions

{ serverUrl: string; }

Required

WebSocket underlying client properties

websocketOptions.callbacks

WeaveStoreWebsocketsCallbacks

{}

Callbacks defined for the instance

TypeScript types

type WeaveStoreWebsocketsConnectionStatusKeys =
  keyof typeof WEAVE_STORE_WEBSOCKETS_CONNECTION_STATUS;
type WeaveStoreWebsocketsConnectionStatus =
  (typeof WEAVE_STORE_WEBSOCKETS_CONNECTION_STATUS)[WeaveStoreWebsocketsConnectionStatusKeys];

type WeaveStoreWebsocketsCallbacks = {
  onConnectionStatusChange?: (
    status: WeaveStoreWebsocketsConnectionStatus,
  ) => void;
};

type WeaveUser = {
  name: string;
  email: string;
};

type WeaveUndoManagerOptions = {
  captureTimeout?: number;
  trackedOrigins?: Set<any>;
};

type WeaveStoreOptions = {
  getUser: () => WeaveUser;
  undoManagerOptions?: WeaveUndoManagerOptions;
};

type WeaveStoreWebsocketsOptions = {
  roomId: string;
  wsOptions: {
    serverUrl: string;
  };
  callbacks?: WeaveStoreWebsocketsCallbacks;
};

type FetchInitialState = (doc: Doc) => void;

Events

onStateChange

onStateChange: WeaveStoreOnStateChangeEvent;

The onStateChange event is called when the shared-state changes.

onRoomLoaded

onRoomLoaded: WeaveStoreOnRoomLoadedEvent;

The onRoomLoaded event is called when the room shared-state is loaded.

onUndoManagerStatusChange

onUndoManagerStatusChange: WeaveStoreOnUndoRedoChangeEvent;

The onUndoManagerStatusChange event is called when the undo/redo manager changes. It is also emitted when a room connection begins (on every connect() call), resetting all stack counts to 0 and both canUndo and canRedo to false.

onNodeChange

onNodeChange: WeaveStoreOnNodeChangeEvent;

The onNodeChange event is called when there is a single node selected and the node changes.

Methods

connect

connect(): Promise<void>

The connect method is called when an user tries to join a room.

disconnect

disconnect(): Promise<void>

The disconnect method is called when an user leaves a room.

setAwarenessInfo

setAwarenessInfo(field: string, value: unknown): void

This method set awareness information, for example if you want to inform the metadata of an user that connected to the room, or to send the pointer information of that user to the other peers.