admin管理员组

文章数量:1293366

I'm using Node and the ws npm package to work with WebSockets. Got the listenKey as stated in the docs (below), but I'm unable to get my account info using User Data Stream. I'd prefer to use a stream to read my most current account info (balances, etc) since using the Rest API to do it incurs a penalty (WEIGHT: 5) each time.

I've tried doing ws.send('outboundAccountInfo') but no joy.

DOCS: .md

Full code example - does not return any data:

import request from 'request'
import WebSocket from 'ws'

import { API_KEY } from '../../assets/secrets'


const DATA_STREAM_ENDPOINT = 'wss://stream.binance:9443/ws'
const BINANCE_API_ROOT = ''
const LISTEN_KEY_ENDPOINT = `${BINANCE_API_ROOT}/api/v1/userDataStream`

const fetchAccountWebsocketData = async() => { 
  const listenKey = await fetchListenKey()

  console.log('-> ', listenKey) // valid key is returned

  let ws

  try {
    ws = await openWebSocket(`${DATA_STREAM_ENDPOINT}/${listenKey}`)
  } catch (err) {
    throw(`ERROR - fetchAccountWebsocketData: ${err}`)
  }

  // Nothing returns from either
  ws.on('message', data => console.log(data))
  ws.on('outboundAccountInfo', accountData => console.log(accountData))
}

const openWebSocket = endpoint => {
  const p = new Promise((resolve, reject) => {
    const ws = new WebSocket(endpoint)

    console.log('\n-->> New Account Websocket')

    ws.on('open', () => {
      console.log('\n-->> Websocket Account open...')
      resolve(ws)
    }, err => { 
      console.log('fetchAccountWebsocketData error:', err)
      reject(err) 
    })
  })

  p.catch(err => console.log(`ERROR - fetchAccountWebsocketData: ${err}`))
  return p
}

const fetchListenKey = () => {
  const p = new Promise((resolve, reject) => {
    const options = {
      url: LISTEN_KEY_ENDPOINT, 
      headers: {'X-MBX-APIKEY': API_KEY}
    }

    request.post(options, (err, httpResponse, body) => {
      if (err) 
        return reject(err)

      resolve(JSON.parse(body).listenKey)
    })
  })

  p.catch(err => console.log(`ERROR - fetchListenKey: ${err}`))
  return p
}

export default fetchAccountWebsocketData

I'm using Node and the ws npm package to work with WebSockets. Got the listenKey as stated in the docs (below), but I'm unable to get my account info using User Data Stream. I'd prefer to use a stream to read my most current account info (balances, etc) since using the Rest API to do it incurs a penalty (WEIGHT: 5) each time.

I've tried doing ws.send('outboundAccountInfo') but no joy.

DOCS: https://github./binance-exchange/binance-official-api-docs/blob/master/user-data-stream.md

Full code example - does not return any data:

import request from 'request'
import WebSocket from 'ws'

import { API_KEY } from '../../assets/secrets'


const DATA_STREAM_ENDPOINT = 'wss://stream.binance.:9443/ws'
const BINANCE_API_ROOT = 'https://api.binance.'
const LISTEN_KEY_ENDPOINT = `${BINANCE_API_ROOT}/api/v1/userDataStream`

const fetchAccountWebsocketData = async() => { 
  const listenKey = await fetchListenKey()

  console.log('-> ', listenKey) // valid key is returned

  let ws

  try {
    ws = await openWebSocket(`${DATA_STREAM_ENDPOINT}/${listenKey}`)
  } catch (err) {
    throw(`ERROR - fetchAccountWebsocketData: ${err}`)
  }

  // Nothing returns from either
  ws.on('message', data => console.log(data))
  ws.on('outboundAccountInfo', accountData => console.log(accountData))
}

const openWebSocket = endpoint => {
  const p = new Promise((resolve, reject) => {
    const ws = new WebSocket(endpoint)

    console.log('\n-->> New Account Websocket')

    ws.on('open', () => {
      console.log('\n-->> Websocket Account open...')
      resolve(ws)
    }, err => { 
      console.log('fetchAccountWebsocketData error:', err)
      reject(err) 
    })
  })

  p.catch(err => console.log(`ERROR - fetchAccountWebsocketData: ${err}`))
  return p
}

const fetchListenKey = () => {
  const p = new Promise((resolve, reject) => {
    const options = {
      url: LISTEN_KEY_ENDPOINT, 
      headers: {'X-MBX-APIKEY': API_KEY}
    }

    request.post(options, (err, httpResponse, body) => {
      if (err) 
        return reject(err)

      resolve(JSON.parse(body).listenKey)
    })
  })

  p.catch(err => console.log(`ERROR - fetchListenKey: ${err}`))
  return p
}

export default fetchAccountWebsocketData
Share edited Dec 24, 2018 at 1:47 Ben asked Mar 4, 2018 at 23:39 BenBen 5,43210 gold badges44 silver badges60 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Was stuggling too .... for hours !!!

https://www.reddit./r/BinanceExchange/ments/a902cq/user_data_streams_has_anyone_used_it_successfully/

The binance user data stream doesn't return anything when you connect to it, only when something changes in your account. Try running your code, then go to binance and place an order in the book, you should see some data show up*

本文标签: javascriptBINANCE APIHow to get Account info with User Data StreamStack Overflow