async def on_message(msg):
    """
    This is the handler for server messages. We then iterate the subscribers
    and send the message through the opened websockets
    the message is received with msgpack short types, but when we subscribe
    we use long types (e.g 't' vs 'trades') and this is how we keep track of it
    we need to translate it first and then send it.
    """
    msg_type = msg.get('T')
    symbol = msg.get('S')
    if msg_type == 't':
        _type = 'trades'
    elif msg_type == 'q':
        _type = 'quotes'
    elif msg_type == 'b':
        _type = 'bars'
    elif msg_type == 'd':
        _type = 'dailyBars'
    elif msg_type == 's':
        _type = 'statuses'
    else:
        return
    try:
        for sub, channels in subscribers.items():
            if symbol in channels[_type]:
                response_queue.put({"subscriber": sub, "response": msg})
    except Exception as e:
        print(e)
        traceback.print_exc()
Exemplo n.º 2
0
def get_current_channels():
    result = defaultdict(set)
    for sub, chans in subscribers.items():
        if chans:
            for _type in chans:
                result[_type].update(set(chans[_type]))
    return result
Exemplo n.º 3
0
async def on_message(conn, subject, msg):
    """
    This is the handler for server messages.
    The entry point to this module.
    We get the server message, already as Entity form translated by the python
    SDK. We try to bring it back to raw form, and then send it to every client
    registered to this service.

    The process of re-constructing is not optimal and we may do it for more
    than one client even if we already have it re-constructed.
    There's some refactoring required to make sure we re-construct once.

    :param conn:
    :param subject:
    :param msg:
    :return:
    """
    # iterate channels and distribute the message to correct subscribers
    try:
        for sub, channels in subscribers.items():
            restructured = _get_original_message(msg, channels)

            if sub.state != State.CLOSED:
                if restructured:
                    # only if we were able to restructure it
                    response_queue.put({
                        "subscriber": sub,
                        "response": restructured
                    })
    except Exception as e:
        print(e)
        traceback.print_exc()
async def on_message(conn, subject, msg):
    """
    This is the handler for server messages. We then iterate the subscribers
    and send the message through the opened websockets
    """
    # iterate channels and distribute the message to correct subscribers
    try:
        for sub, channels in subscribers.items():
            if msg['stream'] in [c.replace('alpacadatav1/', '') for c in channels]:
                response_queue.put({"subscriber": sub,
                                    "response":   msg})
    except Exception as e:
        print(e)
        traceback.print_exc()
Exemplo n.º 5
0
async def clear_dead_subscribers():
    # copy to be able to remove closed connections
    subs = dict(subscribers.items())
    for sub, chans in subs.items():
        if sub.state == State.CLOSED:
            del subscribers[sub]
Exemplo n.º 6
0
async def get_current_channels():
    result = []
    for sub, chans in subscribers.items():
        result.extend(chans)
    result = list(set(result))  # we want the list to be unique
    return result