How To Subscribe To Websocket API Channel Using Python? How To Subscribe To Websocket API Channel Using Python? python python

How To Subscribe To Websocket API Channel Using Python?


The documentation says all the messages are JSON encoded.

Message encoding

Each message sent and received via the Bitfinex’s websocket channel is encoded in JSON format

You need to import json library, to encode and decode your messages.

The documentation mentions three public channels: book, trades and ticker.
If you want to subscribe to a channel, you need to send a subscribe event.

Example of subscribing to the LTCBTC trades, according to the documentation:

ws.send(json.dumps({    "event":"subscribe",    "channel":"trades",    "channel":"LTCBTC"})

Then you also need to parse the incoming JSON encoded messages.

result = ws.recv()result = json.loads(result)


I think you are mixing here 2 different python packages. One is the websockets package which is the line you eventually commented out (#ws.connect("wss://api2.bitfinex.com:3000/ws")), and the other one is the actual package you are using which is websocket-client