.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_generated_sphinx_gallery_run-00-subscribe-to-public-channels.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_generated_sphinx_gallery_run-00-subscribe-to-public-channels.py:


============================
Subscribe to Public channels
============================

.. contents:: Table of Contents
    :local:
    :depth: 1

Initialise client
=================
By default all incoming messages are being logged at INFO level without
any modifications. So you would want to set logging accordingly in case
you need to visually inspect results.


.. code-block:: default

    import time
    import logging
    from pprint import pprint

    from bcx.client import BlockchainWebsocketClient

    logging.basicConfig(level=logging.INFO)

    client = BlockchainWebsocketClient()









Subscribe for monitoring
========================
Subscription can happen on a fly, i.e. you don't need to restart client
(or re-run script) to be able to subscribe to new channels.


.. code-block:: default

    client.subscribe_to_heartbeat()

    client.subscribe_to_prices("BTC-USD", granularity=60)

    client.subscribe_to_prices("ETH-USD", granularity=60)

    client.subscribe_to_ticker("BTC-USD")

    client.subscribe_to_trades("BTC-USD")

    time.sleep(2)
    pprint(client.connected_channels)






.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    [HeartbeatChannel(is_subscribed=True),
     PricesChannel(symbol=BTC-USD, granularity=60, is_subscribed=True),
     PricesChannel(symbol=ETH-USD, granularity=60, is_subscribed=True),
     TickerChannel(symbol=BTC-USD, is_subscribed=True),
     TradesChannel(symbol=BTC-USD, is_subscribed=True)]




Some channels send a snapshot of when you connect to them for the very
first time.


.. code-block:: default

    time.sleep(2)
    client.subscribe_to_symbols()

    client.subscribe_to_orderbook_l2("BTC-USD")

    client.subscribe_to_orderbook_l3("BTC-USD")

    time.sleep(2)
    pprint(client.connected_channels)






.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    [HeartbeatChannel(is_subscribed=True),
     OrderbookL2Channel(symbol=BTC-USD, is_subscribed=True),
     OrderbookL3Channel(symbol=BTC-USD, is_subscribed=True),
     PricesChannel(symbol=BTC-USD, granularity=60, is_subscribed=True),
     PricesChannel(symbol=ETH-USD, granularity=60, is_subscribed=True),
     SymbolsChannel(is_subscribed=True),
     TickerChannel(symbol=BTC-USD, is_subscribed=True),
     TradesChannel(symbol=BTC-USD, is_subscribed=True)]




This is a simple use case showing that the client not only reads incoming
messages and logs them, but also can parse them and store the result in
memory in order to be used later on, i.e. by external API.


.. code-block:: default

    time.sleep(7)
    print(f"Last Heart Beat: {client.get_last_heartbeat()}")




.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Last Heart Beat: 2020-05-17 22:25:27.069964





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  13.331 seconds)


.. _sphx_glr_download_generated_sphinx_gallery_run-00-subscribe-to-public-channels.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: run-00-subscribe-to-public-channels.py <run-00-subscribe-to-public-channels.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: run-00-subscribe-to-public-channels.ipynb <run-00-subscribe-to-public-channels.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
