centrifugal / adjacent

Centrifugo v1 integration with Django framework, for Centrifugo v2 use cent Python client to integrate
https://github.com/centrifugal/cent
32 stars 5 forks source link

Centrifugo server integration with Django framework.

Installation:

pip install adjacent

Add adjacent into INSTALLED_APPS

Add some settings to your settings.py file:

CENTRIFUGE_ADDRESS = 'http://centrifuge.example.com'
CENTRIFUGE_SECRET = 'your secret key from Centrifugo'
CENTRIFUGE_TIMEOUT = 10

After this you get the follows:

1) Function to create connection parameters for user:

from adjacent import get_connection_parameters

params = get_connection_parameters(user)

2) Client wrapper to send messages into Centrifuge

from adjacent import Client

client = Client()

# add some messages to publish
client.publish("channel", {"data": "first"})
client.publish("channel", {"data": "second"})

# actually send request with 2 publish messages added above to Centrifuge
response = client.send()

Client methods:

In template

When you passed connection parameters to template you can initialize Centrifuge javascript object in this way:

var centrifuge = new Centrifuge({
    url: '{{ params.sockjs_endpoint }}',
    user: '{{ params.user }}',
    timestamp: '{{ params.timestamp }}',
    info: '{{ params.info|safe }}',
    token: '{{ params.token }}'
});