Wildcard Subscriptions for Tightly-Coupled Interactions
Apps can subscribe to multiple topics that all belong to a category using wildcards in the topic name for the subscription. This is most useful, typically, in tightly-coupled interactions where topic names are well known and follow a pattern.
Note: | You cannot use wildcard subscriptions with loosely-coupled interactions that are wired in Mashboard. |
Wildcards can be used to represent any
token within a topic name. This is based on the
OpenAjax Hub 2.0 Specification definition that topic names are in the form:
token.token.token...
And only the first token is required.
You can use the asterisk * character as a wildcard for a given token within a topic name. Or use ** as a wildcard for the last token and all subsequent tokens.
This example subscribes to any topic with a three-token name beginning with division. and ending with .notice, such as division.east.notice or division.namerica.notice:
...
onLoad: function(app) {
this.app = app;
var self = this;
...
app.subscribe("division.*.notice", function(topic,msg) {
//subscription handler
}, self);
...
},
...
Or this example, which subscribes to any topic starting with patient.:
...
onLoad: function(app) {
this.app = app;
var self = this;
...
app.subscribe("patient.**", function(topic,msg) {
//subscription handler
}, self);
...
},
...
Subscription Handlers for Wildcard Topic Names
A wildcard subscription provides a single handler for multiple subscriptions. If the apps are not secured, the handler can simply check the published topic name in each message to determine what to do with the message. For example:
...
app.subscribe("patient.**", function(topic,msg) {
if (topic === "patient.add") {
addPatient(msg);
} else if (topic === "patient.transfer") {
transferPatient(msg);
} else {
console.error("unknown patient event");
}
}, self);
...
},
...
If the apps are secured, however, the published topic name is generated during the process of tunnelling through the secured <iframe>, so topic names will not match. To overcome this, it is a better practice to check the message payload to determine which event a message represents. For example:
...
app.subscribe("patient.**", function(topic,msg) {
if (msg.pt.status === "new") {
addPatient(msg);
} else if (msg.pt.status === "transfer") {
transferPatient(msg);
} else {
console.error("unknown patient event");
}
}, self);
...
},
...