binance / binance-websocket-examples

Example code in Nodejs that demonstrate how to subscribe to Binance Websocket server.
MIT License
154 stars 52 forks source link

socketClient.js returning wrong part of message payload #25

Closed pevet closed 3 years ago

pevet commented 3 years ago

src/lib/socketClient.js line 38: const message = JSON.parse(msg.data);

returns the whole message payload, not the data part of the payload. So when you run it, on each message received you get an “Unprocessed method” error. It should be something like:

const payload = JSON.parse(msg.data); const message = payload.data;

I also suggest to change the error message text on line 48 to “Unknown method” to distinguish it from the message on line 45.

Best, Peter

ishuen commented 3 years ago

Hi. Could you provide an example message payload for this?

const payload = JSON.parse(msg.data);
const message = payload.data;

For example, orderbook's message (i.e. msg.data) object is in the form of the following. It does not have another data property inside. So it makes more sense to be const message = JSON.parse(msg.data);.

{
  e: 'depthUpdate',
  E: 1635212586209,
  s: 'BTCUSDT',
  U: 14590880624,
  u: 14590880705,
  b: [
    [ '62797.02000000', '0.66271000' ],
    [ '62795.33000000', '0.00000000' ],
    ...
    [ '62557.12000000', '0.00032000' ]
  ],
  a: [
    [ '62797.03000000', '2.18598000' ],
    [ '62799.25000000', '0.00000000' ],
    ...
    [ '64717.23000000', '0.00000000' ]
  ]
}

Also, thanks for suggestion. Error message unknown method is easier to understand. We will refactor the method a bit and apply the change accordingly.

pevet commented 3 years ago

Hi, I see you updated the error message already, great! Looking into the messages - it looks like it depends on the stream. For example, in your multi-stream example, the message handler is not recognised (your standard repo code, only change - outputting msg as well) there is no message.e:

2021-10-26T11:31:14.769Z WARN Unknown method

message={

stream: @.***',

data: {

lastUpdateId: 4370093361,

bids: [ [Array], [Array], [Array], [Array], [Array] ],

asks: [ [Array], [Array], [Array], [Array], [Array] ]

}

}

2021-10-26T11:31:14.770Z WARN Unknown method

msg=MessageEvent {

target: <ref *1> WebSocket {

_events: [Object: null prototype] {

  open: [Function],

  pong: [Function (anonymous)],

  ping: [Function (anonymous)],

  close: [Function],

  error: [Function],

  message: [Function]

},

_eventsCount: 6,

_maxListeners: undefined,

_binaryType: 'nodebuffer',

_closeCode: 1006,

_closeFrameReceived: false,

_closeFrameSent: false,

_closeMessage: '',

_closeTimer: *null*,

_extensions: { 'permessage-deflate': [PerMessageDeflate] },

_protocol: '',

_readyState: 1,

_receiver: Receiver {

  _writableState: [WritableState],

  _events: [Object: null prototype],

  _eventsCount: 6,

  _maxListeners: undefined,

  _binaryType: 'nodebuffer',

  _extensions: [Object],

  _isServer: false,

  _maxPayload: 104857600,

  _bufferedBytes: 0,

  _buffers: [],

  _compressed: true,

  _payloadLength: 174,

  _mask: undefined,

  _fragmented: 0,

  _masked: false,

  _fin: true,

  _opcode: 1,

  _totalPayloadLength: 0,

  _messageLength: 0,

  _fragments: [],

  _state: 5,

  _loop: false,

  [Symbol(kCapture)]: false,

  [Symbol(websocket)]: [Circular *1]

},

_sender: Sender {

  _extensions: [Object],

  _socket: [TLSSocket],

  _firstFragment: true,

  _compress: false,

  _bufferedBytes: 0,

  _deflating: false,

  _queue: []

},

_socket: TLSSocket {

  _tlsOptions: [Object],

  _secureEstablished: true,

  _securePending: false,

  _newSessionPending: false,

  _controlReleased: true,

  secureConnecting: false,

  _SNICallback: *null*,

  servername: 'stream.binance.com',

  alpnProtocol: false,

  authorized: true,

  authorizationError: *null*,

  encrypted: true,

  _events: [Object: null prototype],

  _eventsCount: 6,

  connecting: false,

  _hadError: false,

  _parent: *null*,

  _host: 'stream.binance.com',

  _readableState: [ReadableState],

  _maxListeners: undefined,

  _writableState: [WritableState],

  allowHalfOpen: false,

  _sockname: *null*,

  _pendingData: *null*,

  _pendingEncoding: '',

  server: undefined,

  _server: *null*,

  ssl: [TLSWrap],

  _requestCert: true,

  _rejectUnauthorized: true,

  parser: *null*,

  _httpMessage: *null*,

  timeout: 0,

  [Symbol(res)]: [TLSWrap],

  [Symbol(verified)]: true,

  [Symbol(pendingSession)]: *null*,

  [Symbol(async_id_symbol)]: 9,

  [Symbol(kHandle)]: [TLSWrap],

  [Symbol(kSetNoDelay)]: true,

  [Symbol(lastWriteQueueSize)]: 0,

  [Symbol(timeout)]: *null*,

  [Symbol(kBuffer)]: *null*,

  [Symbol(kBufferCb)]: *null*,

  [Symbol(kBufferGen)]: *null*,

  [Symbol(kCapture)]: false,

  [Symbol(kBytesRead)]: 0,

  [Symbol(kBytesWritten)]: 0,

  [Symbol(connect-options)]: [Object],

  [Symbol(RequestTimeout)]: undefined,

  [Symbol(websocket)]: [Circular *1]

},

_bufferedAmount: 0,

_isServer: false,

_redirects: 0,

_url: 'wss://

@.**@*.**@*.***' ,

_req: *null*,

[Symbol(kCapture)]: false

},

type: 'message',

data: @.*** ","data":{"lastUpdateId":4370093361,"bids":[["0.06706600","4.09190000"],["0.06706400","0.22710000"],["0.06706300","1.30000000"],["0.06706000","0.02980000"],["0.06705900","7.30190000"]],"asks":[["0.06706700","2.47300000"],["0.06706800","0.76720000"],["0.06706900","4.91640000"],["0.06707200","3.77070000"],["0.06707300","1.11010000"]]}}'

}

