Realtime Market Data with Websockets¶
This section covers the WebSocket API for real-time market data streaming. The Websocket connection provides seamless, low latency access to market data with a real-time connection between your system and the server. This enables users to receive continuous updates on stock prices, volume, and other market data directly from the server.
WebSocket Connection Setup¶
The WebSocket connection can be established using the NubraDataSocket class, which supports multiple data types and callbacks for different market data streams. All market data (stocks, indices, options, and orderbook) is received through the on_market_data callback, while specific data types can be filtered using the subscription method.
from nubra_python_sdk.ticker import websocketdata
from nubra_python_sdk.start_sdk import InitNubraSdk, NubraEnv
# Initialize the Nubra SDK client
# Use NubraEnv.UAT for testing or NubraEnv.PROD for production
nubra = InitNubraSdk(NubraEnv.UAT)  # or NubraEnv.PROD
##using totp login and .env file 
#nubra = InitNubraSdk(NubraEnv.UAT, totp_login= True ,env_creds = True)
# Define callback functions
def on_market_data(msg):
    # This callback receives all market data types
    print(f"[MarketData] {msg}")
def on_index_data(msg):
    print(f"[INDEX] {msg}")
def on_option_data(msg):
    print(f"[OPTION] {msg}")
def on_orderbook_data(msg):
    print("[Orderbook] ", msg)
def on_connect(msg):
    print("[status]", msg)
def on_close(reason):
    print(f"Closed: {reason}")
def on_error(err):
    print(f"Error: {err}")
# Initialize WebSocket
socket = websocketdata.NubraDataSocket(
    client=nubra,
    on_market_data=on_market_data,
    on_index_data=on_index_data,
    on_option_data=on_option_data,
    on_orderbook_data=on_orderbook_data,
    on_connect=on_connect,
    on_close=on_close,
    on_error=on_error,
    )
socket.connect()
#Subscribe to different data types
socket.subscribe(["RELIANCE:20250626"], data_type="option",exchange ="BSE")
socket.subscribe(["NIFTY", "HDFCBANK"], data_type="index", exchange ="NSE")
socket.subscribe(["1746686"], data_type="orderbook")
#The below method will block the main thread. Nothing after this will run.
#You have to use the pre-defined callbacks to manage subscriptions.
socket.keep_running()
Note: In version 0.3.5, the new WebSocket endpoint is used by default. To use the older WebSocket endpoint on version 0.3.5, pass socket_v2 = false.
# Initialize WebSocket socket = websocketdata.NubraDataSocket( client=nubra, on_market_data=on_market_data, on_index_data=on_index_data, on_option_data=on_option_data, on_orderbook_data=on_orderbook_data, on_connect=on_connect, on_close=on_close, on_error=on_error, socket_v2=False )
Advance Usage¶
# Start WebSocket in a separate thread
import threading
def run_websocket():
    socket.connect()
    # Subscribe to different data types
    socket.subscribe(["RELIANCE:20250626"], data_type="option")
    socket.subscribe(["NIFTY", "HDFCBANK"], data_type="index")
    socket.subscribe(["1746686"], data_type="orderbook")
    socket.keep_running()
websocket_thread = threading.Thread(target=run_websocket)
websocket_thread.daemon = True
websocket_thread.start()
Market Data¶
The on_market_data callback is the primary receiver for all market data types. It receives real-time updates for stocks, indices, options, and orderbook data through the following wrapper classes:
- IndexDataWrapper for stocks and index data
- OptionChainWrapper for option chain data
- OrderBookWrapper for orderbook data
The data can be filtered using specific subscriptions for each type as described in the Available Data Types section below.
Available Data Types¶
The WebSocket API supports the following data types that can be filtered from the market data stream:
1. Index Data¶
Subscribes to real-time data for market indices and stocks. While received through on_market_data, can be filtered using the index subscription.
Usage¶
from nubra_python_sdk.ticker import websocketdata
from nubra_python_sdk.start_sdk import InitNubraSdk, NubraEnv
# Initialize the Nubra SDK client
nubra = InitNubraSdk(NubraEnv.UAT)
##using totp login and .env file 
#nubra = InitNubraSdk(NubraEnv.UAT, totp_login= True ,env_creds = True)
# Define callback functions
def on_index_data(msg):
    print(f"[INDEX] {msg}")
def on_connect(msg):
    print("[status]", msg)
def on_close(reason):
    print(f"Closed: {reason}")
def on_error(err):
    print(f"Error: {err}")
# Initialize WebSocket
socket = websocketdata.NubraDataSocket(
    client=nubra,
    on_index_data=on_index_data,
    on_connect=on_connect,
    on_close=on_close,
    on_error=on_error
    )
socket.connect()
#Subscribe to different data types
socket.subscribe(["NIFTY", "BANKNIFTY"], data_type="index",exchange ="NSE")
#Infinite loop on the main thread. Nothing after this will run.
#You have to use the pre-defined callbacks to manage subscriptions.
socket.keep_running()
| Attribute | Data Type | Description | 
|---|---|---|
| symbols | List[str] | List of index symbols (e.g., ['NIFTY', 'BANKNIFTY']) | 
| data_type | str | "index" | 
| exchange | Optional[str] | eg., "NSE" , "BSE" ;if exchange is not passed default is "NSE" | 
Response:
# Response object structure
class IndexDataWrapper:
    indexname: str              # Symbol of the index (e.g., "NIFTY")
    exchange: str               # eg., NSE, BSE
    timestamp: int              # Timestamp in Epoch
    index_value: int            # Current index value
    volume: int                 # Trading volume
    changepercent: float        # Percentage change
    tick_volume: int            # Number of ticks
    prev_close: int             # Previous closing value
