WebSocket SDK for Centrifugo (and any Centrifuge-based server) on top of Python asyncio library
Project description
centrifuge-python (work in progress)
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.
The features implemented by this SDK can be found in 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.
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
orNone
if optional. If you pass non-bytes
data – exception will be raised. - all payloads received from the library will be
bytes
orNone
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.
Run tests
To run tests, first start Centrifugo server:
docker run -p 8000:8000 centrifugo/centrifugo:v5 centrifugo --client_insecure --log_level debug
And then:
python -m venv env
. env/bin/activate
make dev
make test
Run example
To run example, first start Centrifugo with config like this:
{
"token_hmac_secret_key": "secret",
"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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for centrifuge_python-0.3.0b5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eaa31e8f5584c71a72870998ed660ba2ef705a9e39e22b06ad2cd7a55593341 |
|
MD5 | f87351224ec378c3b9f225a30bd273a5 |
|
BLAKE2b-256 | 94e6f9d1632ed15779ad6917cf7886149ac2c525b7fd3ce8cf7ba6bf5166b05c |
Hashes for centrifuge_python-0.3.0b5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ace15b703cd01be58612aa30ec1a4af094f0808ca2128a67e9ec28b4d8487c40 |
|
MD5 | 8d7f9bde7d8e9368c455c9397fb3f074 |
|
BLAKE2b-256 | 2b4e46b02a0f159ec97d520b23778f9296ab018d6a630fb1e7aa733c06d31217 |