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

157
Views
How to receive data on two different ports of websockets in Javascript?

I've currently initialized two different websockets with different ports like this:

const socket_ch1 = new WebSocket(WEB_socket_URL_CH1)
const socket_ch2 = new WebSocket(WEB_socket_URL_CH2)

socket_ch1.onopen = (event) => {
  console.warn('websocket_ch1 connection established', event)
}

socket_ch2.onopen = (event) => {
  console.warn('websocket_ch2 connection established', event)
}

Then, I've pushed data to a logic I've implemented upon incoming data like this.

socket_ch1.onmessage = async (event) => {
  console.log( 'onmessage Ch1: ' + flag)

  const parsedMessage = JSON.parse(event.data)
  const keys = Object.keys(parsedMessage)

  prpdch1.streamPush(parsedMessage[keys][0])
  prpsch1.streamPush(parsedMessage[keys][0])
}

socket_ch2.onmessage = async (event) => {
  console.log( 'onmessage Ch2: ' + flag)

  const parsedMessage = JSON.parse(event.data)
  const keys = Object.keys(parsedMessage)
  prpsch2.streamPush(parsedMessage[keys][0])
  prpdch2.streamPush(parsedMessage[keys][0])
}

streamPush() logic has been implemented as such :

streamPush( samples ){
    console.log('prps streamPush : ' + this.name)
    console.log(samples)


    if(samples === undefined) return

    let temp = []

    // 7680 >> [128]
    while(samples.length > 0 ) {
        var cycle = samples.splice(0, this.dataSampleSize)
        temp.push(cycle)
    }


    if(temp.length > 0)
        this.databuf = this.databuf.concat(temp)

}

However, it seems data only come through one channel, and I don't know why. The following is the debugging console.

enter image description here

I think there is a problem with how I'm using the websocket. Can anyone spot the problem?

Thanks in advance!

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!