Skip to content

Commit c263c8b

Browse files
lodyai[bot]lody-ai
andauthored
feat(loro-websocket): add sendExternalUpdates method to LoroWebsocketClient (#46)
Add a new public method sendExternalUpdates() that allows users to send additional updates to a specified room that are not from the local peer's document. This is useful for forwarding updates received from other sources or peers. The method: - Takes crdt type, roomId, and an array of update payloads - Validates that the room is active and WebSocket is open - Uses existing sendUpdateOrFragments() for proper fragmentation handling - Throws descriptive errors if preconditions are not met Co-authored-by: lody <[email protected]>
1 parent db6a681 commit c263c8b

File tree

1 file changed

+35
-0
lines changed
  • packages/loro-websocket/src/client

1 file changed

+35
-0
lines changed

packages/loro-websocket/src/client/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,41 @@ export class LoroWebsocketClient {
12151215
);
12161216
}
12171217

1218+
/**
1219+
* Send additional updates to a specified room.
1220+
*
1221+
* This method allows sending updates that are not from the local peer's document,
1222+
* such as updates received from other sources or peers. The updates will be
1223+
* broadcast to the room and other connected clients.
1224+
*
1225+
* @param crdt - The CRDT type of the room
1226+
* @param roomId - The room ID to send updates to
1227+
* @param updates - Array of update payloads to send
1228+
* @throws Error if not connected or room is not active
1229+
*/
1230+
sendExternalUpdates(
1231+
crdt: CrdtType,
1232+
roomId: string,
1233+
updates: Uint8Array[]
1234+
): void {
1235+
const id = crdt + roomId;
1236+
const active = this.activeRooms.get(id);
1237+
if (!active) {
1238+
throw new Error(
1239+
`Cannot send updates: room ${roomId} (${crdt}) is not active`
1240+
);
1241+
}
1242+
1243+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
1244+
throw new Error("Cannot send updates: WebSocket is not open");
1245+
}
1246+
1247+
// Send each update individually, fragmenting when necessary
1248+
for (const update of updates) {
1249+
this.sendUpdateOrFragments(crdt, roomId, update);
1250+
}
1251+
}
1252+
12181253
consumeSentBatch(refId: HexString): { roomKey: string; updates: Uint8Array[] } | undefined {
12191254
const entry = this.sentUpdateBatches.get(refId);
12201255
if (entry) {

0 commit comments

Comments
 (0)