<jupyter_start><jupyter_text>[🥭 Mango Markets](https://mango.markets/) support is available at: [Docs](https://docs.mango.markets/) | [Discord](https://discord.gg/67jySBhxrg) | [Twitter](https://twitter.com/mangomarkets) | [Github](https://github.com/blockworks-foundation) | [Email](mailto:<EMAIL>)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/blockworks-foundation/mango-explorer-examples/HEAD?labpath=SusbscribeOrderBook.ipynb) [Run this code](https://mybinder.org/v2/gh/blockworks-foundation/mango-explorer-examples/HEAD?labpath=SusbscribeOrderBook.ipynb) on Binder._🏃‍♀️ To run this notebook press the ⏩ icon in the toolbar above._  🥭 Subscribe to the OrderBookThis notebook shows how to stream the `OrderBook` for a perp market via a websocket, showing new orders as they appear.In the code below, the second parameter to `market.on_orderbook_change()` is a lambda that is called when a new `OrderBook` is available. (All the demonstration code below does is print the current `OrderBook` out with a timestamp. It's here you probably want to insert your own functionality.)Behind the scenes what the code does:* Fetch the current `OrderBook` to start with.* Build websocket subscriptions to both the bids and asks accounts.* Combine those changes into a single subscription that returns the updated `OrderBook`* Watches that subscription for changes, and prints out the `OrderBook` when it is updatedStreaming changes to an `OrderBook` is complicated by the fact that an `OrderBook` is based on two Solana accounts, not one. Bids and asks are held in separate accounts, and we want to update the `OrderBook` when either of them is changed.This code will exit after 60 seconds.<jupyter_code>import datetime
import mango
import time

with mango.ContextBuilder.build(cluster_name="devnet") as context:
    market = mango.market(context, "BTC-PERP")
    subscription = market.on_orderbook_change(context, lambda ob: print("\n", datetime.datetime.now(), "\n", ob))

    time.sleep(60)

    subscription.dispose()

print("Example complete.")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10471204188481675, 'fraction_numerical': 0.0038077106139933364, 'mean_word_length': 5.548286604361371, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '473444', 'license': 'MIT', 'n_tokens_mistral': 632, 'n_tokens_neox': 613, 'n_words': 219, 'path': 'SubscribeOrderBook.ipynb', 'repo_name': 'blockworks-foundation/mango-explorer-examples', 'size': '2121'}