Moksha provides a LiveWidget that handles automatically subscribing your widget to a given message topic, or list of topics. When the widget receives a new message, the onmessage JavaScript callback will be run by the client with JSON data in the json variable.
Below is an example of a really basic live widget. This widget subscribes to the ‘stuff’ message topic, and will perform an alert upon new messages.
from moksha.api.widgets.live import LiveWidget
class MyLiveWidget(LiveWidget):
topic = 'stuff'
onmessage = 'alert(json);'
template = "Hi, I'm a live widget!"
from moksha.api.widgets.feed.live import LiveFeedWidget
The LiveFeedWidget itself is just a simple LiveWidget that uses a little bit of jQuery to add and remove feed entries from a list.
from moksha.api.widgets.live import LiveWidget
from moksha.api.widgets.feed import Feed
class LiveFeedWidget(LiveWidget):
""" A live streaming feed widget """
params = {
'url': 'The feed URL',
'topic': 'A topic or list of topics to subscribe to',
'feed': 'A moksha Feed object',
}
template = '${feed(id=id, url=url)}'
onmessage = """
$.each(json, function() {
$("#${id} ul li:last").remove();
$("<li/>").html(
$("<a/>")
.attr("href", this.link)
.text(this.title))
.prependTo($("#${id} ul"));
});
"""
feed = Feed()