2. Option Chain Data¶
Subscribes to real-time option chain data. While received through on_market_data, can be filtered using the option subscription.
Usage¶
from nubra_python_sdk.ticker import websocketdata
from nubra_python_sdk.start_sdk import InitNubraSdk, NubraEnv
# Initialize the Nubra SDK client
nubra = InitNubraSdk(NubraEnv.UAT)
##using totp login and .env file 
#nubra = InitNubraSdk(NubraEnv.UAT, totp_login= True ,env_creds = True)
# Define callback functions
def on_option_data(msg):
    print(f"[OPTION] {msg}")
def on_connect(msg):
    print("[status]", msg)
def on_close(reason):
    print(f"Closed: {reason}")
def on_error(err):
    print(f"Error: {err}")
# Initialize WebSocket
socket = websocketdata.NubraDataSocket(
    client=nubra,
    on_option_data=on_option_data,
    on_connect=on_connect,
    on_close=on_close,
    on_error=on_error
    )
socket.connect()
#Subscribe to different data types
socket.subscribe(["RELIANCE:20250626"], data_type="option", exchange ="NSE")
#Infinite loop on the main thread. Nothing after this will run.
#You have to use the pre-defined callbacks to manage subscriptions.
socket.keep_running()
| Attribute | Data Type | Description | 
|---|---|---|
| symbols | List[str] | List of option symbols with expiry (e.g., ['RELIANCE:20250626']) | 
| data_type | str | "option" | 
| exchange | Optional[str] | eg., "NSE" , "BSE" ;if excahnge is not passed default is "NSE" | 
Response:
# Response object structure
class OptionChainWrapper:
    asset: str                    # Asset name (e.g., "RELIANCE")
    expiry: str                   # Expiry date in YYYYMMDD format
    at_the_money_strike: int      # At-the-money strike price
    current_price: int          # Current price of the underlying
    exchange: str               # eg. NSE, BSE
    # Call Options (CE)
    ce: List[OptionData]          # List of Call Option objects
    # Put Options (PE)
    pe: List[OptionData]          # List of Put Option objects
class OptionData:
    ref_id: int                                   # Reference ID               
    timestamp: int                                # Timestamp in Epoch
    strike_price: int                             # Strike price
    lot_size: int                                 # Lot size
    last_traded_price: Optional[int]            # Last traded price
    last_traded_price_change: Optional[float]     # Last traded price change
    iv: Optional[float]                           # Implied volatility
    delta: Optional[float]                        # Delta value
    gamma: Optional[float]                        # Gamma value
    theta: Optional[float]                        # Theta value
    vega: Optional[float]                         # Vega value
    volume: Optional[int]                         # Volume
    open_interest: Optional[int]                  # Open interest
    previous_open_interest: Optional[int]         # Open interest change
3. Order Book Data¶
Subscribes to real-time market depth data. While received through on_market_data, can be filtered using the orderbook subscription.
Usage¶
from nubra_python_sdk.ticker import websocketdata
from nubra_python_sdk.start_sdk import InitNubraSdk, NubraEnv
# Initialize the Nubra SDK client
nubra = InitNubraSdk(NubraEnv.UAT)
# Define callback functions
def on_orderbook_data(msg):
    print("[Orderbook] ", msg)
def on_connect(msg):
    print("[status]", msg)
def on_close(reason):
    print(f"Closed: {reason}")
def on_error(err):
    print(f"Error: {err}")
# Initialize WebSocket
socket = websocketdata.NubraDataSocket(
    client=nubra,
    on_orderbook_data=on_orderbook_data,
    on_connect=on_connect,
    on_close=on_close,
    on_error=on_error
    )
socket.connect()
#Subscribe to different data types 
socket.subscribe(["1746686"], data_type="orderbook")
#Infinite loop on the main thread. Nothing after this will run.
#You have to use the pre-defined callbacks to manage subscriptions.
socket.keep_running()
| Attribute | Data Type | Description | 
|---|---|---|
| ref_ids | List[str] | List of reference IDs to subscribe to (e.g., ["1746686"]) | 
Response:
# Response object structure
class OrderBookWrapper:
    ref_id: int                   # Reference ID
    timestamp: int                # Timestamp in Epoch
    last_traded_price: int       # Last traded price
    last_traded_quantity: int     # Last traded quantity
    volume: int                   # Total volume
    # Bids (Buy Orders)
    bids: List[Orders]            # List of Order objects
    # Asks (Sell Orders)
    asks: List[Orders]            # List of Order objects
class Orders:
    price: int                  # Order price
    quantity: int                 # Total quantity at this price
    num_orders: int               # Number of orders at this price
Error Handling¶
The WebSocket connection includes built-in error handling through the on_error callback. Common errors include:
- Connection failures
- Authentication errors
- Subscription errors
- Data parsing errors
Best Practices¶
- Always run WebSocket connections in a separate thread to prevent blocking the main application
- Implement proper error handling using the provided callbacks
- Use the keep_running()method to maintain the connection
- Close the connection properly when done using the close()method
- Monitor connection status using the on_connectandon_closecallbacks
- Use the on_market_datacallback as the primary data receiver, while using specific subscriptions to filter data types