amplify
amplify copied to clipboard
Subscribe to multiple pubs with the same function
Sometimes I'd like to handle a number of similar topics with the same function. I'm imagining something like this:
amplify.subscribe(["foo", "bar", "baz"], function(data, topic) {
alert("got a " + topic + " with " + data);
});
For that matter, a handy feature for debugging and logging would be subscribeAll().
amplify.subscribe() can take a space-delimited list of topics currently, and will subscribe the callback for each topic. For example:
amplify.subscribe("foo bar baz", function(data) {
alert("got some data " + data);
});
Perfect! Looks like it's a documentation issue then; thanks for adding the label.
oh, wait... can you find out which topic you're receiving? Without that, it's a good deal less useful.
You probably noticed I revised my reply's code sample, since I, too, thought the callback args included topic - sorry about that. I'm chatting with the appendTo team to see about opening an issue for adding topic to the callback args....
Awesome, thanks!
Yes, maybe the documentation should mention this. My code wasn't working because I assumed that I could use event names with spaces.
@mgiulio - I have a pull request pending that will update the documentation once it's merged.
@iangilman The pull request I've opened will update the documentation (the amplify.core README) with examples on how you could capture topic inside the subscription callback.
@ifandelse Excellent!