Skip to main content

A JSON-RPC websocket client library for asyncio

Project description

https://img.shields.io/pypi/v/jsonrpc-websocket.svg https://github.com/emlove/jsonrpc-websocket/workflows/tests/badge.svg https://coveralls.io/repos/emlove/jsonrpc-websocket/badge.svg?branch=main

This is a compact and simple JSON-RPC websocket client implementation for asyncio python code. This code is forked from https://github.com/gciotta/jsonrpc-requests

Main Features

  • Supports nested namespaces (eg. app.users.getUsers())

  • 100% test coverage

Usage

It is recommended to manage the aiohttp ClientSession object externally and pass it to the Server constructor. (See the aiohttp documentation.) If not passed to Server, a ClientSession object will be created automatically, and will be closed when the websocket connection is closed. If you pass in an external ClientSession, it is your responsibility to close it when you are finished.

Execute remote JSON-RPC functions

import asyncio
from jsonrpc_websocket import Server

async def routine():
    server = Server('ws://localhost:9090')
    try:
        await server.ws_connect()

        await server.foo(1, 2)
        await server.foo(bar=1, baz=2)
        await server.foo({'foo': 'bar'})
        await server.foo.bar(baz=1, qux=2)
    finally:
        await server.close()

asyncio.get_event_loop().run_until_complete(routine())

A notification

import asyncio
from jsonrpc_websocket import Server

async def routine():
    server = Server('ws://localhost:9090')
    try:
        await server.ws_connect()

        await server.foo(bar=1, _notification=True)
    finally:
        await server.close()

asyncio.get_event_loop().run_until_complete(routine())

Handle requests from server to client

import asyncio
from jsonrpc_websocket import Server

def client_method(arg1, arg2):
    return arg1 + arg2

async def routine():
    server = Server('ws://localhost:9090')
    # client_method is called when server requests method 'namespace.client_method'
    server.namespace.client_method = client_method
    try:
        await server.ws_connect()
    finally:
        await server.close()

asyncio.get_event_loop().run_until_complete(routine())

Pass through arguments to aiohttp (see also aiohttp documentation)

import asyncio
import aiohttp
from jsonrpc_websocket import Server

async def routine():
    server = Server(
        'ws://localhost:9090',
        auth=aiohttp.BasicAuth('user', 'pass'),
        headers={'x-test2': 'true'})
    try:
        await server.ws_connect()

        await server.foo()
    finally:
        await server.close()

asyncio.get_event_loop().run_until_complete(routine())

Pass through aiohttp exceptions

import asyncio
import aiohttp
from jsonrpc_websocket import Server

async def routine():
    server = Server('ws://unknown-host')
    try:
        await server.ws_connect()

        await server.foo()
    except TransportError as transport_error:
        print(transport_error.args[1]) # this will hold a aiohttp exception instance
    finally:
        await server.close()

asyncio.get_event_loop().run_until_complete(routine())

Tests

Install the Python tox package and run tox, it’ll test this package with various versions of Python.

Changelog

3.2.1 (2026-04-22)

  • Update deprecated patterns of accessing the event loop through the aiohttp ClientSession. Instead use the new asyncio.get_running_loop().

3.2.0 (2025-12-30)

  • Prevent reverse RPC calls from blocking the main read loop (#18) @vegas503

3.1.6 (2025-09-18)

  • Port to pytest-asyncio >= 1.0.0 (#16) @cjwatson

  • Use built-in asyncio timeout for python versions 11 and up

3.1.5 (2024-01-16)

3.1.4 (2022-05-23)

  • Only reconnect session when the session is managed internally

  • Remove deprecated with timeout syntax

3.1.3 (2022-05-23)

3.1.2 (2022-05-03)

  • Unpin test dependencies

3.1.1 (2021-11-21)

  • Fixed compatibility with async_timeout 4.0

3.1.0 (2021-05-03)

  • Bumped jsonrpc-base to version 2.1.0

3.0.0 (2021-03-17)

  • Bumped jsonrpc-base to version 2.0.0

  • BREAKING CHANGE: Allow single mapping as a positional parameter. Previously, when calling with a single dict as a parameter (example: server.foo({'bar': 0})), the mapping was used as the JSON-RPC keyword parameters. This made it impossible to send a mapping as the first and only positional parameter. If you depended on the old behavior, you can recreate it by spreading the mapping as your method’s kwargs. (example: server.foo(**{'bar': 0}))

2.0.0 (2020-12-22)

  • Remove session as a reserved attribute on Server

1.2.1 (2020-09-11)

  • Fix loop not closing after client closes

1.2.0 (2020-08-24)

  • Support for async server request handlers

1.1.0 (2020-02-17)

  • Support servers that send JSON-RPC requests as binary messages encoded with UTF-8 (#5) @shiaky

1.0.2 (2019-11-12)

  • Bumped jsonrpc-base to version 1.0.3

1.0.1 (2018-08-23)

  • Bumped jsonrpc-base to version 1.0.2

1.0.0 (2018-07-06)

  • Bumped jsonrpc-base to version 1.0.1

0.6 (2018-03-11)

  • Minimum required version of aiohttp is now 3.0.

  • Support for Python 3.4 is now dropped.

Credits

@gciotta for creating the base project jsonrpc-requests.

@mbroadst for providing full support for nested method calls, JSON-RPC RFC compliance and other improvements.

@vaab for providing api and tests improvements, better RFC compliance.

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

jsonrpc_websocket-3.2.1.tar.gz (7.1 kB view details)

Uploaded Source

File details

Details for the file jsonrpc_websocket-3.2.1.tar.gz.

File metadata

  • Download URL: jsonrpc_websocket-3.2.1.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for jsonrpc_websocket-3.2.1.tar.gz
Algorithm Hash digest
SHA256 84fec215aa0ae332d679bcbcdcfd5c2119a56901df1640518c43ff58dc9a6056
MD5 3343ca3da2d24f9ebd8d11a81a5dc92c
BLAKE2b-256 af4deb6417977bf24b68f33d209e4bdcdf4420a6a7bbf8ad8ac98844f14bef2b

See more details on using hashes here.

Supported by

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