gateio / gatews

Gate Websocket V4 SDK
89 stars 35 forks source link

subscribe all user data events in one ws connection #11

Closed ioogle closed 3 years ago

ioogle commented 3 years ago

Thanks for your hard work! But there are two critical problems that I found in your futures API:

1. Some connections are contract specific which is inconvenient

For example, for User Trades API, the connection is contract specific:

from websocket import create_connection
ws = create_connection("wss://fx-ws-testnet.gateio.ws/v4/ws/btc")
ws.send('{
    "time" : 123456,
    "channel" : "futures.usertrades",
    "event": "subscribe",
    "payload" : ["20011", "BTC_USD"],
    "auth": {
            "method": "api_key",
            "KEY":"xxxx",
            "SIGN": "xxxx"
            }}')
print(ws.recv())

which means that N connections are needed if we want to receive notifications from N contracts.

2. If all user-specific data can be integrated with one connection, it will be much better

As far as I know, Binance provides a single Websocket endpoint User Data Stream to receive all user updates, and that's really awesome.

revilwang commented 3 years ago

Actually there are examples that you can subscribe to multiple channels with multiple contracts using one connection

revilwang commented 3 years ago

For example

from gate_ws import Configuration, Connection
from gate_ws.futures import FuturesUserTradesChannel, FuturesBalanceChannel

async def main():
    conn = Connection(Configuration(api_key='YOUR_API_KEY',
                                    api_secret='YOUR_API_SECRET', 
                                    default_callback=lambda c, r: print(r.result),
                                    app='futures', settle='usdt')
    balance_channel = FuturesBalanceChannel(conn)
    balance_channel.subscribe(["BTC_USDT", "ETH_USDT"])

    user_trade_channel = FuturesUserTradesChannel(conn)
    user_trade_channel.subscribe(["YOUR_USER_ID", "BTC_USDT"])
    user_trade_channel.subscribe(["YOUR_USER_ID", "ETH_USDT"])

    # start the client
    await conn.run()
ioogle commented 3 years ago

Thanks. But considering that there are around 7-8 user-specific endpoints that are also contract specific. If I want to monitor all of the contracts, there might be hundreds of connections which may be unacceptable.

revilwang commented 3 years ago

The example above uses only one connection to subscribe to multiple channels with multiple contracts. Even for contract specific channels, you can call subscribe multiples times with different contracts. All contracts' updates will be returned in one connection.

ioogle commented 3 years ago

That's really cool. Would you mind providing an example for Golang?

revilwang commented 3 years ago

The example in quick start shows how to do so

ioogle commented 3 years ago

Thank you again for your patient reply:)