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

154
Views
Send websocket data to specific client

I made a websocket server using nodeJS, and my problem is as follow : two clients will connect to the server. When the client 1 send a data to the server, I want to forward it to client 2.

In order to do this, I took inspiration from this kind of solution :https://github.com/websockets/ws/issues/367

Looks great, but when I apply it, it gaves me the following error :

    TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received an instance of Object
    at new NodeError (node:internal/errors:371:5)
    at Function.from (node:buffer:323:9)
    at toBuffer (/home/ttt/node_modules/ws/lib/buffer-util.js:95:18)
    at Sender.send (/home/ttt/node_modules/ws/lib/sender.js:315:14)
    at WebSocket.send (/home/ttt/node_modules/ws/lib/websocket.js:467:18)
    at WebSocket.<anonymous> (/home/ttt/STT_project/sttproject/IHM_server/websocketServer.js:43:15)
    at WebSocket.emit (node:events:526:28)
    at Receiver.receiverOnMessage (/home/ttt/node_modules/ws/lib/websocket.js:1137:20)
    at Receiver.emit (node:events:526:28)
    at Receiver.dataMessage (/home/ttt/node_modules/ws/lib/receiver.js:528:14) 
  code: 'ERR_INVALID_ARG_TYPE'

I tried different methods, but the result remains the same. Does anyone can help me with this ? Or propose a better solution to my problem.

There is a sample of my code:

wss.on("connection", ws => {
    ws.id = id++;
    lookup[ws.id] = ws;

    ws.on("message", data => {
    if(data.toString() != "\n")
    {
      message = {
        "message": 
          {
            "text": data.toString(),
            "end": false
          }
      }
    }
    else {
      message = {
        "message": 
          {
            "text": "",
            "end": true
          }
      }
    }
    lookup[0].send('message');

    })
...
about 4 years ago · Santiago Gelvez
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!