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

206
Views
How Do I handle receiving multiple WebSocket onopen & onmessage Using JavaScript?

I Am New to Coding and Learning to start small Projects to help me improve and learn-

I wanted to get Different WebSocket feed simultaneously in the same Script- response for both granularity for 300 & 900 at the same time; below is the actual code for a single request

const WebSocket = require('ws');
var ws = new WebSocket('wss://ws.binaryws.com/websockets/v3?app_id=1089');


ws.onopen = function(evt) {
   
    ws.send(JSON.stringify(
    {ticks_history: "R_25",
     subscribe: 1,
     adjust_start_time: 1,
     count: 1,
     end: "latest",
     granularity: 300,
     start: 1,
     style: "candles"}
     
    ));

};

ws.onmessage = function(msg) {
   var data = JSON.parse(msg.data);
   console.log('ticks update: %o', data.tick.quote);

};

Where by I can get different granularity response of same feed at the same time, although I have been able to get this independently but would love to be able to get them all from the same script!

Below is my script

const WebSocket = require('ws');
var ws = new WebSocket('wss://ws.binaryws.com/websockets/v3?app_id=1089');


ws.onopen = function(evt) {
   
    ws.send(JSON.stringify(
    {ticks_history: "R_25",
     subscribe: 1,
     adjust_start_time: 1,
     count: 1,
     end: "latest",
     granularity: 900,
     start: 1,
     style: "candles"}, 

//here is where i added the additional request for 900 time frame

     {ticks_history: "R_25",
     subscribe: 1,
     adjust_start_time: 1,
     count: 1,
     end: "latest",
     granularity: 300,
     start: 1,
     style: "candles"}
     
    ));

};

ws.onmessage = function(msg) {
   var data = JSON.parse(msg.data);
   console.log('ticks update: %o', data);

};

from the above script, I only get a data feed from only the second request -900

How do I write this that will allow me to get both data responses same time?

about 4 years ago · Juan Pablo Isaza
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!