y-websocket icon indicating copy to clipboard operation
y-websocket copied to clipboard

Message handling

Open EvanSchalton opened this issue 5 years ago • 1 comments

I haven't tried my hand at writing JSDoc before so please confirm I didn't do something boneheaded here

My problem statement is that my server (API Gateway) wont allow me to transmit my data in binary, so I had to base64 encode it which means that I need to tell y-quill to convert that data to unit8array/buffer -- I think it'd be helpful to future proof by allowing the user to provide a function that pre-processes their websocket events; with the default/fallback behavior of your typical processing (reading into a unit8array)

Huly®: YJS-730

EvanSchalton avatar Feb 09 '21 06:02 EvanSchalton

I don't like maintaining code separately so I instead hacked this by overwriting the onmessage with the following:

    this._provider.ws.onmessage = event => {
      this._provider.wsLastMessageReceived = time.getUnixTime()
      const eventData = new Uint8Array(base64ToArrayBuffer(event.data))
      const encoder = readMessage(this._provider, eventData, true)
      if (encoding.length(encoder) > 1) {
        this._provider.send(encoding.toUint8Array(encoder))
      }
    }

It isn't as clean a solution as it required me to rewrite the non-exported readMessage function also, but I wanted to share this incase it helps illustrate the intent

EvanSchalton avatar Feb 09 '21 07:02 EvanSchalton