Skip to main content

Bug Tracker

Side navigation

#7547 closed enhancement (plugin)

Opened November 17, 2010 10:32PM UTC

Closed September 21, 2011 11:41PM UTC

Last modified September 22, 2011 09:07AM UTC

Adding pub/sub into jQuery core?

Reported by: cowboy Owned by:
Priority: low Milestone: 1.7
Component: event Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:
Description

Using the native events system, we can add a fairly robust Pub/Sub implementation into jQuery in under 200 bytes.

https://gist.github.com/661855

Here are my comments from that gist:

I frequently see comments about how jQuery's events system has unnecessary overhead that precludes it from being used as the core of a Pub/Sub implementation. The jQuery events system is tried-and-true, having been architected to be both fast and robust, and the vast majority of users of this plugin should never encounter any kind of performance issues.

Because this plugin's $.subscribe, $.unsubscribe and $.publish methods all use the jQuery .bind(), .unbind() and .trigger() methods internally, those methods' complete signatures are available to you.

This includes being able to subscribe to multiple topics simultaneously as well as specify data at both the time of subscription as well as at the time of publishing. It allows you to use namespaces for more control over unsubscribing and publishing. It even gives each bound callback an event object with many useful properties, like .type (topic) and .timeStamp.

Just use this handy terminology guide (jQuery events term → Pub/Sub term), and everything should make sense:

  • bind → subscribe
  • unbind → unsubscribe
  • trigger → publish
  • type → topic

In addition, should you need it, these methods are fully compatible with the jQuery.proxy() method, in case you not only want more control over to which context the subscribed callback is bound, but want to be able to very easily unsubscribe via callback reference.

Regarding performance: If at some point, your application starts processing so many messages that performance issues start to develop, you could always write a replacement "jQuery Not-So-Tiny Pub/Sub" plugin with the same API and just drop it in as a replacement to this plugin. But keep in mind that you'll also need to add in the aforementioned features, too.

// Super-basic example:

function handle( event, a, b, c ) {
  console.log( event.type, a + b + c );
};

$.subscribe( "/some/topic", handle );

$.publish( "/some/topic", [ "a", "b", "c" ] );
// logs: /some/topic abc

$.unsubscribe( "/some/topic", handle ); // Unsubscribe just this handler

// Or:

$.subscribe( "/some/topic", function( event, a, b, c ) {
  console.log( event.type, a + b + c );
});

$.publish( "/some/topic", [ "a", "b", "c" ] );
// logs: /some/topic abc

$.unsubscribe( "/some/topic" ); // Unsubscribe all handlers for this topic
Attachments (0)
Change History (18)

Changed November 17, 2010 10:40PM UTC by ajpiano comment:1

I know that this conversation will largely turn to implementation questions, but before that happens, I would just like to voice my support for this. When jQuery adds "Good Ideas" like pubsub - or event delegation - into core, it encourages and enlightens broad swathes of developers to techniques and patterns that improve their code and their skill as developers. +1

Changed November 17, 2010 11:02PM UTC by peol comment:2

Replying to [comment:1 ajpiano]:

I know that this conversation will largely turn to implementation questions, but before that happens, I would just like to voice my support for this. When jQuery adds "Good Ideas" like pubsub - or event delegation - into core, it encourages and enlightens broad swathes of developers to techniques and patterns that improve their code and their skill as developers. +1

I agree wholeheartedly. It's also lightweight and follows the usefulness that is jQuery overall.

Changed November 17, 2010 11:03PM UTC by shadedecho comment:3

+1 for adding pub/sub to jquery core, however the underlying implementation ends up being.

Changed November 18, 2010 06:01AM UTC by addyosmani comment:4

+1 I completely agree that this would be an excellent addition.

Changed November 18, 2010 05:09PM UTC by rwaldron comment:5

+1 to simple, minimal overhead publish/subscribe systems.

Changed November 18, 2010 05:09PM UTC by rwaldron comment:6

component: unfiledevent
priority: undecidedlow

Changed November 19, 2010 09:40PM UTC by SlexAxton comment:7

Would this have the behavior of splitting up publishes and subscribes with spaces in them?

$.subscribe('something here', func(){});

would actually listen for 'something' and 'here' separately and then subsequently fire the handler twice whenever it's published.

Is this (al)right?

Changed November 21, 2010 04:53AM UTC by snover comment:8

milestone: 1.5
status: newopen

Changed November 21, 2010 12:46PM UTC by peol comment:9

Replying to [comment:7 SlexAxton]:

Would this have the behavior of splitting up publishes and subscribes with spaces in them? $.subscribe('something here', func(){}); would actually listen for 'something' and 'here' separately and then subsequently fire the handler twice whenever it's published. Is this (al)right?

I would say so, yes. If it's documented.

Worst case scenario, $.subscribe and $.publish could simply handle whitespace, replacing it with underscores or somesuch.

Changed November 24, 2010 05:58PM UTC by rwaldron comment:10

milestone: → 1.5

Changed February 14, 2011 10:29PM UTC by danheberden comment:11

milestone: → 1.6

Changed March 30, 2011 07:44AM UTC by anonymous comment:12

You already have pub/sub in jQuery, just the syntax is different. Bind/Trigger is not limited to click events and the likes, just specify what you like..

jQuery(document).bind("/myEvent", function(){});

jQuery(document).triggerHandler("/myEvent");

Changed April 16, 2011 04:19PM UTC by john comment:13

resolution: → wontfix
status: openclosed

This was discussed in the jQuery 1.6 roadmap meeting and we agreed that this was not something that we were going to put into core (leave as a plugin).

Changed August 03, 2011 06:10PM UTC by timmywil comment:14

milestone: 1.6
resolution: wontfix
status: closedreopened

With the introduction of $.Callbacks() in 1.7, jaubourg's implementation of pub/sub in https://github.com/jquery/jquery/commit/96b0089c9ce5c65f9d002966256d90e778345a2a seems a more plausible solution if pub/sub was going to be added to core. However, this could also easily be a new plugin (even an official one), which is where I am leaning at this point. Thoughts?

Changed August 03, 2011 06:10PM UTC by timmywil comment:15

keywords: → needsreview
milestone: → 1.7
status: reopenedopen

Changed August 14, 2011 01:31AM UTC by daniel.c.herman@gmail.com comment:16

+1, I'd love to see this in jQuery.

Changed September 21, 2011 11:41PM UTC by timmywil comment:17

keywords: needsreview
resolution: → plugin
status: openclosed

We've decided to implement this as a plugin for now.

Changed September 22, 2011 09:07AM UTC by addyosmani comment:18

Just posting a reminder for us to include a link to the plugin repo/gist when we get a chance.