Search code examples
pythonkivyautobahn

Kivy GUI with autobahn WAMP


And I am trying to combine between kivy application and autobahn wamp. For start I want to make the most basic app which will show a label and change it when a publish command will come.

This is my basic kivy App:

class MyFrontendComponent(App):

    def build(self):
        root = self.setup_gui()
        return root

    def setup_gui(self):

        self.label = Label(text='connecting...\n')
        self.layout = BoxLayout(orientation='vertical')
        self.layout.add_widget(self.label)
        return self.layout

    def changeLabel(self, text):
        self.label.text = text

if __name__ == '__main__':
    # Run the kivy app
    kivyapp = MyFrontendComponent()
    kivyapp.run()

And here is how autobahn Wamp should be implemented according to: http://autobahn.ws/python/wamp/programming.html

from autobahn.twisted.wamp import ApplicationSession
from twisted.internet.defer import inlineCallbacks


class MyComponent(ApplicationSession):

   @inlineCallbacks
   def onJoin(self, details):
      print("session ready")

      def oncounter(count):
         print("event received: {0}", count)

      try:
         yield self.subscribe(oncounter, u'com.myapp.oncounter')
         print("subscribed to topic")
      except Exception as e:
         print("could not subscribe to topic: {0}".format(e))

I have tried to use the autobahn.twisted.wamp Application using Threads because of the kivy app main loop but they are not getting syncronized

from autobahn.twisted.wamp import Application

app = Application()

@app.signal('onjoined')
def onjoined():
    kivyapp.changeLabel("realm joined!")

Can you give a n advice of how to combine between them because I search a lot with no results.


Solution

  • You need to run Kivy with Twisted support activated.

    Here is a complete example that demonstrates how to use WAMP for real-time messaging from Kivy using Crossbar.io.