python - Crossbar.io: How to publish a message on a topic using a Django service? -
i started using crossbar.io implement live stats page. i've looked @ lot of code examples, can't figure out how this:
i have django service (to avoid confusion, can assume i´m talking function in views.py) , i'd publish messages in specific topic, whenever gets called. i've seen these approaches: (1) extending applicationsession , (2) using application instance "runned".
none of them work me, because django service doesn't live within class, , not executed stand-alone python file either, don't find way phone call "publish" method (that thing want on server side).
i tried instance of "statsbackend", extends applicationsession, , publish something... statsbackend._instance none (even when execute 'crossbar start' , statsbackend.init() called).
statsbackend.py:
from twisted.internet.defer import inlinecallbacks autobahn import wamp autobahn.twisted.wamp import applicationsession class statsbackend(applicationsession): _instance = none def __init__(self, config): applicationsession.__init__(self, config) statsbackend._instance = self @classmethod def update_stats(cls, amount): if cls._instance: cls._instance.publish('com.xxx.statsupdate', {'amount': amount}) @inlinecallbacks def onjoin(self, details): res = yield self.register(self) print("campaignstatsbackend: {} procedures registered!".format(len(res)))
test.py:
import statsbackend statsbackend.update_stats(100) #doesn't anything, statsbackend._instance none
django blocking wsgi application, , not blend autobahnpython, non-blocking (runs on top of twisted or asyncio).
however, crossbar.io has built-in rest bridge, includes http pusher can submit events via http/post capable client. crossbar.io forwards events regular wamp subscribers (eg via websocket in real-time).
crossbar.io comes finish application template demonstrate above functionality. try:
cd ~/test1 crossbar init --template pusher crossbar start
open browser @ http://localhost:8080
(open js console) , in sec terminal
curl -h "content-type: application/json" \ -d '{"topic": "com.myapp.topic1", "args": ["hello, world"]}' \ http://127.0.0.1:8080/push
you can publish within blocking application django.
python django twisted autobahn crossbar
No comments:
Post a Comment