If you change the stream to @ticker on line 15 of multi-stream-depth.js, the output is:

2021-10-26T11:34:21.208Z WARN Unknown method

message = {

stream: @.***',

data: {

e: '24hrTicker',

E: 1635248061082,

s: 'LTCBTC',

p: '-0.00000700',

P: '-0.225',

w: '0.00310778',

x: '0.00311100',

c: '0.00310500',

Q: '0.07500000',

b: '0.00310400',

B: '2.82100000',

a: '0.00310500',

A: '28.33800000',

o: '0.00311200',

h: '0.00315000',

l: '0.00307900',

v: '69995.64200000',

q: '217.53100027',

O: 1635161660881,

C: 1635248060881,

F: 71756063,

L: 71783312,

n: 27250

}

}

2021-10-26T11:34:21.208Z WARN Unknown method

msg=MessageEvent {

target: <ref *1> WebSocket {

_events: [Object: null prototype] {

  open: [Function],

  pong: [Function (anonymous)],

  ping: [Function (anonymous)],

  close: [Function],

  error: [Function],

  message: [Function]

},

_eventsCount: 6,

_maxListeners: undefined,

_binaryType: 'nodebuffer',

_closeCode: 1006,

_closeFrameReceived: false,

_closeFrameSent: false,

_closeMessage: '',

_closeTimer: *null*,

_extensions: { 'permessage-deflate': [PerMessageDeflate] },

_protocol: '',

_readyState: 1,

_receiver: Receiver {

  _writableState: [WritableState],

  _events: [Object: null prototype],

  _eventsCount: 6,

  _maxListeners: undefined,

  _binaryType: 'nodebuffer',

  _extensions: [Object],

  _isServer: false,

  _maxPayload: 104857600,

  _bufferedBytes: 0,

  _buffers: [],

  _compressed: true,

  _payloadLength: 228,

  _mask: undefined,

  _fragmented: 0,

  _masked: false,

  _fin: true,

  _opcode: 1,

  _totalPayloadLength: 0,

  _messageLength: 0,

  _fragments: [],

  _state: 5,

  _loop: false,

  [Symbol(kCapture)]: false,

  [Symbol(websocket)]: [Circular *1]

},

_sender: Sender {

  _extensions: [Object],

  _socket: [TLSSocket],

  _firstFragment: true,

  _compress: false,

  _bufferedBytes: 0,

  _deflating: false,

  _queue: []

},

_socket: TLSSocket {

  _tlsOptions: [Object],

  _secureEstablished: true,

  _securePending: false,

  _newSessionPending: false,

  _controlReleased: true,

  secureConnecting: false,

  _SNICallback: *null*,

  servername: 'stream.binance.com',

  alpnProtocol: false,

  authorized: true,

  authorizationError: *null*,

  encrypted: true,

  _events: [Object: null prototype],

  _eventsCount: 6,

  connecting: false,

  _hadError: false,

  _parent: *null*,

  _host: 'stream.binance.com',

  _readableState: [ReadableState],

  _maxListeners: undefined,

  _writableState: [WritableState],

  allowHalfOpen: false,

  _sockname: *null*,

  _pendingData: *null*,

  _pendingEncoding: '',

  server: undefined,

  _server: *null*,

  ssl: [TLSWrap],

  _requestCert: true,

  _rejectUnauthorized: true,

  parser: *null*,

  _httpMessage: *null*,

  timeout: 0,

  [Symbol(res)]: [TLSWrap],

  [Symbol(verified)]: true,

  [Symbol(pendingSession)]: *null*,

  [Symbol(async_id_symbol)]: 9,

  [Symbol(kHandle)]: [TLSWrap],

  [Symbol(kSetNoDelay)]: true,

  [Symbol(lastWriteQueueSize)]: 0,

  [Symbol(timeout)]: *null*,

  [Symbol(kBuffer)]: *null*,

  [Symbol(kBufferCb)]: *null*,

  [Symbol(kBufferGen)]: *null*,

  [Symbol(kCapture)]: false,

  [Symbol(kBytesRead)]: 0,

  [Symbol(kBytesWritten)]: 0,

  [Symbol(connect-options)]: [Object],

  [Symbol(RequestTimeout)]: undefined,

  [Symbol(websocket)]: [Circular *1]

},

_bufferedAmount: 0,

_isServer: false,

_redirects: 0,

_url: 'wss://

@.**@*.**@*.***' ,

_req: *null*,

[Symbol(kCapture)]: false

},

type: 'message',

data: @.*** ","data":{"e":"24hrTicker","E":1635248061082,"s":"LTCBTC","p":"-0.00000700","P":"-0.225","w":"0.00310778","x":"0.00311100","c":"0.00310500","Q":"0.07500000","b":"0.00310400","B":"2.82100000","a":"0.00310500","A":"28.33800000","o":"0.00311200","h":"0.00315000","l":"0.00307900","v":"69995.64200000","q":"217.53100027","O":1635161660881,"C":1635248060881,"F":71756063,"L":71783312,"n":27250}}'

}

