Skip to main content

WebSocket SDK for Centrifugo (and any Centrifuge-based server) on top of Python asyncio library

Project description

centrifuge-python

CI pypi versions license

This is a WebSocket real-time SDK for Centrifugo server (and any Centrifuge-based server) on top of Python asyncio library.

[!TIP] If you are looking for Centrifugo server API client – check out pycent instead.

Before starting to work with this library check out Centrifugo client SDK API specification as it contains common information about Centrifugal real-time SDK behavior. This SDK supports all major features of Centrifugo client protocol - see SDK feature matrix.

Install

pip install centrifuge-python

Then in your code:

from centrifuge import Client

See example code and how to run it locally.

JSON vs Protobuf protocols

By default, SDK uses JSON protocol. If you want to use Protobuf protocol instead then pass use_protobuf=True option to Client constructor.

When using JSON protocol:

  • all payloads (data to publish, connect/subscribe data) you pass to the library are encoded to JSON internally using json.dumps before sending to server. So make sure you pass only JSON-serializable data to the library.
  • all payloads received from server are decoded to Python objects using json.loads internally before passing to your code.

When using Protobuf protocol:

  • all payloads you pass to the library must be bytes or None if optional. If you pass non-bytes data – exception will be raised.
  • all payloads received from the library will be bytes or None if not present.
  • don't forget that when using Protobuf protocol you can still have JSON payloads - just encode them to bytes before passing to the library.

Callbacks should not block

Event callbacks are called by SDK using await internally, the websocket connection read loop is blocked for the time SDK waits for the callback to be executed. This means that if you need to perform long operations in callbacks consider moving the work to a separate coroutine/task to return fast and continue reading data from the websocket.

The fact WebSocket read is blocked for the time we execute callbacks means that you can not call awaitable SDK APIs from callback – because SDK does not have a chance to read the reply. You will get OperationTimeoutError exception. The rule is the same - do the work asynchronously, for example use asyncio.ensure_future.

Run example

To run example, first start Centrifugo with config like this:

{
  "client": {
    "token": {
      "hmac_secret_key": "secret"
    }
  },
  "channel": {
    "namespaces": [
      {
        "name": "example",
        "presence": true,
        "history_size": 300,
        "history_ttl": "300s",
        "join_leave": true,
        "force_push_join_leave": true,
        "allow_publish_for_subscriber": true,
        "allow_presence_for_subscriber": true,
        "allow_history_for_subscriber": true
      }
    ]
  }
}

And then:

python -m venv env
. env/bin/activate
make dev
python example.py

Run tests

To run tests locally, start test Centrifugo server:

docker compose up

Then:

python -m venv env
. env/bin/activate
make dev
make test

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

centrifuge_python-0.4.3.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

centrifuge_python-0.4.3-py3-none-any.whl (26.4 kB view details)

Uploaded Python 3

File details

Details for the file centrifuge_python-0.4.3.tar.gz.

File metadata

  • Download URL: centrifuge_python-0.4.3.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for centrifuge_python-0.4.3.tar.gz
Algorithm Hash digest
SHA256 c7b6efc5e220d08350219b453d35f7c8b347094fd47d457b76d582346db4e04f
MD5 2b73424c91a76fbd28880707ed65312e
BLAKE2b-256 8afa225ecf40715b497dd763728355c59742e9b1834bbb8bd5a2aa803b7468ec

See more details on using hashes here.

File details

Details for the file centrifuge_python-0.4.3-py3-none-any.whl.

File metadata

File hashes

Hashes for centrifuge_python-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 016c77219f8d5973b2b7ab20ce7ff5826fae8fe89d2b34a40bf13c728603afac
MD5 6f3e69d2d40a5603d365506b56827ddc
BLAKE2b-256 7275bc482eb1ea6b4c2020d2453e6abde9f4b790a0e1cd23071163e9b33455e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page