Last active
April 27, 2016 15:29
-
-
Save jirfag/94cddddd6b60bbeb464c86cf8de14a38 to your computer and use it in GitHub Desktop.
Centrifugo in Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #static/js/answers_subscribe.js | |
| function subscribe_to_new_answers() { | |
| var $info = $('#answers-cent-data'); | |
| var centrifuge = new Centrifuge({ | |
| url: $info.data('cent-url'), | |
| user: $info.data('cent-user').toString(), | |
| timestamp: $info.data('cent-ts').toString(), | |
| info: $info.data('cent-info'), | |
| token: $info.data('cent-token'), | |
| debug: false, | |
| }); | |
| var channel = $info.data('cent-channel'); | |
| console.log('subscribe on channel ' + channel); | |
| centrifuge.subscribe(channel, function(msg) { | |
| console.log(msg); | |
| var row_html = '<tr><td>' + msg.data.text + '</td><td>' + msg.data.likes_n + '</td></tr>' | |
| $('#answers-table tr:last').after(row_html); | |
| }); | |
| centrifuge.connect(); | |
| return centrifuge; | |
| } | |
| var g_centrifuge = undefined; | |
| $(document).ready(function() { | |
| g_centrifuge = subscribe_to_new_answers(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #app/settings.py | |
| CENTRIFUGE_ADDRESS = 'http://ws.disaev.ru' | |
| CENTRIFUGE_SECRET = 'e830c79a-7e1b-4291-9c64-6eb57653c23a' | |
| CENTRIFUGE_TIMEOUT = 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #qa/templates/qa/question_detail.html | |
| {% block js %} | |
| <script src="//cdn.jsdelivr.net/sockjs/1.0/sockjs.min.js" type="text/javascript"></script> | |
| {% load staticfiles %} | |
| <script src="{% static 'js/centrifuge.js' %}"></script> | |
| <script src="{% static 'js/answers_subscribe.js' %}"></script> | |
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="answers-cent-data" | |
| data-cent-url="{{ CENTRIFUGE_SOCKJS_ENDPOINT }}" | |
| data-cent-user="{{ CENTRIFUGE_USER }}" | |
| data-cent-ts="{{ CENTRIFUGE_TIMESTAMP }}" | |
| data-cent-info="{{ CENTRIFUGE_INFO }}" | |
| data-cent-token="{{ CENTRIFUGE_TOKEN }}" | |
| data-cent-channel="{{ object.get_cent_answers_channel_name }}" | |
| > | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ... | |
| from adjacent import Client | |
| client = Client() | |
| client.publish(answer.question.get_cent_answers_channel_name(), answer.as_compact_dict()) | |
| response = client.send() | |
| print('sent to channel {}, got response from centrifugo: {}'.format(answer.question.get_cent_answers_channel_name(), response)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment