Add WAMP features to existing Twisted App

#1

Hi,

I would like to add some WAMP features to my current twisted services.

Most of my services look like that (simplified skeleton) :

class ESA(LineReceiver):

class ESAClientFactory(ReconnectingClientFactory):

def buildProtocol(self, addr):
    self.resetDelay()
    return ESA()

def lineReceived(self, line):
    msg = json.loads(line)
    handleMsg(msg)

if name == “main”:
f = ESAClientFactory()
reactor.connectSSL(HOST,
443, f, ClientContextFactory())
reactor.run()

``

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.

Thanks,

Freddy

0 Likes

#2

(obviously I pasted the “lineReceived()” in the wrong class, it goes into the ESA class)

···

On Thursday, 10 November 2016 10:38:18 UTC, Freddy wrote:

Hi,

I would like to add some WAMP features to my current twisted services.

Most of my services look like that (simplified skeleton) :

class ESA(LineReceiver):

class ESAClientFactory(ReconnectingClientFactory):

def buildProtocol(self, addr):
    self.resetDelay()
    return ESA()

def lineReceived(self, line):
    msg = json.loads(line)
    handleMsg(msg)

if name == “main”:
f = ESAClientFactory()
reactor.connectSSL(HOST,
443, f, ClientContextFactory())
reactor.run()

``

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.

Thanks,

Freddy

0 Likes

#3

Hi Freddy,

I’m starting down the road to adding WAMP to our existing Twisted-based application, and it’ll need to be much like what you’re describing here:

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.

Did you end up having any luck with getting this to work?

Cheers,

Josh

0 Likes