Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Implement JavaScript Buffer to store data

I receive these huge Strings via WebSocket:

[
  "BTC-31DEC21-100000-P",
  "{\"data\":{\"bids\":{\"0.01\":{\"price\":0.01,\"volume\":66.2,\"exchange\":\"DER\"},\"5.0E-4\":{\"price\":5.0E-4,\"volume\":1.1,\"exchange\":\"DER\"},\"0.637\":{\"price\":0.637,\"volume\":8.4,\"exchange\":\"DER\"}},\"asks\":{\"0.664\":{\"price\":0.664,\"volume\":8.4,\"exchange\":\"DER\"}}},\"isMasterFrame\":true}"
]

or

[
  "BTC-31DEC21-36000-C",
  "{\"data\":[{\"price\":0.422,\"volume\":8.4,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.423,\"volume\":0.0,\"exchange\":\"DER\",\"side\":\"ASKS\"}],\"isMasterFrame\":false}"
]

or

[
  "BTC-31DEC21-60000-P",
  "{\"data\":[{\"price\":0.105,\"volume\":0.0,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.1055,\"volume\":28.7,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.106,\"volume\":7.6,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.1065,\"volume\":43.0,\"exchange\":\"DER\",\"side\":\"ASKS\"}],\"isMasterFrame\":false}"
]

I want to make JavaScript Buffer where I can store the data and reset it if I receive JSON with isMasterFrame = true

let payload = JSON.parse(messageString[1]);

if (payload.hasOwnProperty("isMasterFrame")) {
  for (let i = 0; i < payload.pairs.length; i++) {
    let currentPair = payload.data[i]
    currentPair = currentPair.replace(/\0/g, ''); //Remove null chars
    if (currentPair.toUpperCase() != 'KILL') {
      // reset the buffer and start again to fill data
    }
  }
} else {
  // if we receive "isMasterFrame":false just update the data without reset
}

What are the available options to implement this buffer?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think this is what you are after. It caches the details until it finds isMasterFrame: true && KILL, at which time it clears the cache and starts over again.

EDIT: Stored data using BTC style string as key and data as value to enable querying cache.

let messageCache = {};
const messages = [
  ["BTC-31DEC21-36000-C", "{\"data\":[{\"price\":0.422,\"volume\":8.4,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.423,\"volume\":0.0,\"exchange\":\"DER\",\"side\":\"ASKS\"}],\"isMasterFrame\":false}"],
  ["BTC-31DEC21-36000-D", "{\"data\":[{\"price\":0.422,\"volume\":8.4,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.423,\"volume\":0.0,\"exchange\":\"DER\",\"side\":\"ASKS\"}],\"isMasterFrame\":false}"],
  ["BTC-31DEC21-60000-P", "{\"data\":[{\"price\":0.105,\"volume\":0.0,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.1055,\"volume\":28.7,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.106,\"volume\":7.6,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.1065,\"volume\":43.0,\"exchange\":\"DER\",\"side\":\"KILL\"}],\"isMasterFrame\"\:true}"],
  ["BTC-31DEC21-60000-Q", "{\"data\":[{\"price\":0.105,\"volume\":0.0,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.1055,\"volume\":28.7,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.106,\"volume\":7.6,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.1065,\"volume\":43.0,\"exchange\":\"DER\",\"side\":\"ASKS\"}],\"isMasterFrame\"\:false}"]
];

function processData(key, payload) {
  if (!payload.isMasterFrame) {
    messageCache[key] = payload; 
    return;
  }
  for (let obj of payload.data) {
    Object.values(obj).forEach(item => {
      item = item.toString().replace(/\0/g, ''); //Remove null chars
      if (item.toUpperCase() !== 'KILL') {
        messageCache = {}; // Clear the cache/buffer
      }
    });
  }
}

function queryCache(key){
   return messageCache[key];
}

messages.forEach(message => {
  const payload = JSON.parse(message[1]);
  processData(message[0], payload);
  console.log("Number of cached messages: " + Object.keys(messageCache).length);
});

console.log('Query Cache [BTC-31DEC21-60000-Q]:');
let result = queryCache('BTC-31DEC21-60000-Q');
console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!