bubblingPubSub for jQuery and NodeJS

Today, we shared parts of the code for our new framework (VisualWeb) with the javascript community. In a first step, we published our take on pubsub : bubPubSub

bubPubSub project

description

PubSub systems are great and widely used today. However, we wanted something that emulates the bubbling architecture of DOM events.
Of course, next to publications being able to bubble, subscribers can also choose whether they would like to receive bubbled publications.
Furthermore, the system has the ability to LOG publications (if they choose to persist) and retrieve them later.
This comes in handy when using asynchronously loaded code or instantiation that needs to refer to old publications.
We created this little thing for use in our VisualWeb framework project. We use it on the serverside with node.js and on the client as jQuery plugin.

open source repository on github

Today, we (re-)opened github repositories for it here : https://github.com/itsatony/bubPubSub

contributions

As we depend on some cool core functions of jQuery we ported portions of jQuery to node. The utilitary functions are included in bubpubsub.js module file.

The code is certainly not brilliant or crazy fast, but we attempted to annotated it well and make the code easy to read, understand (and extend).
We’re very happy to learn about ideas, improvements, bugs (and fixes!) ..

contact

To get in contact with us, just comment here on my blog, use twitter ( @itsatony ) or email me at  [i @ itsatony . com ]  .

Huge thanks go out to the contributing teams at K.lab Berlin ( http://www.klab-berlin.com ) and LinkCloud ( http://www.linkcloud.org )
Happy coding to you all – the javascript and nodejs communities truly rock!

 version history

v0.70 @21.03.12

  • renamed
  • fused client (jQuery) and server (nodeJS) versions
  • series of bugfixes

v0.60 29.10.11

  • removed dependencies from jQuery.
  • enabled using the same file for client- and nodeJS server-side implementation
  • added the reply function explicitely

demo code

// SUBSCRIPTION TO a deep branch WITHOUT PICKING UP BUBBLES 
var myEuropeSubscription = aBubPubSub.subscribe(
	'/politics/europe', 
	function(data, currentBranch, publisher, replyCall, subscriptionIdObject) { 
		console.log('original publication branch: ' + data.originalTopic);  
		console.log('i subscribed to : ' + currentBranch);  
		replyCall('I am [' + myEuropeSubscription.id + '] and I am only interested in european politics');
	}, 
	{ getBubbles: false } 
	'myEuropeSubscriber'
);

// SUBSCRIPTION TO ROOT WITH BUBBLING
var myRootSubscription = aBubPubSub.subscribe(
	'/', 
	function(data, currentBranch, publisher, replyCall, subscriptionIdObject) { 
		console.log('original publication branch: ' + data.originalTopic);  
		console.log('i subscribed to : ' + currentBranch);  
		replyCall('I am [' + myRootSubscription.id + '] and I pick up any publication');
	}, 
	{ getBubbles: true } 
	'myRootSubscriber'
);

// PUBLISH WITH PERSISTANCE AND BUBBLING
aBubPubSub.publish(
	'/politics/europe/germany/pipa', 
	{ 
		content: 'this is wonderfool',  
	},
	{ 
		bubble:true, 
		persist:true 
	}, 
	'myTestPublisher1',
	function(replyCallParametersOfYourChoice) {
		console.dir(replyCallParametersOfYourChoice);
		return true;
	}
);

// PUBLISH WITH PERSISTANCE AND BUBBLING
aBubPubSub.publish(
	'/politics/europe/germany/pipa/legislation', 
	{ 
		content: 'this can not be picked up by the nonBubbler or the persistence check (below)',  
	},
	{ 
		bubble:true, 
		persist:false 
	}, 
	'myTestPublisher2',
	function(replyCallParametersOfYourChoice) {
		console.dir(replyCallParametersOfYourChoice);
		return true;
	}
);


// PERSISTANCE
var persistentPublicationObjectArrayOnPolitics = aBubPubSub.wasPublishedOnTopic('/politics', true);

// UNSUBSCRIBE
aBubPubSub.unsubscribe(myRootSubscription);

 

 

Related posts:

how to write a job application
GO LightTable, Chris Granger
Spielberichtsbogen für Basketball auswerten leicht gemacht mit Hoopsy