I would like to add some WAMP features to my current twisted services.
Most of my services look like that (simplified skeleton) :
def buildProtocol(self, addr): self.resetDelay() return ESA() def lineReceived(self, line): msg = json.loads(line) handleMsg(msg)
if name == “main”:
f = ESAClientFactory()
443, f, ClientContextFactory())
The handleMsg(msg) part is the critical one, I need this to happen in any case. But in addition to that, I would like to publish the update on a Crossbar.io realm. That is, every time a particular message has been processed (transformed and saved in a database in my case), I want to notify whoever might be interested and tell them some details about what happened by publishing to different topics.
My naive attempt was to start from an ApplicationSession class, and use the ApplicationRunner to spawn it and connect to the wamp router. Using the runner method “run()” with start_reactor=False should allow me to reuse the reactor that already runs my twisted events.
However, from there, I’m facing some issues :
how can I get the application session object spawned from the ApplicationRunner.run() ? The idea would be to do keep a reference to that object in my ESAClientFactory, and call session_obj.notify_update() that would publish the update.
am I supposed to use the ApplicationSession at all ? And even the ApplicationRunner in the first place ?
if I were, in addition to simple publishes, to listen to events from the WAMP realm, how to integrate that events in the currently running event loops ?
Most of my existing services use this kind of Twisted workflow. Some read from rabbitMQ, other from raw TCP sockets, and are performing a critical operation that I don’t want to disrupt. So if the wamp router is down, my twisted reactor needs to continue to run and simply skip the “notify” part. So I’m looking for the less disruptive way of adding such features to my existing services.
Anybody having the same usecase ? How did you solve it ? I simply have no idea how to integrate all that.