We're excited to share Ably’s newest feature, Subscription Filters. It gives subscribers to a channel the ability to easily create their own custom realtime data feeds, by selectively subscribing to receive only part of the data from the channel.
For a publisher, the advantage is being able to create personalized data streams for large numbers of individual clients, without having to worry about the scalability, maintenance, and reliability of a complicated realtime data feed infrastructure.
Benefits of Subscription Filters
With Subscription Filters, a publisher can add arbitrary metadata to a message, which can be used as the basis for filtering. The filter is set on the SDK, but it is applied on the server-side. This has two major benefits:
- A simplified channel architecture: You can satisfy the needs of different clients for different portions of the same data using a single channel.
- Cost savings: The more subscribers you have using the same filter to subscribe to the same subset of data, the greater your cost savings.
Common Use Cases
Broadly speaking, you can use Subscription Filters to:
- Give subscribers the ability to create their own custom realtime data feeds, without the publisher having to know exactly what each subscriber wants to subscribe to.
- Send data only to clients that satisfy the conditions of a filter.
Let’s have a look at a few real-life applications where it can be used:
Chat and social experiences
- In large chat rooms, users can filter out noise by choosing to only see messages from a specific user group (e.g. from friends, moderators, premium users).
- Users can create personalized live activity feeds by automatically grouping similar activities together such as most liked content first or updates from friends first, etc.
Data broadcast
Keeping complex dashboards up to date generates significant traffic between the client and the server. The number of messages received can be drastically reduced if the application only updates the active view of the dashboards. Subscription Filters make it possible to listen to channels that meet certain conditions (e.g. a view parameter).
For example, a sports data or financial data provider can conveniently limit clients’ access to different parts of the data, based on specific criteria.
How to use Subscription Filters
Subscription Filters is currently in preview, which means that more functionality will be added and changes may be made based on user feedback, but its behavior will not change dramatically.
The code below shows how you can an additional set of attributes to messages that can be used for filtering. These attributes are included in the messages extras field, similar to push, using extras.headers.
realtime.channels.get('foo').publish({
name: 'foo',
data: 'msg',
extras: {
headers: {
key_1: "value-1",
key_2: 2,
key_3: true,
}
}
});
You can subscribe to a channel with a filter expression that determines which messages you will receive on that channel. This is done with a special qualifier called filter on the channel name which accepts the filter expression as a parameter.
realtime.channels.getDerived('foo', {
filter: '<FILTER_EXPRESSION>'
}).subscribe(...);
Read the documentation for Subscription Filters (Preview).
Watch Subscription Filters in action
Below is a short demo of this feature in action. The use case is a stock inventory dashboard that gives Country Managers realtime updates on all global inventory stored in different locations across the world:
- While managers in the central office can have an exact overview of the entire stock, regional branch managers can set up filters to only receive information on the category, product, location, or quantity range. For example, the Paris managers will only see the state of size 6 shoes in France, while the London managers will care about the same brand size 6 shoes, but across the UK warehouses.
- Custom alerts can be configured for low-stock alerts and out-of-stock alerts. These are entirely defined by the client and are derived from the raw change events occurring in the inventory list.
These are the attributes we used to create the filters in this demo:
var testData = {
name: 'filtered',
data: 'This should be filtered',
extras: {
headers: {
name: 'update',
category: 'computing',
location: 'London',
product: '"Laptop"',
quantity: '10'
},
},
};
var filterOption = {
filter: 'headers.category: 'computing` && headers.location == `"London"`',
};
...
/* set up realtime */
....
var rtChannel = realtime.channels.getDerived('chan', filterOption);
/* subscribe to event */
rtChannel.subscribe('filtered', function (msg) {
To find out more about how Subscription Filters works, you can read the documentation here.
Tell us what you think
We'd love to get your feedback on this new Ably feature. What do you want to use it for? Are there any additions or changes you'd like to see in order to better fit your use case? Please get in touch and let us know what you think.