0

This question is about converting a gzip deflate message from a websocket message and convert it to array OR raw text that I can apply JSON.parse on it...

*** to be also clear: In this question : i use a websocket from a crypto exchange.... but the question is about the received message NOT about crypto exchange

in the documentation they say "please use zlib deflate"

HERE THE JAVASCRIPT

digifinexopen = '{"id":12312,"method":"trades.subscribe","params":["btc_usdt"]}';
digifinex_market_ws = new WebSocket("wss://openapi.digifinex.com/ws/v1/"); 
digifinex_market_ws.binaryType = "arraybuffer";
digifinex_market_ws.onmessage = event => digifinex_trades(event.data);
digifinex_market_ws.onopen = event => digifinex_market_ws.send(digifinexopen);

function fu_bitmex_trades (jsonx) { console.log(jsonx); }

I have this in the log

object=>[[Int8Array]]: Int8Array(1129) 0 … 99]
0: 120
1: -38

I tried with <script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.0.4/pako.min.js" ...></script> if I do pako.deflate(jsonx); I get

object=> Uint8Array(78) [120, 156, 1, 67, 0, 188, 255, 120, 218, 4, 192, 177, 13, 196, 32, 12, 133, 225, 93, 254, 154, 6, 174, 243, 54, 39, 66, 17, 201, 74, 36, 63, 187, 66, 236, 158, 111, 179, 34, 222, 192, 158, 114, 111, 196, 82, 121, 98, 27, 229, 63, 75, 24, 170, 57, 151, 196, 105, 220, 23, 214, 199, 175, 143, 243, 5, 0, 0, 255, 255, 32, 108, 18, 108, 62, 68, 31, 

If I add decoder = new TextDecoder("utf8"); and log(decoder.decode(jsonx)); I get

string=> x�E��xڜ��n\7����'5���*
���$pƋ Ȼ�*�֋�g��#����|�����������������v\�//�_������������

but, HOW TO RETREIVE the array or raw data that I could json.parse ????

John R
  • 277
  • 3
  • 10

1 Answers1

1

If I decompress your data twice, I get:

{"error":null,"result":{"status":"success"},"id":12312}

It looks like you compressed instead of decompressed. Use pako.inflate().

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • I must be missing something, with `pako.inflate(jsonx)` I get `object=> Uint8Array(55) [123, 34, 101, 114, 114, 111, 114, 34, 58, 110, 117, 108, 108, 44, 34, 114, 101, 115, 117, 108, 116, 34, 58, 123, 34, 115, 116, 97, 116, 117, 115, 34, 58, 34, 115,` – John R Jun 17 '22 at 02:12
  • I modified `digifinex_market_ws.onmessage = event => digifinex_trades(new Uint8Array(event.data));` to get the Uint8Array and when I pass `new TextDecoder().decode(jsonx)` ==> it gives the stuff ������� and when I apply it like that : `pako.inflate(decoder.decode(jsonx))` I get `Uncaught unknown compression method` – John R Jun 17 '22 at 02:28
  • If (with `digifinex_trades(event.data)`) I do it twice like that `console.log(pako.inflate(pako.inflate(jsonx)));` I get `Uncaught incorrect header check` – John R Jun 17 '22 at 02:31
  • You keep cutting off your objects, but your first comment is exactly the start of the text in my answer (up through the first "s" in "success"). You did decompress it that time, and the result is indeed 55 bytes. – Mark Adler Jun 17 '22 at 02:32
  • I'm still confused.... what step do I do wrong ??? can you edit you answer with the full working code you did to get the raw json data ? – John R Jun 17 '22 at 02:42
  • You already got it. You do know that each integer corresponds to a character, right? 101 is "e", 114 is "r", ... – Mark Adler Jun 17 '22 at 02:48
  • Now I get it, but you confused me with "decompress your data twice"..... 1- I `pako.inflate(jsonx)` THEN I `decoder.decode(unint8inflated);` now it works – John R Jun 17 '22 at 02:48
  • I had to decompress it twice because _you_ compressed the already compressed data with `deflate`. It's right there in your question: "if I do `pako.deflate(jsonx);` I get" – Mark Adler Jun 17 '22 at 02:53