-
Does anybody have any basic example on how to use uWS with avsc (https://github.com/mtth/avsc) ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Packing binary data was discussed here #501 (comment) with example using avsc as part of benchmark. Ideally do custom binary encode but if you need something to help you can do // encode data benchmark
const data={field1:true,field2:false,field3:3,field4:15}
const msgpack=require('@msgpack/msgpack')
const avscSchema=require('avsc').Type.forValue(data)
const packBytes=(...bitsVals)=>bitsVals.reduce((byte,val,i)=>i%2?byte+val:byte<<val,0)>>>0
const encodeFns={
packBytes:data=>{
const buf=Buffer.allocUnsafe(1)
buf.writeUInt8(packBytes(1,data.field1,1,data.field2,2,data.field3,4,data.field4))
return buf
},
avsc:data=>avscSchema.toBuffer(data),
msgpack:data=>msgpack.encode(data),
json:data=>Buffer.from(JSON.stringify(data)),
}
// node benchmark.js [encodings=all] [iterations=2_000_000]
const encodings=process.argv[2]&&process.argv[2]!='all'?[process.argv[2]]:Object.keys(encodeFns)
const iterations=process.argv[3]||2_000_000
encodings.forEach(type=>{
let i=iterations,time=process.hrtime.bigint()
while(i--)encodeFns[type](data)
console.log(`${type} encode time(ns): ${Math.floor(Number(process.hrtime.bigint()-time)/iterations)} - bytes: ${encodeFns[type](data).length}`)
})
|
Beta Was this translation helpful? Give feedback.
-
Just a side note - encoding/decoding your application's data has no relation to uWS. It is entirely disjoint, isolated from uWS. |
Beta Was this translation helpful? Give feedback.
Packing binary data was discussed here #501 (comment) with example using avsc as part of benchmark. Ideally do custom binary encode but if you need something to help you can do
avscSchema=avsc.Type.forValue(data)
andbuffer=avscSchema.toBuffer(data)
ws.send(buffer,true)