bun icon indicating copy to clipboard operation
bun copied to clipboard

Websocket mixes up event.data

Open mynlexi opened this issue 2 years ago • 0 comments

What version of Bun is running?

0.5.6

What platform is your computer?

Darwin 22.1.0 arm64 arm

What steps can reproduce the bug?

we open connection from golang server from bun server we except a bunch of truncated json which we fuse together after last loop

var ws = new WebSocket(`${process.env.BACKEND_LOCATION_WS}servus_bun`);

ws.onopen = (event)=>{
    ws.send("init start")
}

ws.onmessage = async (event) => {
        if (dataIncoming) {
            counts += 1

            pipedJson = pipedJson.concat(event.data) // ||   pipedJson += event.data;
   
            if (counts-1  == countShould) {
                insertJsonToDatabasesEspresso(pipedJson, wspId)
                    .then((db_id) => console.log("set DB for WorkspaceID " + db_id))
                    .then(() => {
                        dataIncoming = false
                        ws.send("next")
                    })
            } else {
                ws.send('iterate')
            }
// inti code
}

our two solutions : 1)

             await Bun.write('storage.txt', event.data)
             let msg = await Bun.file('storage.txt').text()
            jsonData += msg
           console.assert(pipedJson.endsWith("") )

What is the expected behavior?

json parsing works

What do you see instead?

looking into the string(writing it into a file we see the last json part, ends with ], multiple times inserted, instead of the correct string. console.log(event.data) logs the correct slice data += event.data adds the wrong slice

51 | export async function insertJsonToDatabasesEspresso(message: string, wspId: number,) {
52 | 
53 |     // console.log(message)
54 | 
55 | 
56 |     let json = JSON.parse(message)
                   ^
SyntaxError: JSON Parse error: Expected ':' before value in object property definition

Additional information

we split json by bytes on golang side (4096 bytes) we choose to send 'iterate' to make sure its not another issue from pumping too much

the other direction, if golang opens the connection works good (bao.js implementation) (but we have loadbalancer in between, so we cant ping the container on startup)

mynlexi avatar Feb 23 '23 14:02 mynlexi