yjs / y-websocket

Websocket Connector for Yjs
https://docs.yjs.dev/ecosystem/connection-provider/y-websocket
MIT License
499 stars 258 forks source link

Support transforming data for WebSocket transport. #78

Closed whawker closed 8 months ago

whawker commented 3 years ago

On AWS I am not able to use binary in WebSocket payloads, this PR adds two backwards compatible options transformRead and transformWrite to convert the incoming and outgoing data on the WebSocket.

Example usage

import { fromBase64, toBase64 } from 'lib0/buffer';

new WebsocketProvider(
  'ws://localhost:1234',
  'my-room-name',
  yDoc,
  {
    transformRead: fromBase64,
    transformWrite: toBase64
  }
);

Huly®: YJS-736

MartinRamm commented 8 months ago

+1 for this. Also solves #152 @dmonad any chance you would consider merging this PR?

dmonad commented 8 months ago

I made bad experiences with feature requests like this before. It's a valid concern. However, it adds a few features that blow up the codebase, that I need to maintain forever, and that are generally not needed by most users. I don't want y-websocket to blow up with things. Ultimately, the codebase would get too large for anyone to understand, modify, and adapt to their own needs.

There is just a plethora of things that people want to add to y-websocket. But I want y-websocket to be a barebone implementation that you can adapt to their own needs. If you are interested in a one-size-fits all solution there are other attempts: Check y-sweet, LiveBlocks, or Hocuspocus.

I also feel you could solve the issue differently by wrapping Websocket in a Base64 Layer and supplying it as a polyfill to y-websocket:

class Base64Websocket {
  send (m) { super.send(toBase64(m)) }
  set onmessage (f) { 
    super.onmessage = (m) => f(fromBase64(m))
   }
}
new WebsocketProvider(.., { WebsocketPolyfill: Base64Websocket })