Skip to main content

Async library for building Ruby on Rails Action Cable clients, built on top of websockets.

Project description

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

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

def process(msg)
  print(f'Message received on {json.dumps(identifier)}')
  ...

async def ac_recv(uri, identifier):
  async with aioactioncable.connect(uri) as acconnect:
    subscription = 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 = acconnect.subscribe({'channel': 'ChatChannel'})

Recv messages on an Action Cable channel

Receive next message on subscription channel:

msg = 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

subscription.send({'action': 'create', 'chatRoom': 'climbing'})

Unsubscribe from an Action Cable channel

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:

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.

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

aioactioncable-0.8.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

aioactioncable-0.8-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file aioactioncable-0.8.tar.gz.

File metadata

  • Download URL: aioactioncable-0.8.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.23.0

File hashes

Hashes for aioactioncable-0.8.tar.gz
Algorithm Hash digest
SHA256 de857e28d68c66f0904e540cda28319b1ec3bcd72fdb9607631961ce9a1d2f03
MD5 9aef041ceb47c583f587db082268f0d9
BLAKE2b-256 215a8f38a0031cc8b7c6d5d5556ea4008fa18e7df0901eac0cca26611d46d85a

See more details on using hashes here.

Provenance

File details

Details for the file aioactioncable-0.8-py3-none-any.whl.

File metadata

File hashes

Hashes for aioactioncable-0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 f2dcd785696f26d6e6878e7b44e8d9eab7fdd05f4c8cc2cd53e7d6606de91295
MD5 b7f676355c40e1d4269600984b91d92f
BLAKE2b-256 8b08640c50465a4ded0b1ac2744fecacd5447b849892497e54768872cfe361df

See more details on using hashes here.

Provenance

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