Using Bayeux with Persevere

by Kris ZypOctober 9th, 2008

Recently I integrated Jetty’s cometD implementation of Bayeux with Persevere. With this new functionality, Bayeux-based Comet communication can be used to route messages through Persevere’s stored objects. The PubSub system provided by Bayeux is powerful paradigm for building Comet applications, but often applications need to make further integrations with the data model for more sophisticated messaging logic. With Persevere, the PubSub messages are delivered through persisted data resources so close integration and data-driven logic for message routing is very straightforward.

Persevere integrates with Bayeux’s PubSub system by equating topics or channels with the URLs associated with persisted objects in Persevere. Messages are published to these objects, and these objects can include logic for how to react to the messages.

To utilize Bayeux with Persevere, simply download and run Persevere to get started. From the browser, we can initiate a Bayeux connection using Dojo’s Cometd client:


To subscribe to messages, we subscribe to the URL of the object resources that we want to listen to. If we wanted to subscribe to all the messages sent to the users, the objects in the User table, we could do:

	... handle message from the server

Now we can send a message to a specific user by their URL/id:

dojox.cometd.publish("/User/1","Just wanted to say hello");

Furthermore, we can utilize the PersevereStore (based on JsonRestStore) to interact with the database and perform queries. Then we can use the identities from objects as the topics for messaging.

By default, messages are passed through objects without modification in Persevere. However, we can easily write our own message handlers. When a message is sent to an object the message method is called. To create our own custom handler, we can simply define our own message method, this can be done at the Persevere console, or you can create a message function in the REST Object browser in Persevere (this action will persist this method to the definition for the User in the database):

User.prototype.message = function (message) {
    console.log("got message", message);
    return message + " just added a little something";

Or we can set it using the REST interface:

PUT /User.prototype
{message: function (message) {
    console.log("got message", message)
    return message + ", just added a little something"

All the User instance objects in Persevere inherit from User.prototype. Now when we send a message to a User object from Bayeux, our message method will be called. The method will output a message to the console and then append some text to the message before it is delivered to all the subscribers. If we were to publish “Hi there” to User/1, all the subscribers would get a message “Hi there, just added a little something”. This method also increments the user’s popularity property; Persevere is a persistent JavaScript environment, property changes are persisted in the database.

The execution of the message method and subsequent delivery of a message to subscribers does not need to originate only with a Bayeux publish message. We can execute this method using the console:

load("User/1").message("How are you doing?");

Or through JSON-RPC:

POST /User/1
{"id": "call1", "method": "message", "params": ["What's up?"]}

After message has successfully executed subscribers will receive the data returned from the call. If the message method throws an exception, no notification will be sent to subscribers (this can be used to cancel the delivery).

The client-side code demonstrated in this article is included in the Persevere distribution under /examples/cometd.html.

Combining Persevere’s data storage and server-side JavaScript environment with Jetty’s easy to use Cometd/Bayeux implementation provides a powerful PubSub system with tight integration with a data model. Furthermore, with Dojo’s out of the box client-side Bayeux capabilities, rapid development of real-time web applications is readily accessible with this stack.

[Slashdot] [Digg] [Reddit] [] [Facebook] [Technorati] [Google] [StumbleUpon]
SitePen, Inc. Comet Services

One Response to “Using Bayeux with Persevere”

  1. SpringOne 2008 Day 4 « Incremental Operations Says:

    [...] Bayeux - JMS for the web - or asynchronous messages over HTTP [...]

Leave a Reply

Copyright 2014 Comet Daily, LLC. All Rights Reserved