aioactioncable: async Action Cable client library
aioactioncable is a python library for building Ruby on Rails Action Cable clients.
The library is based on websockets and asyncio.
aioactioncable is thus an async Rails Action Cable client library.
Installation
$ python3 -m pip install aioactioncable
aioactioncable requires Python 3 and therefore needs to be installed using the Python 3 version of pip.
Requirements
- Python >= 3.7
- websockets
Usage
In addition to managing websockets connections, Action Cable servers manage multiple channels, that clients can subscribe to.
Here is a code example to connect to an Action Cable server, subscribe to a channel and receive messages on that channel:
#!/usr/bin/env python3
import aioactioncable
import asyncio
import json
def process(msg, identifier)
msg_json = json.loads(msg)
print(f'Message received on {json.dumps(identifier)}')
...
async def ac_recv(uri, identifier):
async with aioactioncable.connect(uri) as acconnect:
subscription = await acconnect.subscribe(identifier)
async for msg in subscription:
process(msg, identifier)
asyncio.run(ac_recv('wss://example.app', {'channel': 'ChatChannel'}))
All the code examples below must be run in an asyncio event loop. Examples are built "chronologically", object created in Connect section is reused in Subscribe section, and so on.
Connect to an Action Cable server
import aioactioncable
acconnect = aioactioncable.connect(uri)
aioactioncable Connect object is an async context manager, you can thus use it in an async with statement:
import aioactioncable
import asyncio
async with aioactioncable.connect('wss://example.app') as acconnect:
...
Subscribe to an Action Cable channel
subscription = await acconnect.subscribe({'channel': 'ChatChannel'})
Recv messages on an Action Cable channel
Receive next message on subscription channel:
msg = await subscription.recv()
Subscription object is an iterable, you can thus iterate over to recv messages in an async for loop:
async for msg in subscription:
...
Send messages on an Action Cable channel
await subscription.send({'action': 'create', 'chatRoom': 'climbing'})
Unsubscribe from an Action Cable channel
await subscription.unsubscribe()
Close an Action Cable server connection
Explicit close of the connection is not needed if it is done in an async with statement.
Otherwise:
await acconnect.close()
License
aioactioncable is distributed under the MIT license.
Contributions
Contributions are very welcome!
Feel free to open an issue for any bug report.
Feel free to propose bug fixes or features via a Pull Request.
Metadata
Release files for aioactioncable 0.8.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| aioactioncable-0.8.1.tar.gz | 6.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aioactioncable-0.8.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.5 kB
Release files / aioactioncable-0.8.1.tar.gz
| Download URL | aioactioncable-0.8.1.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3d38ecb571bd6d0f33bba0cddbba27125503d07136bf20950fa613067603b82e
|
|
BLAKE2b-256 checksum How to use checksums |
1197e6ba82dc20be5d170b4126882641d226cf9307a5b697eecf049557e592a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.23.0
|
Release files / aioactioncable-0.8.1-py3-none-any.whl
| Download URL | aioactioncable-0.8.1-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f0727a25c3df5ef13f187f7e4df548c5f88598c3cf5504814945ea2471b4c173
|
|
BLAKE2b-256 checksum How to use checksums |
4ea392fcac12232a0eafd7c747d3265c83f08c1641f819b7f3cb9bf592f95643
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.23.0
|
Security for aioactioncable 0.8.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.