slazarov / python-signalr-client

SignalR client for python based on asyncio.
MIT License
33 stars 22 forks source link

python-signalr-client

Python signalR client using asyncio. It's mainly based on TargetProcess signalR client which uses gevent.

I am mainly developing the client for my Python Bittrex Websocket project, however I would make it as universal as possible.

Road map

Notices

None right now.

Performance and supplemental libraries

Compatibility

Asyncio requires Python 3.5+.

For Python2.X compatibility try TargetProcess' gevent based SignalR client.

Installation

Pypi (most stable)

pip install signalr-client-aio

Github (master)

pip install git+https://github.com/slazarov/python-signalr-client.git

Github (work in progress branch)

pip install git+https://github.com/slazarov/python-signalr-client.git@next-version-number

Sample usage

from signalr_aio import Connection
from base64 import b64decode
from zlib import decompress, MAX_WBITS
import json

def process_message(message):
    deflated_msg = decompress(b64decode(message), -MAX_WBITS)
    return json.loads(deflated_msg.decode())

# Create debug message handler.
async def on_debug(**msg):
    # In case of 'queryExchangeState'
    if 'R' in msg and type(msg['R']) is not bool:
        decoded_msg = process_message(msg['R'])
        print(decoded_msg)

# Create error handler
async def on_error(msg):
    print(msg)

# Create hub message handler
async def on_message(msg):
    decoded_msg = process_message(msg[0])
    print(decoded_msg)

if __name__ == "__main__":
    # Create connection
    # Users can optionally pass a session object to the client, e.g a cfscrape session to bypass cloudflare.
    connection = Connection('https://beta.bittrex.com/signalr', session=None)

    # Register hub
    hub = connection.register_hub('c2')

    # Assign debug message handler. It streams unfiltered data, uncomment it to test.
    connection.received += on_debug

    # Assign error handler
    connection.error += on_error

    # Assign hub message handler
    hub.client.on('uE', on_message)
    hub.client.on('uS', on_message)

    # Send a message
    hub.server.invoke('SubscribeToExchangeDeltas', 'BTC-ETH')
    hub.server.invoke('SubscribeToSummaryDeltas')
    hub.server.invoke('queryExchangeState', 'BTC-NEO')

    # Start the client
    connection.start()

Change log

0.0.1.6.2 - 16/04/2018:

0.0.1.5 - 06/04/2018:

0.0.1.0 - Initial release.

Other libraries

Python Bittrex Websocket

Python websocket client for getting live streaming data from Bittrex Exchange.

Python Bittrex Autosell

Python CLI tool to auto sell coins on Bittrex.

It is used in the cases when you want to auto sell a specific coin for another, but there is no direct market, so you have to use an intermediate market.