On this page
In plain English

A short 'still here' message you send on a timer. Without it, idle connections get dropped by the network long before you notice -- and a silent feed looks exactly like a quiet market.

WSwss://api.infinn.in/NorenWSTP/

What it does

Keeps the connection alive.

No interval is documented

The API specification says to keep the connection alive with heartbeat messages but does not publish a required interval or a server-side timeout. Documentation information not available. A heartbeat every 30 seconds is a common choice for this class of feed and is well inside any plausible idle timeout; confirm with Infinn if you need a guarantee.

Use the reply as a liveness check, not just a formality

If you send h and no hk comes back within a few seconds, the connection is dead even though the socket may still report itself as open. That is your cue to reconnect and resubscribe.

Messages you send

Heartbeat t: "h"

Client → server

Send this periodically for as long as you want the connection to stay up.

Example message
{ "t": "h" }
Heartbeat fields
FieldTypeDescription
tstring

Always the literal 'h'.

Messages you receive

Heartbeat acknowledgement t: "hk"

Server → client

The server's reply.

Example message
{ "t": "hk", "ft": "1767225600" }
Heartbeat acknowledgement fields
FieldTypeDescription
tstring

Always 'hk'.

ftstring

Timestamp in seconds.

Working example

A complete Python client for this feed. It needs only the websocket-client package (pip install websocket-client).

Python
"""Infinn WebSocket client -- Heartbeat.

Requires: pip install websocket-client
"""
import json
import threading
import time

import websocket  # websocket-client

ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
UID = "IN001"

# Merged state per token. The acknowledgement carries a full snapshot and
# every later update carries only what changed, so updates must be MERGED
# into this, never used to replace an entry.
state = {}


def on_open(ws):
  # Step 1: authenticate. Nothing else is honoured until "ak" arrives.
  ws.send(json.dumps({
      "t": "a",
      "uid": UID,
      "actid": UID,
      "source": "API",
      "accesstoken": ACCESS_TOKEN,
  }))


def on_message(ws, raw):
  msg = json.loads(raw)
  kind = msg.get("t")

  if kind == "ak":
      # The documentation says "Ok or Not_Ok"; the live feed sends "NOT_OK".
      # Compare case-insensitively.
      if str(msg.get("s", "")).lower() != "ok":
          print("Authentication refused:", msg)
          ws.close()
          return
      print("Authenticated. Subscribing.")
      threading.Thread(target=heartbeat, args=(ws,), daemon=True).start()
      return

  # Acknowledgements carry a full snapshot; feeds carry deltas.
  if kind in ("tk", "dk", "lk"):
      state[msg["tk"]] = dict(msg)
  elif kind in ("tf", "df", "lf"):
      state.setdefault(msg["tk"], {}).update(msg)
  elif kind == "hk":
      return  # heartbeat acknowledged
  else:
      print(kind, msg)
      return

  row = state[msg["tk"]]
  print(f'{row.get("ts", row["tk"]):<16} {row.get("lp", "-"):>10}')


def heartbeat(ws):
  # No interval is published. 30s is comfortably inside any plausible
  # idle timeout for a feed of this kind.
  while ws.keep_running:
      time.sleep(30)
      try:
          ws.send(json.dumps({"t": "h"}))
      except Exception:
          return


def on_error(ws, err):
  print("error:", err)


def on_close(ws, code, reason):
  # 1008 (PolicyViolation) is what the server sends when it rejects
  # your credentials.
  print(f"closed: {code} {reason}")


if __name__ == "__main__":
  websocket.WebSocketApp(
      "wss://api.infinn.in/NorenWSTP/",
      on_open=on_open,
      on_message=on_message,
      on_error=on_error,
      on_close=on_close,
  ).run_forever()
Esc
↑↓ navigate↵ openEsc close/ to search