Skip to main content

ActionCable Client for Python 3

Project description

ActionCable Client - A Python 3 client for Rails' Action Cable

pipeline status coverage report Latest Release

This library handles the connections and subscriptions and monitors the connection. It removes the underlaying websocket layer.

Currently authentication is not managed by this library.

Credits

The project originate this that was stalled for more than 5 years.

ROADMAP

See Roadmap file

Get started

sudo pip3 install actioncable_client

Example usage

Setup the connection

Setup the connection to your Action Cable server.

from actioncable_client.connection import Connection
# We advise you to use OAuth2 connections, but you can use Bearer Token or any other authentication mechanism.
# Here we use the [deviceFlow](https://www.rfc-editor.org/rfc/rfc8628)
from requests_oauth2client import OAuth2Client, OAuth2DeviceCodeAuth
from urllib.parse import urlparse

# just to get URL from string
basehost = 'https://example.com'
basehost_uri = urlparse(basehost)
secured = basehost_uri.schema == 'https'

# Create the authentication for OAuth2
client = OAuth2Client(
    token_endpoint='https://example.com/oauth/token',
    device_authorization_endpoint='https://example.com/oauth/authorize_device',
    client_id='7afe93b6-6063-11ef-b51a-9ea0527474b9', # The UUID of your application
    # If you accept testing agains not https servers, don't forget to set this to true
    # This is because requests_oauth2client do not accept custom ports (and they are right)
    testing=(not secured or (basehost_uri.port is not None and basehost.port != 443))
)
# Get your device code to display This is linked to the Device Flow
# I put a device_id which will get authorization from the resource_owner to acces with a given scope
device_auth_resp = self._client.authorize_device(device_id='7afe93b6-6063-11ef-b51a-9ea0527474b9', scopes='my_scope')

connection = Connection(
    url=f'{'wss' if secured else 'ws'}://{basehost_uri.netloc}/cable',
    # This will force the origin. If header is a BaseOAuth2RenewableTokenAuth subclass like
    # OAuth2DeviceCodeAuth is, then it will take the auth server as origin by default (so let it blank)
    origin='https://example2.com',
    header=OAuth2DeviceCodeAuth(client, device_auth_resp)
)
connection.connect()

Subscribe to a channel

from actioncable_client.subscription import Subscription

subscription = Subscription(connection, identifier={'channel': 'YourChannelCLassName'})

def on_receive(message: dict):
  print('New message arrived!')
  print('Action: {} | Data: {}'.format(message['action'], message['data']))

subscription.on_receive(callback=on_receive)
subscription.create()

Send data

from actioncable_client.message import Message

message = Message(action='update_something', data={'something': 'important'})

subscription.send(message)

Unsubscribe

subscription.remove()

Using ActionCable Channel's interface

Create a Channel class inheriting from ActionBaseChannel class

from actioncable_client.action_base_channel import ActionBaseChannel

from typing import Any
import logging
logger = logging.getLogger(__name__)

class MyChannel(ActionBaseChannel):
    def hello(self, data: dict[str, Any]):
        logger.info(f"Just received a Hello command with: {data}")
    def do_hello(self, data={}):
        self.transmit(action="hello", data=data)

Then after or before the connection just instantiate it.

my_channel = AncDeviceChannel(self._cable_connection)

It will subscribe to the ActionCable server on the channel named 'MyChannel'. You'll get all the hello actions from the server. And by calling the do_hello with some data you'll transmit the data in parameter to the action you specified (here hello on the server).

my_channel.do_hello({'from': 'My device'})

Close connection

connection.disconnect()

Development

Pull it up!

You need help?

Ask a question on StackOverflow with the tag 'actioncable-client'.

Contribution

Create pull requests on GitLab and help us to improve this package. There are some guidelines to follow:

  • Follow the conventions
  • Test all your implementations
  • Document methods which aren't self-explaining
  • try to follow the Roadmap

Copyright (c) 2024 Liant SASU, MIT license

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

actioncable_client-0.3.1a2.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

actioncable_client-0.3.1a2-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file actioncable_client-0.3.1a2.tar.gz.

File metadata

  • Download URL: actioncable_client-0.3.1a2.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for actioncable_client-0.3.1a2.tar.gz
Algorithm Hash digest
SHA256 891309dd778cd16d839ba5775be9afc2a79b17fed45d9b4256293491be374e55
MD5 96730e95d722ecff16d5f6cf5272f469
BLAKE2b-256 13bff53583ce35ad78e4e0c4b8ab1fcdedfe4c6cb1970cf199c644cb31aa10ef

See more details on using hashes here.

File details

Details for the file actioncable_client-0.3.1a2-py3-none-any.whl.

File metadata

File hashes

Hashes for actioncable_client-0.3.1a2-py3-none-any.whl
Algorithm Hash digest
SHA256 df9e8cf2876214d712af78be99adcca94568deb86a0b00b88ef7dddb2b1cae7a
MD5 ce1e4dba4c1459e512b4975a1a072ca3
BLAKE2b-256 df9915ebd11b6ef41f4acbdaf62a6ce42a15799e16ee165e8fa6a060f05e0b1c

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