You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 16, 2026. It is now read-only.
gate_ws provides Gate WebSocket V4 Python implementation, including all channels defined in
spot(new) and futures WebSocket.
Features:
Fully asynchronous
Reconnect on connection to server lost, and resubscribe on connection recovered
Support connecting to multiple websocket servers
Highly configurable
Installation
This package requires Python version 3.6+. Python 2 will NOT be supported.
pip install --user gate-ws
Getting Started
importasynciofromgate_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 Demos
We provide some demo applications in the examples directory, which can be run directly.
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 WebSockets
importasynciofromgate_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 function
importconcurrent.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()