docs/main/UsingLiveSocketOutsideOfMoksha.rst | 32 +++++++++++++++++++++++----
moksha/controllers/root.py | 9 +++++--
moksha/templates/amqp_socket.mak | 23 +++++++++++++++----
moksha/templates/stomp_socket.mak | 18 +++++++++------
4 files changed, 64 insertions(+), 18 deletions(-)
New commits:
commit 8facec0398ec0d215a19d57f99965e52bb743a52
Author: Luke Macken <lmacken(a)redhat.com>
Date: Tue Jan 18 14:03:06 2011 -0500
Fetch our AMQP javascript from the appropriate server/port/path
diff --git a/moksha/controllers/root.py b/moksha/controllers/root.py
index 83dd198..ac51c1d 100644
--- a/moksha/controllers/root.py
+++ b/moksha/controllers/root.py
@@ -90,6 +90,11 @@ class RootController(BaseController):
data['orbited_url'] = '%s://%s:%s' %
(data['orbited_scheme'],
data['orbited_host'], data['orbited_port'])
+ environ = pylons.request.environ
+ data['server'] = '%s://%s:%s/%s' %
(environ.get('wsgi.url_scheme', 'http'),
+ environ['SERVER_NAME'],
+ environ['SERVER_PORT'],
+ environ['toscawidgets.prefix'])
return data
@expose('mako:moksha.templates.login')
diff --git a/moksha/templates/amqp_socket.mak b/moksha/templates/amqp_socket.mak
index f982ae3..432c68d 100644
--- a/moksha/templates/amqp_socket.mak
+++ b/moksha/templates/amqp_socket.mak
@@ -58,9 +58,9 @@ if (typeof moksha_amqp_conn == 'undefined') {
Orbited.settings.port = ${orbited_port};
Orbited.settings.hostname = '${orbited_host}';
Orbited.settings.streaming = true;
-
$.getScript('/toscawidgets/resources/moksha.api.widgets.amqp.widgets/static/amqp.protocol.js',
function() {
-
$.getScript('/toscawidgets/resources/moksha.api.widgets.amqp.widgets/static/amqp.protocol_0_10.js',
function() {
-
$.getScript('/toscawidgets/resources/moksha.api.widgets.amqp.widgets/static/qpid_amqp.js',
function() {
+
$.getScript('${server}/toscawidgets/resources/moksha.api.widgets.amqp.widgets/static/amqp.protocol.js',
function() {
+
$.getScript('${server}/toscawidgets/resources/moksha.api.widgets.amqp.widgets/static/amqp.protocol_0_10.js',
function() {
+
$.getScript('${server}/toscawidgets/resources/moksha.api.widgets.amqp.widgets/static/qpid_amqp.js',
function() {
moksha_amqp_conn = new amqp.Connection({
host: '${amqp_broker_host}',
port: ${amqp_broker_port},
commit fad10c338537385946e4cf0294b3b98280987042
Author: Luke Macken <lmacken(a)redhat.com>
Date: Tue Jan 18 14:02:35 2011 -0500
Update our "Using the live socket outside of moksha" documentation
diff --git a/docs/main/UsingLiveSocketOutsideOfMoksha.rst
b/docs/main/UsingLiveSocketOutsideOfMoksha.rst
index daa193c..f42b7f7 100644
--- a/docs/main/UsingLiveSocketOutsideOfMoksha.rst
+++ b/docs/main/UsingLiveSocketOutsideOfMoksha.rst
@@ -10,10 +10,16 @@ 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.
+
.. code-block:: html
<html>
@@ -24,14 +30,32 @@ that should be called with each new message as they arrive.
<ul id="data"/>
</body>
<script>
- function consume_message(msg) {
- $('<li/>').text(msg).appendTo('#data')
+ function consume_message(json) {
+ $('<li/>').text(json.msg).appendTo('#data')
}
</script>
<script type="text/javascript"
src="http://localhost:8080/livesocket?topic=helloworld&callback=consume_message"></script>
</html>
+
+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.
+
+.. code-block:: html
+
+ <a href="#" onclick="moksha.send_message('helloworld',
{'msg': 'Hi there!'});">Send message</a>
+
+
+.. note::
+
+ In order for the live sockets to work properly, Moksha (specifically
+ Orbited) must be running under the same domain as your web site.
+
.. note::
- 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.
commit 208ee045e0ede9bc7b322bc89d910cc282175316
Author: Luke Macken <lmacken(a)redhat.com>
Date: Tue Jan 18 14:02:14 2011 -0500
Make the auto JSON decoding/encoding of our live socket configurable
diff --git a/moksha/controllers/root.py b/moksha/controllers/root.py
index 79eaf18..83dd198 100644
--- a/moksha/controllers/root.py
+++ b/moksha/controllers/root.py
@@ -58,7 +58,7 @@ class RootController(BaseController):
return DefaultRootController(), remainder
@expose()
- def livesocket(self, topic=None, callback=None):
+ def livesocket(self, topic=None, callback=None, json=True):
"""Returns a raw Moksha live socket, for use in non-Moksha apps.
<script> function bar(msg) { alert('bar(' + msg + ')'); }
</script>
@@ -67,7 +67,7 @@ class RootController(BaseController):
</script>
"""
- data = {'topic': topic, 'callback': callback}
+ data = {'topic': topic, 'callback': callback, 'json':
json}
backend = config.get('moksha.livesocket.backend',
'stomp').lower()
if backend == 'stomp':
override_template(self.livesocket,
'mako:moksha.templates.stomp_socket')
diff --git a/moksha/templates/amqp_socket.mak b/moksha/templates/amqp_socket.mak
index 667119f..f982ae3 100644
--- a/moksha/templates/amqp_socket.mak
+++ b/moksha/templates/amqp_socket.mak
@@ -5,7 +5,11 @@ moksha = {
accept_mode: 1,
acquire_mode: 1,
destination: 'amq.topic',
- _body: $.toJSON(body),
+% if json:
+ _body: JSON.stringify(body),
+% else:
+ _body: body,
+% endif
_header: {
delivery_properties: {
routing_key: topic
@@ -21,9 +25,18 @@ if (typeof moksha_amqp_conn == 'undefined') {
moksha_amqp_queue = null;
moksha_amqp_on_message = function(msg) {
var dest = msg.header.delivery_properties.routing_key;
+ var json = msg.body;
+% if json:
+ try {
+ var json = JSON.parse(msg.body);
+ } catch(err) {
+ console.log("Unable to decode JSON message body:");
+ console.log(f.body);
+ }
+% endif
if (moksha_callbacks[dest]) {
for (var i=0; i < moksha_callbacks[dest].length; i++) {
- moksha_callbacks[dest][i](msg.body);
+ moksha_callbacks[dest][i](json);
}
}
}
diff --git a/moksha/templates/stomp_socket.mak b/moksha/templates/stomp_socket.mak
index bcf252a..cbe1383 100644
--- a/moksha/templates/stomp_socket.mak
+++ b/moksha/templates/stomp_socket.mak
@@ -1,7 +1,11 @@
moksha = {
/* Send a STOMP message to a given topic */
send_message: function(topic, body) {
- stomp.send($.toJSON(body), topic)
+% if json:
+ stomp.send(JSON.stringify(body), topic)
+% else:
+ stomp.send(body, topic)
+% endif
},
}
@@ -41,18 +45,18 @@ if (typeof TCPSocket == 'undefined') {
};
stomp.onmessageframe = function(f){
var dest = f.headers.destination;
-/*
- var json = null;
+ var json = f.body;
+% if json:
try {
- var json = $.parseJSON(f.body);
+ var json = JSON.parse(f.body);
} catch(err) {
console.log("Unable to decode JSON message body:");
- console.log(f.body);
+ console.log(f);
}
-*/
+% endif
if (moksha_callbacks[dest]) {
for (var i=0; i < moksha_callbacks[dest].length; i++) {
- moksha_callbacks[dest][i](f.body);
+ moksha_callbacks[dest][i](json);
}
}
};