zhouaini528 / binance-php

Binance API Like the official document interface, Support for arbitrary extension.
MIT License
107 stars 43 forks source link

User Data Streams #15

Closed mohammad-rj closed 3 years ago

mohammad-rj commented 3 years ago

can u plz add an example of User Data Streams

zhouaini528 commented 3 years ago

User Data Streams

$binance=new Binance($key,$secret);
try {
    $result=$binance->user()->postUserDataStream();
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$binance->user()->deleteUserDataStream();
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}

try {
    $result=$binance->user()->putUserDataStream([
'listenKey'=>'xxxxxxx'
]);
    print_r($result);
}catch (\Exception $e){
    print_r(json_decode($e->getMessage(),true));
}
mohammad-rj commented 3 years ago

tanks for answering

postUserDataStream just give me a [listenKey] => xxx i can Close this with deleteUserDataStream and Keepalive with putUserDataStream base on what i read in binance doc but do not know how i can get data in Streams way ?

like when i run client_usdt_future.php file in ./binance/tests/websocket and get price with websocket every 0.1 s , how i can get user data update like that (I mean in real time)

and also i need this feature USER_STREAM on futures market https://binance-docs.github.io/apidocs/futures/en/#user-data-streams and what u mentioned kindly is in the spot

zhouaini528 commented 3 years ago

Here is an example

When the private channel is successfully established, all user data will be subscribed by default.

For the type of private channel, the queue data is saved 100 by default.

$binance->config([
    //Do you want to enable local logging,default false
    'log'=>['filename'=>'spot'],

    //Daemons address and port,default 0.0.0.0:2208
    'global'=>'127.0.0.1:22088',

    //Number of messages WS queue shuold hold, default 100
    'queue_count'=>100,

    //baseurl
    'baseurl'=>'ws://stream.binance.com:9443',
]);

$binance->keysecret([
    'key'=>'xxxxxxxxx',
    'secret'=>'xxxxxxxxx',
]);

//Subscribe to all private channels by default
$binance->subscribe();

This question may help you

mohammad-rj commented 3 years ago

thank you very much for your response

I tested several models. I will summarize the result here. Maybe it will help to understand others faster. (Or if you misunderstand, let me know)

we have 3 base url for 3 market type [spot, usdt future, coin future] 'baseurl'=>'ws://stream.binance.com:9443',//default //spot 'baseurl'=>'ws://fstream.binance.com', //usdt future 'baseurl'=>'ws://dstream.binance.com', //coin future

we must first start "websocket server daemon" (channel) (we can see listen on public or private data) (we can see listen on private data only when we subscribe with our key and secret api keys)

after that we must run a client daemon to start getinge data When the private channel is successfully established, all user data will be subscribed by default.

noob fact: with ws://stream.binance.com:9443 we can just get stop market data and with ws://fstream.binance.com we can just get usdt future market data