# pysignalr
[![GitHub stars](https://img.shields.io/github/stars/baking-bad/pysignalr?color=2c2c2c&style=plain)](https://github.com/baking-bad/pysignalr)
[![Latest stable release](https://img.shields.io/github/v/release/baking-bad/pysignalr?label=stable%20release&color=2c2c2c)](https://github.com/baking-bad/pysignalr/releases)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pysignalr?color=2c2c2c)](https://www.python.org)
[![License: MIT](https://img.shields.io/github/license/baking-bad/pysignalr?color=2c2c2c)](https://github.com/baking-bad/pysignalr/blob/master/LICENSE)
<br>
[![PyPI monthly downloads](https://img.shields.io/pypi/dm/pysignalr?color=2c2c2c)](https://pypi.org/project/pysignalr/)
[![GitHub issues](https://img.shields.io/github/issues/baking-bad/pysignalr?color=2c2c2c)](https://github.com/baking-bad/pysignalr/issues)
[![GitHub pull requests](https://img.shields.io/github/issues-pr/baking-bad/pysignalr?color=2c2c2c)](https://github.com/baking-bad/pysignalr/pulls)
**pysignalr** is a modern, reliable, and async-ready client for the [SignalR protocol](https://docs.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-5.0). This project started as an asyncio fork of mandrewcito's [signalrcore](https://github.com/mandrewcito/signalrcore) library and ended up as a complete rewrite.
## Table of Contents
1. [Installation](#installation)
2. [Basic Usage](#basic-usage)
3. [Usage with Token Authentication](#usage-with-token-authentication)
4. [API Reference](#api-reference)
5. [License](#license)
## Installation
To install `pysignalr`, simply use pip:
```bash
pip install pysignalr
```
## Basic Usage
Let's connect to [TzKT](https://tzkt.io/), an API and block explorer of Tezos blockchain, and subscribe to all operations:
```python
import asyncio
from contextlib import suppress
from typing import Any
from typing import Dict
from typing import List
from pysignalr.client import SignalRClient
from pysignalr.messages import CompletionMessage
async def on_open() -> None:
print('Connected to the server')
async def on_close() -> None:
print('Disconnected from the server')
async def on_message(message: List[Dict[str, Any]]) -> None:
print(f'Received message: {message}')
async def on_error(message: CompletionMessage) -> None:
print(f'Received error: {message.error}')
async def main() -> None:
client = SignalRClient('https://api.tzkt.io/v1/ws')
client.on_open(on_open)
client.on_close(on_close)
client.on_error(on_error)
client.on('operations', on_message)
await asyncio.gather(
client.run(),
client.send('SubscribeToOperations', [{}]),
)
with suppress(KeyboardInterrupt, asyncio.CancelledError):
asyncio.run(main())
```
## Usage with Token Authentication
To connect to the SignalR server using token authentication:
```python
import asyncio
from contextlib import suppress
from typing import Any, Dict, List
from pysignalr.client import SignalRClient
from pysignalr.messages import CompletionMessage
async def on_open() -> None:
print('Connected to the server')
async def on_close() -> None:
print('Disconnected from the server')
async def on_message(message: List[Dict[str, Any]]) -> None:
print(f'Received message: {message}')
async def on_error(message: CompletionMessage) -> None:
print(f'Received error: {message.error}')
def token_factory() -> str:
# Replace with logic to fetch or generate the token
return "your_access_token_here"
async def main() -> None:
client = SignalRClient(
url='https://api.tzkt.io/v1/ws',
access_token_factory=token_factory,
headers={"mycustomheader": "mycustomheadervalue"},
)
client.on_open(on_open)
client.on_close(on_close)
client.on_error(on_error)
client.on('operations', on_message)
await asyncio.gather(
client.run(),
client.send('SubscribeToOperations', [{}]),
)
with suppress(KeyboardInterrupt, asyncio.CancelledError):
asyncio.run(main())
```
## API Reference
### `SignalRClient`
#### Parameters
- `url` (str): The SignalR server URL.
- `access_token_factory` (Callable[[], str], optional): A function that returns the access token.
- `headers` (Dict[str, str], optional): Additional headers to include in the WebSocket handshake.
#### Methods
- `on_open(callback: Callable[[], Awaitable[None]])`: Set the callback for connection open event.
- `on_close(callback: Callable[[], Awaitable[None]])`: Set the callback for connection close event.
- `on_error(callback: Callable[[CompletionMessage], Awaitable[None]])`: Set the callback for error events.
- `on(event: str, callback: Callable[[List[Dict[str, Any]]], Awaitable[None]])`: Set the callback for a specific event.
- `send(method: str, args: List[Any])`: Send a message to the server.
### `CompletionMessage`
A message received from the server upon completion of a method invocation.
#### Attributes
- `error` (str): The error message, if any.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
Raw data
{
"_id": null,
"home_page": "https://github.com/baking-bad/pysignalr",
"name": "pysignalr",
"maintainer": "Lev Gorodetskii",
"docs_url": null,
"requires_python": "<4,>=3.9",
"maintainer_email": "pysignalr@drsr.io",
"keywords": "signalr, asp, client, asyncio, json, messagepack, websockets",
"author": "Lev Gorodetskii",
"author_email": "pysignalr@drsr.io",
"download_url": "https://files.pythonhosted.org/packages/3a/9f/de6401378e236d77723d743b91bc39bffae6d8ab89689b97991363c94643/pysignalr-1.1.0.tar.gz",
"platform": null,
"description": "# pysignalr\n\n[![GitHub stars](https://img.shields.io/github/stars/baking-bad/pysignalr?color=2c2c2c&style=plain)](https://github.com/baking-bad/pysignalr)\n[![Latest stable release](https://img.shields.io/github/v/release/baking-bad/pysignalr?label=stable%20release&color=2c2c2c)](https://github.com/baking-bad/pysignalr/releases)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pysignalr?color=2c2c2c)](https://www.python.org)\n[![License: MIT](https://img.shields.io/github/license/baking-bad/pysignalr?color=2c2c2c)](https://github.com/baking-bad/pysignalr/blob/master/LICENSE)\n<br>\n[![PyPI monthly downloads](https://img.shields.io/pypi/dm/pysignalr?color=2c2c2c)](https://pypi.org/project/pysignalr/)\n[![GitHub issues](https://img.shields.io/github/issues/baking-bad/pysignalr?color=2c2c2c)](https://github.com/baking-bad/pysignalr/issues)\n[![GitHub pull requests](https://img.shields.io/github/issues-pr/baking-bad/pysignalr?color=2c2c2c)](https://github.com/baking-bad/pysignalr/pulls)\n\n**pysignalr** is a modern, reliable, and async-ready client for the [SignalR protocol](https://docs.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-5.0). This project started as an asyncio fork of mandrewcito's [signalrcore](https://github.com/mandrewcito/signalrcore) library and ended up as a complete rewrite.\n\n## Table of Contents\n\n1. [Installation](#installation)\n2. [Basic Usage](#basic-usage)\n3. [Usage with Token Authentication](#usage-with-token-authentication)\n4. [API Reference](#api-reference)\n5. [License](#license)\n\n## Installation\n\nTo install `pysignalr`, simply use pip:\n\n```bash\npip install pysignalr\n```\n\n## Basic Usage\n\nLet's connect to [TzKT](https://tzkt.io/), an API and block explorer of Tezos blockchain, and subscribe to all operations:\n\n```python\nimport asyncio\nfrom contextlib import suppress\nfrom typing import Any\nfrom typing import Dict\nfrom typing import List\n\nfrom pysignalr.client import SignalRClient\nfrom pysignalr.messages import CompletionMessage\n\n\nasync def on_open() -> None:\n print('Connected to the server')\n\n\nasync def on_close() -> None:\n print('Disconnected from the server')\n\n\nasync def on_message(message: List[Dict[str, Any]]) -> None:\n print(f'Received message: {message}')\n\n\nasync def on_error(message: CompletionMessage) -> None:\n print(f'Received error: {message.error}')\n\n\nasync def main() -> None:\n client = SignalRClient('https://api.tzkt.io/v1/ws')\n\n client.on_open(on_open)\n client.on_close(on_close)\n client.on_error(on_error)\n client.on('operations', on_message)\n\n await asyncio.gather(\n client.run(),\n client.send('SubscribeToOperations', [{}]),\n )\n\n\nwith suppress(KeyboardInterrupt, asyncio.CancelledError):\n asyncio.run(main())\n```\n\n## Usage with Token Authentication\n\nTo connect to the SignalR server using token authentication:\n\n```python\nimport asyncio\nfrom contextlib import suppress\nfrom typing import Any, Dict, List\n\nfrom pysignalr.client import SignalRClient\nfrom pysignalr.messages import CompletionMessage\n\nasync def on_open() -> None:\n print('Connected to the server')\n\nasync def on_close() -> None:\n print('Disconnected from the server')\n\nasync def on_message(message: List[Dict[str, Any]]) -> None:\n print(f'Received message: {message}')\n\nasync def on_error(message: CompletionMessage) -> None:\n print(f'Received error: {message.error}')\n\ndef token_factory() -> str:\n # Replace with logic to fetch or generate the token\n return \"your_access_token_here\"\n\nasync def main() -> None:\n client = SignalRClient(\n url='https://api.tzkt.io/v1/ws',\n access_token_factory=token_factory,\n headers={\"mycustomheader\": \"mycustomheadervalue\"},\n )\n\n client.on_open(on_open)\n client.on_close(on_close)\n client.on_error(on_error)\n client.on('operations', on_message)\n\n await asyncio.gather(\n client.run(),\n client.send('SubscribeToOperations', [{}]),\n )\n\nwith suppress(KeyboardInterrupt, asyncio.CancelledError):\n asyncio.run(main())\n```\n\n## API Reference\n\n### `SignalRClient`\n\n#### Parameters\n\n- `url` (str): The SignalR server URL.\n- `access_token_factory` (Callable[[], str], optional): A function that returns the access token.\n- `headers` (Dict[str, str], optional): Additional headers to include in the WebSocket handshake.\n\n#### Methods\n\n- `on_open(callback: Callable[[], Awaitable[None]])`: Set the callback for connection open event.\n- `on_close(callback: Callable[[], Awaitable[None]])`: Set the callback for connection close event.\n- `on_error(callback: Callable[[CompletionMessage], Awaitable[None]])`: Set the callback for error events.\n- `on(event: str, callback: Callable[[List[Dict[str, Any]]], Awaitable[None]])`: Set the callback for a specific event.\n- `send(method: str, args: List[Any])`: Send a message to the server.\n\n### `CompletionMessage`\n\nA message received from the server upon completion of a method invocation.\n\n#### Attributes\n\n- `error` (str): The error message, if any.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n",
"bugtrack_url": null,
"license": null,
"summary": "Modern, reliable and async-ready client for SignalR protocol",
"version": "1.1.0",
"project_urls": {
"Homepage": "https://github.com/baking-bad/pysignalr",
"Repository": "https://github.com/baking-bad/pysignalr"
},
"split_keywords": [
"signalr",
" asp",
" client",
" asyncio",
" json",
" messagepack",
" websockets"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "671f6df95e7ad27dae9a78c21774e75d7037e8630728849c77426d59a1e688c4",
"md5": "8c59e4eeea8357a3d0c350eb7cb756d5",
"sha256": "17977720e3360403b05e8c764a28c70a3865fef973cc0f08bd877101f06ae815"
},
"downloads": -1,
"filename": "pysignalr-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8c59e4eeea8357a3d0c350eb7cb756d5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.9",
"size": 19213,
"upload_time": "2024-11-30T17:40:10",
"upload_time_iso_8601": "2024-11-30T17:40:10.539310Z",
"url": "https://files.pythonhosted.org/packages/67/1f/6df95e7ad27dae9a78c21774e75d7037e8630728849c77426d59a1e688c4/pysignalr-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3a9fde6401378e236d77723d743b91bc39bffae6d8ab89689b97991363c94643",
"md5": "9a0c7827a336fe1981fc5e9f9427bf65",
"sha256": "f9e9ed611c999043778ebb9e5bd7d92e0afae3f9555eb73d447335e48de3b385"
},
"downloads": -1,
"filename": "pysignalr-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "9a0c7827a336fe1981fc5e9f9427bf65",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.9",
"size": 16385,
"upload_time": "2024-11-30T17:40:16",
"upload_time_iso_8601": "2024-11-30T17:40:16.069176Z",
"url": "https://files.pythonhosted.org/packages/3a/9f/de6401378e236d77723d743b91bc39bffae6d8ab89689b97991363c94643/pysignalr-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-30 17:40:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "baking-bad",
"github_project": "pysignalr",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pysignalr"
}