Coder Social home page Coder Social logo

centrifugal / centrifuge-python Goto Github PK

View Code? Open in Web Editor NEW
28.0 6.0 12.0 187 KB

Centrifugo real-time WebSocket SDK for Python on top of asyncio

Home Page: https://centrifugal.dev

License: MIT License

Python 99.70% Makefile 0.30%
centrifugo client pubsub sdk websocket asynchronous

centrifuge-python's Introduction

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:

{
  "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

Run tests

To run tests, first start Centrifugo server:

docker pull centrifugo/centrifugo:v5
docker run -d -p 8000:8000 -e CENTRIFUGO_TOKEN_HMAC_SECRET_KEY="secret" -e CENTRIFUGO_PRESENCE=1 \
-e CENTRIFUGO_JOIN_LEAVE=true -e CENTRIFUGO_FORCE_PUSH_JOIN_LEAVE=true \
-e CENTRIFUGO_HISTORY_TTL=300s -e CENTRIFUGO_HISTORY_SIZE=100 \
-e CENTRIFUGO_FORCE_RECOVERY=true -e CENTRIFUGO_USER_SUBSCRIBE_TO_PERSONAL=true \
-e CENTRIFUGO_ALLOW_PUBLISH_FOR_SUBSCRIBER=true -e CENTRIFUGO_ALLOW_PRESENCE_FOR_SUBSCRIBER=true \
-e CENTRIFUGO_ALLOW_HISTORY_FOR_SUBSCRIBER=true centrifugo/centrifugo:v5 centrifugo

And then (from cloned repo root):

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

centrifuge-python's People

Contributors

fzambia avatar katantdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

centrifuge-python's Issues

Synchronous python client

Do you think that a synchronous python client would be possible?

The reason is because I would like to use it in some scenario where asyncio is not an option (in a Django management command).

Possible to publish to any channel?

Is it possible for clients using this library to publish to an arbitrary channel?

According to the following,

@asyncio.coroutine
def _publish(self, sub, data):
if sub.channel not in self._subs:
raise CallError("subscription not in subscribed state")

It seems like I can only publish something to a channel that the client has already subscribed. Is this feature not ready yet or should I just use https://github.com/centrifugal/cent

subscribe to private channels?

Hi!
Is it possible to subscribe to private channels with this client?
I have a private channel subscription sign but I don't know how to pass it to client.subscribe.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.