Skip to main content

Gate.io WebSocket V4 Python SDK

Project description

Gate.io WebSocket Python SDK

gate_ws provides Gate.io WebSocket V4 Python implementation, including all channels defined in spot(new) and futures WebSocket.

Features:

  1. Fully asynchronous
  2. Reconnect on connection to server lost, and resubscribe on connection recovered
  3. Support connecting to multiple websocket servers
  4. Highly configurable

Installation

This package requires Python version 3.6+. Python 2 will NOT be supported.

pip install --user gate-ws

Getting Started

import asyncio

from gate_ws import Configuration, Connection, WebSocketResponse
from gate_ws.spot import SpotPublicTradeChannel


# define your callback function on message received
def print_message(conn: Connection, response: WebSocketResponse):
    if response.error:
        print('error returned: ', response.error)
        conn.close()
        return
    print(response.result)


async def main():
    # initialize default connection, which connects to spot WebSocket V4
    # it is recommended to use one conn to initialize multiple channels
    conn = Connection(Configuration())

    # subscribe to any channel you are interested into, with the callback function
    channel = SpotPublicTradeChannel(conn, print_message)
    channel.subscribe(["GT_USDT"])

    # start the client
    await conn.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.

Advanced usage

  1. Subscribe to private channels
    from gate_ws import Configuration, Connection
    from gate_ws.spot import SpotOrderChannel
    
    
    async def main():
        conn = Connection(Configuration(api_key='YOUR_API_KEY', api_secret='YOUR_API_SECRET'))
        channel = SpotOrderChannel(conn, lambda c, r: print(r.result))
        channel.subscribe(["GT_USDT"])
    
        # start the client
        await conn.run()
    
  2. Your callback function can also be a coroutine
    import asyncio
    
    
    async def my_callback(conn, response):
        await asyncio.sleep(1)
        print(response.result)
    
  3. You can provide a default callback function for all channels, so that when subscribing to new channels, no additional callback function are needed.
    from gate_ws import Configuration, Connection
    from gate_ws.spot import SpotPublicTradeChannel
    
    
    async def main():
        # provide default callback for all channels
        conn = Connection(Configuration(default_callback=lambda c, r: print(r.result)))
    
        # default callback will be used if callback not provided when initializing channels
        channel = SpotPublicTradeChannel(conn)
        channel.subscribe(["GT_USDT"])
    
        # start the client
        await conn.run()
    
  4. Subscribe to both spot and futures WebSockets
    import asyncio
    
    from gate_ws import Configuration, Connection
    from gate_ws.spot import SpotPublicTradeChannel
    from gate_ws.futures import FuturesPublicTradeChannel
    
    
    async def main():
        # initialize a spot connection, which is the default if no parameters is provided
        spot_conn = Connection(Configuration(app='spot'))
        # initialize a futures connection
        futures_conn = Connection(Configuration(app='futures', settle='usdt', test_net=False))
    
        # subscribe to any channel you are interested into, with the callback function
        channel = SpotPublicTradeChannel(spot_conn, lambda c, r: print(r.result))
        channel.subscribe(["BTC_USDT"])
    
        channel = FuturesPublicTradeChannel(futures_conn, lambda c, r: print(r.result))
        channel.subscribe(["BTC_USDT"])
    
        # start both connection
        await asyncio.gather(spot_conn.run(), futures_conn.run())
    
  5. You can use your own executor pool to run your callback function
    import concurrent.futures
    
    from gate_ws import Configuration, Connection
    from gate_ws.spot import SpotPublicTradeChannel
    
    
    async def main():
        # use process pool to run your callback function
        with concurrent.futures.ProcessPoolExecutor() as pool:
            conn = Connection(Configuration(executor_pool=pool))
    
            # default callback will be used if callback not provided when subscribing
            channel = SpotPublicTradeChannel(conn, lambda c, r: print(r.result))
            channel.subscribe(["GT_USDT"])
    
            # start the client
            await conn.run()
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gate-ws-0.3.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

gate_ws-0.3.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file gate-ws-0.3.1.tar.gz.

File metadata

  • Download URL: gate-ws-0.3.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.16

File hashes

Hashes for gate-ws-0.3.1.tar.gz
Algorithm Hash digest
SHA256 f8e13a2149863304a57d0f65636af41a43a5844fb716805a7392e9e5a7cc3761
MD5 60fdaf6fb41e3207762b03c3b65ed080
BLAKE2b-256 349c842227e630e1cee966bab9890fe3e616018cef8e1cb913911ceeb37c962e

See more details on using hashes here.

File details

Details for the file gate_ws-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: gate_ws-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/2.7.16

File hashes

Hashes for gate_ws-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b0b3ccd066edb3cbc8407457f3f9587bd1095cacd251d4af62458738d8d68a91
MD5 17d78aa39d574e51791c60a40bf98097
BLAKE2b-256 e18b06d993393358da77576be69d4e32362c8b99d38a41b92b23853d0b53ae34

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page