Skip to main content

MarketHub DDN - WebSocket SDK for Centrifugo with built-in offset persistence and automatic recovery

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, first start Centrifugo server:

docker pull centrifugo/centrifugo:v6
docker run -d -p 8000:8000 \
-e CENTRIFUGO_CLIENT_TOKEN_HMAC_SECRET_KEY="secret" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_ALLOWED_DELTA_TYPES="fossil" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_DELTA_PUBLISH="true" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_PRESENCE="true" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_JOIN_LEAVE="true" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_FORCE_PUSH_JOIN_LEAVE="true" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_HISTORY_SIZE="100" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_HISTORY_TTL="300s" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_FORCE_RECOVERY="true" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_ALLOW_PUBLISH_FOR_SUBSCRIBER="true" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_ALLOW_PRESENCE_FOR_SUBSCRIBER="true" \
-e CENTRIFUGO_CHANNEL_WITHOUT_NAMESPACE_ALLOW_HISTORY_FOR_SUBSCRIBER="true" \
-e CENTRIFUGO_CLIENT_SUBSCRIBE_TO_USER_PERSONAL_CHANNEL_ENABLED="true" \
-e CENTRIFUGO_LOG_LEVEL="trace" \
centrifugo/centrifugo:v6 centrifugo

And then (from cloned repo root):

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

markethub_ddn-1.1.2.tar.gz (26.9 kB view details)

Uploaded Source

Built Distribution

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

markethub_ddn-1.1.2-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

Details for the file markethub_ddn-1.1.2.tar.gz.

File metadata

  • Download URL: markethub_ddn-1.1.2.tar.gz
  • Upload date:
  • Size: 26.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for markethub_ddn-1.1.2.tar.gz
Algorithm Hash digest
SHA256 8b402bd2344123575005fe3021041ed9ce9f4a5feb4448a149015a58ff2d90f0
MD5 12b7ebf49dad4e8ae2a3a87802c72b64
BLAKE2b-256 ecb8d217bc61fc9a536165e9a372204a98b277b7e96f8325e286f169f45e8b13

See more details on using hashes here.

File details

Details for the file markethub_ddn-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: markethub_ddn-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 29.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for markethub_ddn-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8e994fac75cad8ccc2fd56e8150c990e7a785710e33423386fccfcd1bce4362
MD5 43b5010e65f051db97bfa711a874dad1
BLAKE2b-256 4d1809263ae5633e34b0f0f32ef1d1997781c46d975382cf9625376b5c430f36

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