Wednesday, December 31, 2008

Multicast feature added to UniversalComet / Chat sample app

One of the goals of my project UniversalComet is to enable client-only P2P applications. I thus added the feature to subscribe to scopes and do multicast posts to all subscribers. This enables "discovering" peers and talking to all of them at once.

The feature could be especially useful for things like widgets or facebook apps.

Using this feature I created a sample chat application.

The chat example is hosted on the Google Code SVN repository. There is no server side magic involved. The whole chat is implemented in a couple lines of JavaScript code (using jQuery for DOM access):

$(document).ready(function () {

// Output a message
var log$ = $("#log");
function log(msg) {
log$.text(""+msg + "\n" + log$.text())
}

// Make me a server with Google Gears support enabled
var server = new Addressable.Server({ useGears: true });

// When a message arrives, just "log" it
server.onmessage = function (message) {
log(message)
}

// Out subscription scope is the current URL
var scope = location.href;

// Connect to the UniversalComet server
server.connect(function (id, url) {
log("Connected.")
// When connected subcribe to the scope
server.subscribe(scope, function () {
log("Joined channnel")
// When subscribed, post "Hi All"
server.post(scope, "Hi All")
})

// Subscription expire after 5 Minutes. Renew ours every 4 Minutes.
setInterval(function () { // renew subscription
server.subscribe(scope, function () {})
}, 1000 * 4 * 60)
})

// When the "chatForm" is submitted post the message to the scope on UniversalComet
var message = $("#message").focus();
$("#chatForm").submit(function (e) {
server.post(scope, message.val())
message.val("")
e.preventDefault() // Dont really submit the form
})

})

Security
Anyone can subscribe and post to any scope. You can work around this by using "secret" scope names and doing the subscription and posts on your server to work around this. I might add Authentication to the process later. However, UniversalComet is meant as a low-level API for doing Comet. There is nothing stopping you to layer any authentication layer on top of it that you like.

Compatibility
UniversalComet has now been tested on the following platforms
  • FF3 on Mac (With and without Google Gears)
  • FF2 on Windows (With and without Google Gears)
  • Safari 3 on Mac
  • IE7
  • iPhone
  • Android
Post a Comment