nunodsousa
nunodsousa

Reputation: 2795

Websocket to download live data in python

I'm developing a simple program that downloads the order book of the bitstamp webpage using websockets.

The following code subscribes a channel, however, I cannot see the data that is receiving. Do you know why?

The code is the following (and there is an example in javascript here):

import asyncio
import websockets
import json


async def test():
    uri = "wss://ws.bitstamp.net"
    async with websockets.connect(uri) as websocket:
        subscribeMsg = {"event": "bts:subscribe", "data": {"channel": "order_book_btcusd"}}

        subscribeMsg = json.dumps(subscribeMsg)

        await websocket.send(subscribeMsg)
        print("subscription message = ", subscribeMsg)

        answer = await websocket.recv()
        print("answer = ", answer)


loop = asyncio.get_event_loop()

loop.run_until_complete(test())
loop.run_forever()

The output is:

subscription message =  {"event": "bts:subscribe", "data": {"channel": "order_book_btcusd"}}
answer =  {"event":"bts:subscription_succeeded","channel":"order_book_btcusd","data":{}}

However I expect something with the order book information, like shown in [here]. 1

Upvotes: 1

Views: 1118

Answers (1)

Try, after printing answer:

   print( “answer=“,answer )
   while True:
       mesg = await websocket.recv()
       print( “mesg=“,mesg )

Upvotes: 1

Related Questions