When i connect binance websocket in nodejs gived error. How i can fix this

68 Views Asked by At
const WebSocket = require('ws');

const ws = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@trade');

ws.on('message', function incoming(data) {
    console.log(data);
});

this is my code when i run this gived error

node:events:493
      throw er; // Unhandled 'error' event
      ^

Error: write EPROTO 885B0000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:355:

    at WriteWrap.onWriteComplete [as oncomplete] (node:internal/stream_base_commons:94:16)
Emitted 'error' event on WebSocket instance at:
    at emitErrorAndClose (C:\Users\uchun\node_modules\ws\lib\websocket.js:1033:13)
    at ClientRequest.<anonymous> (C:\Users\uchun\node_modules\ws\lib\websocket.js:880:5)
    at ClientRequest.emit (node:events:515:28)
    at TLSSocket.socketErrorListener (node:_http_client:495:9)
    at TLSSocket.emit (node:events:515:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -4046,
  code: 'EPROTO',
  syscall: 'write'
}

Node.js v21.1.0

i trying everything but not result

1

There are 1 best solutions below

0
Mayank Singh On

First update the websocket and try this:

const ws = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@trade',{
  rejectUnauthorized: false, 
  secure_protocol: 'TLSv1_2_method'
});