Here the message data is 1 level deeper, so even if you register the correct handler (24HrTicker), it fails. To work correctly, it needs message = msg.data.data (I'm using this stream, that's why I proposed to change the message code...). But looking at it now, it's not that straightforward. The message data is inconsistent - each stream returns a different format. @depth contains no message type (.e) at all, with @ticker the data part is 1 level deeper. Did not test other streams though, as I need just @ticker for the project I'm working on now. Best, Peter

On Tue, Oct 26, 2021 at 3:55 AM ishuen @.***> wrote:

Hi. Could you provide an example message payload for this?

const payload = JSON.parse(msg.data); const message = payload.data;

For example, orderbook's message (i.e. msg.data) object is in the form of the following. It does not have another data property inside. So it makes more sense to be const message = JSON.parse(msg.data);.

{ e: 'depthUpdate', E: 1635212586209, s: 'BTCUSDT', U: 14590880624, u: 14590880705, b: [ [ '62797.02000000', '0.66271000' ], [ '62795.33000000', '0.00000000' ], ... [ '62557.12000000', '0.00032000' ] ], a: [ [ '62797.03000000', '2.18598000' ], [ '62799.25000000', '0.00000000' ], ... [ '64717.23000000', '0.00000000' ] ] }

Also, thanks for suggestion. Error message unknown method is easier to understand. We will refactor the method a bit and apply the change accordingly.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/binance/binance-websocket-examples/issues/25#issuecomment-951482984, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFM6R5LURVP3RNT5UUAVIHLUIYDBRANCNFSM5GVK26EQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

ishuen commented 3 years ago

Thanks for the response. We will figure out a way to handle it gracefully, thanks.