How to decode the y-websocket message

I am tried to decode the y-websocket message to plain text like this:

websocket.onmessage = (event) => {
      const data = new Uint8Array(
      const ss = Y.decodeSnapshot(data)
      console.warn('get decode msg:' + ss)
      console.warn('get event data:' +
      provider.wsLastMessageReceived = time.getUnixTime()
      const encoder = readMessage(provider, new Uint8Array(, true)
      if (encoding.length(encoder) > 1) {

Am I missing something? I also tried this:

wsProvider.on('message', (event: MessageEvent) => {
        const data: Uint8Array = new Uint8Array(;
        if (data[0] === 0) {
            const snapshot = Y.decodeSnapshot(data);
            // const snapshot = Y.decodeSnapshot(data.subarray(1))
            console.log('snapshot:' + JSON.stringify(snapshot));
        if (data[0] === 1) {
            const update = Y.decodeUpdateV2(data);
            console.log('update:' + JSON.stringify(update));

the decodeUpdateV2 shows error Uncaught Error: Unexpected end of array.

I think you want Y.decodeUpdate, not Y.decodeSnapshot.

Y.decodeSnapshot also shows error:

Item.js:703 Uncaught TypeError: contentRefs[(info & BITS5)] is not a function
    at readItemContent (Item.js:703:82)
    at lazyStructReaderGenerator (updates.js:72:11)
    at (<anonymous>)
    at (updates.js:107:28)
    at decodeUpdateV2 (updates.js:151:71)
    at Module.decodeUpdate (updates.js:140:41)
    at CollarEditorService.ts:113:30
    at observable.js:154:90
    at Array.forEach (<anonymous>)
    at WebsocketProvider.emit (observable.js:154:77)
(anonymous)	@	CollarEditorService.ts:113
Show 8 more frames

Sorry, I misread your message and thought you were trying to decode a raw update.

The y-websocket has its own binary messaging format. First the messageType is read (messageSync = 0, messageAwareness = 1). Then y-protocols/readSyncMessage is called and the rest of the message is decoded. So it is possible to decode the message yourself with a combination of y-protocols and lib0.

If possible, it would be much better to simply subscribe to the Doc and update messages.