I have a service that sends me data through WebSocket however the data is gzipped. when I try to ungziped the Blob data I have a problem. I used packages fflate gzip-js pako and zlib all with different error but the funny thing when I write the code for node js Server with package node-gzip which underhood used the zlib package mention above everything is fine and the data will be ungzped.
Client.js (Problem is unGzip data)
let ws = new WebSocket('wss://api.huobi.pro/ws');
var zlib = require('zlib');;
let zip = {
gzip: function gzip(input, options) {
return new Promise(function (resolve, reject) {
zlib.gzip(input, options, function (error, result) {
if (!error) resolve(result); else reject(Error(error));
});
});
},
ungzip: function ungzip(input, options) {
return new Promise(function (resolve, reject) {
zlib.gunzip(input, options, function (error, result) {
if (!error) resolve(result); else reject(Error(error));
});
});
}
};
ws.onopen = ( function () {
ws.send(`{
"sub": "market.ethbtc.kline.1min",
"id": "id1"
}`);
});
ws.onmessage = async function (event) {
console.log(await zip.ungzip(event.data));
};
ERROR : Uncaught (in promise) Error: TypeError: Invalid non-string/buffer chunk
Node.js Working
const {ungzip} = require('node-gzip');
ws = new WebSocket('wss://api.huobi.pro/ws');
ws.on('open', function () {
ws.send('{
"sub": "market.ethbtc.kline.1min",
"id": "id1"
}');
});
ws.on('message', function (data) {
ungzip(data).then((r) => {
let data = JSON.parse(r.toString());
console.log(data);
}).catch(console.error)
});
In the browser the event.data
is Blob data when I used the text
method on them there are some jibberish code and most of the library and packages above have said the data header is unknown
but why same data point with the same structure one working on node.js another not working in the browser
Please help me unzipped the data
sample response data after unGziped in Node.js code above I want the same in my Client.js
{
"ch": "market.ethbtc.kline.1min",
"ts": 1489474082831, //system update time
"tick": {
"id": 1489464480,
"amount": 0.0,
"count": 0,
"open": 7962.62,
"close": 7962.62,
"low": 7962.62,
"high": 7962.62,
"vol": 0.0
}
}