Skip to content
ifandelse edited this page May 10, 2014 · 4 revisions

In postal.js, channels are logical partitions of topics. Many libraries providing a 'message bus' approach are single channel - meaning you primarily use a topic to subscribe and publish, and all subscriptions are effectively in a single queue/stack. Channels logically separate groups of topics into separate 'queues'. Channel names are unique, but topics can be duplicated on multiple channels. It's much like an address - the same street name - or even house number - can appear in multiple cities, but the city makes it unique from the same number & street in a different city.

Why Are Channels Useful?

On one hand, channels provide a very small performance optimization, in that when a message is published, postal does not have to traverse all subscribers to find matching subscriptions. Instead, it only traverses the subscriptions for that channel. However, that's only a side benefit of channels. One of the main points (and gains) of having channels is to encourage and enable good separation of concerns. Channels represent a logically related group of communications, and it's common to find channels dedicated, for example, to serving a module. This helps keep communication concerns segmented by bounded context.

A channel instance in postal can be passed around within a module and used to quickly call publish or subscribe. Sure, you can always call postal.publish or postal.subscribe directly - but you have to provide the full metadata necessary for a publish or subscribe, where if you're working with a channel object, you already have your channel data set up for you.

Extending Channels

The ChannelDefinition constructor function is exposed via postal.ChannelDefinition. You can extend it by adding to the prototype - just remember, if you override core members, you may break core behavior. The postal.request-response plugin is an example of extending the ChannelDefintion.prototype.

Getting an instance of a channel

// Default channel is "\"
var defaultChannel = postal.channel();
// Getting a named channel:
var uiChannel = postal.channel( "ui" );