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()

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.1.dev1.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

actioncable_client-0.3.1.dev1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for actioncable_client-0.3.1.dev1.tar.gz
Algorithm Hash digest
SHA256 d9bf59e0989d48c44ee08c345e01cd01d73e54ac87abac8354d0e7a7dc57eeab
MD5 742784e641c684afb064c21302bce900
BLAKE2b-256 90620888956ff79ddc77fba62590b4b7a3cada79598a39120bab198bd546322c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for actioncable_client-0.3.1.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f0ad322a8dfaec2e8e4ce97f57158ff49989256d0971521a8fdd5d672a0caa3
MD5 80b8716ee18d58b3b325e9e9ac3ee3b9
BLAKE2b-256 aef175c8c5a335fd7c0ea2201a98993ee93b7d3169be403e5d32fd9d06b8ec83

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