Skip to main content

JSON-RPC Python client with streaming support

Project description

JSON-RPC Python Client

Library for interacting with JSON-RPC servers. Implements the JSON-RPC 2.0 spec, as well as first-class support for the subscription streaming endpoints exposed by most blockchain RPC interfaces (e.g. Ethereum, Solana) for JSON-RPC websockets connections.

Features

  • Websockets connection wrapper with full support for matching request IDs to responses and subscription updates to subscription IDs
  • Classes for (de)serializing JSON-RPC spec compliant message types

Installation

$ pip install bx-jsonrpc-py

Websockets Streaming

Many blockchains use a subscription RPC endpoint with JSON-RPC notifications to provide real-time event streams of updates in the network. They usually look something like this:

// create subscription
>> {"id": 1, "method": "subscribe", "params": ["feedName"]}
<< {"jsonrpc":"2.0","id":1,"result":"subscriptionID"}

// incoming notifications
<< {"jsonrpc":"2.0","method":"subscribe","params":{"subscription":"subscriptionID","result":"data"}}}
<< {"jsonrpc":"2.0","method":"subscribe","params":{"subscription":"subscriptionID","result":"data}}}

// cancel subscription
>> {"id": 1, "method": "unsubscribe", "params": ["subscriptionID"]}
<< {"jsonrpc":"2.0","id":1,"result":true}

Some examples: go-ethereum, solana

The included jsonrpc.WsRpcConnection class fully implements this feature.

Usage

import jsonrpc

async with jsonrpc.WsRpcConnection("ws://...") as ws:
    print(await ws.call("method", {"param": "value"}))
    
    subscription_id = await ws.subscribe(
        "feedname", {"option": "value"}
    )
    async for notification in ws.notifications_for_id(subscription_id):
        print(notification)

See examples/client.py for a working sample.

Anticipated Future Work

  • Reconnection support for websockets to reconnect and resubscribe to former feeds

Comments / Questions

Feel free to open up a Github issue to discuss enhancements or problems.

MIT License

Copyright (c) 2022 bloXroute Labs

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bx-jsonrpc-py-0.2.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

bx_jsonrpc_py-0.2.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file bx-jsonrpc-py-0.2.0.tar.gz.

File metadata

  • Download URL: bx-jsonrpc-py-0.2.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for bx-jsonrpc-py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7d5b4888ac2aee1d65cc85b787f1cfb6cd61c07d28b5699c25764337b18ba37f
MD5 120cb7936e894f8fa0dd58413142503f
BLAKE2b-256 cd699963fd07838e9ed35df5222a6978e52c98c58c3fc8aad0e589d68397e007

See more details on using hashes here.

File details

Details for the file bx_jsonrpc_py-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bx_jsonrpc_py-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9db51d433ecb4248c681c137a58050b820c2360f3b47d84c262991a29873c6f5
MD5 bdf3434323a769737328e31ed404d2eb
BLAKE2b-256 555c6069accc43bdac9319f8dc2383b44904f4cec420c7f4703f9c6943af2601

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page