Using Moksha’s real-time pipes outside of Python

Want to add realtime functionality to your existing web site, but don’t want to have to learn Python or TurboGears?

Thankfully, Moksha allows you to leverage it’s realtime pipes on any existing web page without having to write a line of Python code.

This means that you can have your web site listen to topics from your message broker, and run javascript when new messages arrive.

Subscribing to real-time message streams

All you need to do is add a single <script> tag to your web site that specifies the topic that you wish to listen to, and the javascript function that should be called with each new message as they arrive.

The Moksha live socket will automatically handle decoding the message body to JSON before passing it to your callback.

        <script src=""></script>
        <ul id="data"/>
        function consume_message(json) {
    <script type="text/javascript" src="http://localhost:8080/livesocket?topic=helloworld&callback=consume_message"></script>

Sending messages to the broker

You can easily send messages to a given topic using the moksha.send_message function. This function will automatically handle converting your javascript objects to JSON for you.

<a href="#" onclick="moksha.send_message('helloworld', {'msg': 'Hi there!'});">Send message</a>


In order for the live sockets to work properly, Moksha (specifically Orbited) must be running under the same domain as your web site.


You can disable the automatic JSON encoding/decoding by passing json=False to the livesocket URL.