Protocol Usage

JSON

Standard JSON, each websocket frame can only contain one JSON message at a time.

Import Classes:

import asyncio
import json
import time
import websockets
import nest_asyncio
import threading
import datetime
nest_asyncio.apply()

Authentication:

uriJson = "wss://mlink-live.nms.saturn.spiderrockconnect.com/mlink/json"
apiKey = 'your api key'
password = 'your password'
api_key_token = f"{apiKey}.{password}"

Asynchronously query AAPL:

async def recv_msg(websocket):
  buffer = await websocket.recv()
  parts = list(filter(None, buffer.split(b'\r\n')))
    for msg in parts:
      result = json.loads(msg)
      print(result, '\n')
return True

async def query_mlink(api_key_token):
  retry = True
  while retry:
    try:
      async with websockets.connect(
        uriJson,
        extra_headers={"Authorization": f"Bearer {api_key_token}"},
        ping_timeout=None
      ) as websocket:
        msg = {
                "header": {
                    "mTyp": "MLinkStream"
                },
                "message": {
                    "queryLabel": "ExampleStockNbbo",
                    "activeLatency": 1, #stream
                    "msgName": "StockBookQuote",
                    "where":"ticker.tk:eq:AAPL | ticker.at:eq:EQT | ticker.ts:eq:NMS"
                }
            }
        t = time.time_ns()
        tstr = '.'.join([time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(t / 1000000000)), "%06d" % ((t / 1000) % 1000000)])
        msg['header']['sTim'] = tstr
        msg['header']['encT'] = tstr
        smsg = json.dumps(msg)
        await websocket.send(smsg)
        notDone = True
        while notDone:
          notDone = await recv_msg(websocket)
          retry = False
        except asyncio.exceptions.TimeoutError:
          print("timeout occurred, retrying...")

Framed JSON

SpiderRock JSON with protobuf-like header.

Same as JSON above, except for the parser:

async def query_mlink(api_key_token):
  retry = True
  while retry:
    try:
      async with websockets.connect(
        uriJson,
        extra_headers={"Authorization": f"Bearer {api_key_token}"},
        ping_timeout=None
      ) as websocket:
        msg = {
                "header": {
                    "mTyp": "MLinkStream"
                },
                "message": {
                    "queryLabel": "ExampleStockNbbo",
                    "activeLatency": 1, #stream
                    "msgName": "StockBookQuote",
                    "where":"ticker.tk:eq:AAPL & ticker.at:eq:EQT & ticker.ts:eq:NMS"
                }
            }
        t = time.time_ns()
        tstr = '.'.join([time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(t/1000000000)),"%06d"%((t/1000)%1000000)])
        msg['header']['sTim'] = tstr
        msg['header']['encT'] = tstr
        smsg = json.dumps(msg)
        jmsg = ''.join(['\r\nJ', '%011d'%len(smsg), smsg]) #header
        await websocket.send(jmsg)
        notDone = True
        while notDone:
          buffer = await websocket.recv()
          parts = list(filter(None,buffer.split(b'\r\n')))
          for msg in parts:
            result = json.loads(msg[12:])
            print(result, '\n')
        except asyncio.exceptions.TimeoutError:
          print("timeout occurred, retrying...")