Vanilla JS event delegation – dealing with child elements of the target element

Newer browsers Newer browsers support .matches: this.container.addEventListener(‘click’, function(e){ if (e.target.matches(‘#game-again,#game-again *’)) { e.stopPropagation(); self.publish(‘primo:evento’); } }); You can get the unprefixed version with var matches = document.body.matchesSelector || document.body.webkitMatchesSelector || document.body.mozMatchesSelector || document.body.msMatchesSelector || document.body.webkitMatchesSelector And then use .apply for more browsers (Still IE9+). Older browsers Assuming you have to support older browsers, you can … Read more

Publishing/subscribing multiple subsets of the same server collection

Could you not just use the same query client-side when you want to look at the items? In a lib directory: enabledItems = function() { return Items.find({enabled: true}); } processedItems = function() { return Items.find({processed: true}); } On the server: Meteor.publish(‘enabled_items’, function() { return enabledItems(); }); Meteor.publish(‘processed_items’, function() { return processedItems(); }); On the client … Read more

What are the main differences between Redis Pub/Sub and Redis Stream?

Data storage Pub/Sub is a Publisher/Subscriber platform, it’s not data storage. Published messages evaporate, regardless if there was any subscriber. In Redis Streams, stream is a data type, a data structure on its own right. Messages or entries are stored in memory and stay there until commanded to be deleted. Sync/Async communication (Push/Pull) Pub/Sub is … Read more

Why would one use the Publish/Subscribe pattern (in JS/jQuery)?

It’s all about loose coupling and single responsibility, which goes hand to hand with MV* (MVC/MVP/MVVM) patterns in JavaScript which are very modern in the last few years. Loose coupling is an Object-oriented principle in which each component of the system knows its responsibility and doesn’t care about the other components (or at least tries … Read more

Difference between Observer, Pub/Sub, and Data Binding

There are two major differences between Observer/Observable and Publisher/Subscriber patterns: Observer/Observable pattern is mostly implemented in a synchronous way, i.e. the observable calls the appropriate method of all its observers when some event occurs. The Publisher/Subscriber pattern is mostly implemented in an asynchronous way (using message queue). In the Observer/Observable pattern, the observers are aware … Read more

Meteor: difference between names for collections, variables, publications, and subscriptions?

Let’s distinguish between the different names you might have to deal with when programming Meteor: Variable names, such as Posts = new Meteor.Collection(…). These are used only so your code knows how to access this variable. Meteor doesn’t know or care what it is, although the convention is to capitalize. Collection names, such as new … Read more

Redis + ActionController::Live threads not dying

A solution I just did (borrowing a lot from @teeg) which seems to work okay (haven’t failure tested it, tho) config/initializers/redis.rb $redis = Redis.new(:host => “xxxx.com”, :port => 6379) heartbeat_thread = Thread.new do while true $redis.publish(“heartbeat”,”thump”) sleep 30.seconds end end at_exit do # not sure this is needed, but just in case heartbeat_thread.kill $redis.quit end … Read more

How to unsubscribe from a socket.io subscription?

From looking at the source of socket.io.js (couldn’t find it in documentation anywhere), I found these two functions: removeListener = function(name, fn) removeAllListeners = function(name) I used removeAllListeners successfully in my app; you should be able to choose from these: socket.removeListener(“news”, cbProxy); socket.removeAllListeners(“news”); Also, I don’t think your solution of cbProxy = _blank would actually … Read more

How does the messages-count example in Meteor docs work?

Thanks for prompting me to write a clearer explanation. Here’s a fuller example with my comments. There were a few bugs and inconsistencies that I’ve cleaned up. Next docs release will use this. Meteor.publish is quite flexible. It’s not limited to publishing existing MongoDB collections to the client: we can publish anything we want. Specifically, … Read more