reCluster
  • Welcome
    • reCluster
    • Registration
    • Frequently Asked Questions
    • Roadmap
  • Features
    • Midprice
    • Tickers
    • Trades
    • OHLC
    • Volume Weighted Average Price
  • Integration
    • General Information
    • Python
    • Node.js
    • Golang
Powered by GitBook
On this page
  • Prerequisites
  • Simple Example
  1. Integration

Python

Connect with Python

Prerequisites

  • Python 3.7+ is recommended.

  • Install the websockets library: pip install websockets

Simple Example

import asyncio
import websockets
import json

async def main():
    url = "wss://ws.recluster.io/ws?password=example"

    try:
        async with websockets.connect(url) as ws:
            print("Connected to the reCluster!")
            subscription_message = {
                "topic": "orderbook",
                "symbol": "BTCUSDT",
                "action": "subscribe"
            }
            await ws.send(json.dumps(subscription_message))
            while True:
                message = await ws.recv()
                try:
                    data = json.loads(message)
                    print("Data:", data)
                except json.JSONDecodeError as err:
                    print("Failed to parse JSON:", err, "\nRaw message:", message)

    except websockets.ConnectionClosedOK:
        print("Connection closed.")
    except websockets.ConnectionClosedError as err:
        print("Connection closed with error:", err)
    except Exception as e:
        print("Error occurred:", e)


if __name__ == "__main__":
    asyncio.run(main())

PreviousGeneral InformationNextNode.js

Last updated 3 months ago