Package: gate-ws
Gate.io WebSocket Python SDKgate_wsprovides Gate.io WebSocket V4 Python implementation, including all channels defined in
spot(new) and futures WebSocket.Features:Fully asynchronousReconnect on connection to server lost, and resubscribe on connection recoveredSupport connecting to multiple websocket serversHighly configurableInstallationThis package requires Python version 3.6+. Python 2 will NOT be supported.pipinstall--usergate-wsGetting Startedimportasynciofromgate_wsimportConfiguration,Connection,WebSocketResponsefromgate_ws.spotimportSpotPublicTradeChannel# define your callback function on message receiveddefprint_message(conn:Connection,response:WebSocketResponse):ifresponse.error:print('error returned: ',response.error)conn.close()returnprint(response.result)asyncdefmain():# initialize default connection, which connects to spot WebSocket V4# it is recommended to use one conn to initialize multiple channelsconn=Connection(Configuration())# subscribe to any channel you are interested into, with the callback functionchannel=SpotPublicTradeChannel(conn,print_message)channel.subscribe(["GT_USDT"])# start the clientawaitconn.run()if__name__=='__main__':loop=asyncio.get_event_loop()loop.run_until_complete(main())loop.close()Application DemosWe provide some demo applications in theexamplesdirectory, which can be run directly.Advanced usageSubscribe to private channelsfromgate_wsimportConfiguration,Connectionfromgate_ws.spotimportSpotOrderChannelasyncdefmain():conn=Connection(Configuration(api_key='YOUR_API_KEY',api_secret='YOUR_API_SECRET'))channel=SpotOrderChannel(conn,lambdac,r:print(r.result))channel.subscribe(["GT_USDT"])# start the clientawaitconn.run()Your callback function can also be a coroutineimportasyncioasyncdefmy_callback(conn,response):awaitasyncio.sleep(1)print(response.result)You can provide a default callback function for all channels, so that when subscribing to new
channels, no additional callback function are needed.fromgate_wsimportConfiguration,Connectionfromgate_ws.spotimportSpotPublicTradeChannelasyncdefmain():# provide default callback for all channelsconn=Connection(Configuration(default_callback=lambdac,r:print(r.result)))# default callback will be used if callback not provided when initializing channelschannel=SpotPublicTradeChannel(conn)channel.subscribe(["GT_USDT"])# start the clientawaitconn.run()Subscribe to both spot and futures WebSocketsimportasynciofromgate_wsimportConfiguration,Connectionfromgate_ws.spotimportSpotPublicTradeChannelfromgate_ws.futuresimportFuturesPublicTradeChannelasyncdefmain():# initialize a spot connection, which is the default if no parameters is providedspot_conn=Connection(Configuration(app='spot'))# initialize a futures connectionfutures_conn=Connection(Configuration(app='futures',settle='usdt',test_net=False))# subscribe to any channel you are interested into, with the callback functionchannel=SpotPublicTradeChannel(spot_conn,lambdac,r:print(r.result))channel.subscribe(["BTC_USDT"])channel=FuturesPublicTradeChannel(futures_conn,lambdac,r:print(r.result))channel.subscribe(["BTC_USDT"])# start both connectionawaitasyncio.gather(spot_conn.run(),futures_conn.run())You can use your own executor pool to run your callback functionimportconcurrent.futuresfromgate_wsimportConfiguration,Connectionfromgate_ws.spotimportSpotPublicTradeChannelasyncdefmain():# use process pool to run your callback functionwithconcurrent.futures.ProcessPoolExecutor()aspool:conn=Connection(Configuration(executor_pool=pool))# default callback will be used if callback not provided when subscribingchannel=SpotPublicTradeChannel(conn,lambdac,r:print(r.result))channel.subscribe(["GT_USDT"])# start the clientawaitconn.run()