centrifuge-python
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.dumpsbefore 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.loadsinternally before passing to your code.
When using Protobuf protocol:
- all payloads you pass to the library must be
bytesorNoneif optional. If you pass non-bytesdata – exception will be raised. - all payloads received from the library will be
bytesorNoneif not present. - don't forget that when using Protobuf protocol you can still have JSON payloads - just encode them to
bytesbefore passing to the library.
Custom TLS configuration
When connecting to a wss:// endpoint the SDK uses the default TLS context of the ssl module – i.e. server certificates are verified against the system CA store. To customize TLS – for example to trust a custom CA – pass your own ssl.SSLContext as ssl_context option:
import ssl
ssl_ctx = ssl.create_default_context(cafile="/path/to/ca.pem")
client = Client(
"wss://localhost:8000/connection/websocket",
ssl_context=ssl_ctx,
)
The same option allows disabling certificate verification entirely – only do this for local development, never in production:
import ssl
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = False
ssl_ctx.verify_mode = ssl.CERT_NONE
client = Client(
"wss://localhost:8000/connection/websocket",
ssl_context=ssl_ctx,
)
Connecting through a proxy
By default the proxy configuration is taken from the environment (WS_PROXY/WSS_PROXY, HTTP_PROXY/HTTPS_PROXY, honoring NO_PROXY). To set the proxy explicitly – use proxy option of Client constructor:
client = Client(
"ws://localhost:8000/connection/websocket",
proxy="http://user:pass@proxy-host:3128",
)
Pass proxy=None to always connect directly, ignoring the environment configuration.
SOCKS proxies (socks5://...) are supported too, but require the python-socks package to be installed:
pip install python-socks
Proxy support in websockets appeared in version 15.0 – with older versions the client always connects directly, ignoring both the proxy option and the environment configuration. Setting a proxy URL there raises ValueError from the Client constructor, so that the option does not silently do nothing. Invalid proxy URLs and a missing python-socks package are reported the same way.
A couple of things to keep in mind when going through a proxy:
- with a
wss://address the proxy only sees theCONNECT host:portrequest – the WebSocket traffic inside the tunnel stays encrypted end to end, and the server certificate is still verified as usual. With aws://address the proxy sees everything, including the connection token. - credentials in an
http://proxy URL are sent to the proxy as a base64-encodedProxy-Authorizationheader over an unencrypted connection. They are never forwarded to the Centrifugo server, but use anhttps://proxy if the proxy connection itself may be observed.
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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file centrifuge_python-0.5.2.tar.gz.
File metadata
- Download URL: centrifuge_python-0.5.2.tar.gz
- Upload date:
- Size: 66.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07f26b3d29400ebca14caab72e07a7077020b07b6950463dc1f68e0ee44bcdd5
|
|
| MD5 |
e9edfba59f12cdc4500211567ac2f3bb
|
|
| BLAKE2b-256 |
c3eac6df1e3fd1ce0c530e673e1eba7222e151acf14df5fbd3d2808d754c72b1
|
File details
Details for the file centrifuge_python-0.5.2-py3-none-any.whl.
File metadata
- Download URL: centrifuge_python-0.5.2-py3-none-any.whl
- Upload date:
- Size: 35.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f38b6dbe256cb209fafa68b133abdc193495a6e9ed32ec574da512a7a3f77d61
|
|
| MD5 |
4cde474039729d95bcac7d7200de2875
|
|
| BLAKE2b-256 |
5195596f31aa1631a0ffa571fe43a7e5f4641b52bec3cf5c5d45b7b44320348f
|