When using Ably for Pub/Sub over WebSockets, can I use wildcards to subscribe to multiple channels like so
var channel = ably.channels.get('foo:*')
channel.attach()
(disclaimer: I am a developer advocate for Ably, and posting and self-answering a commonly asked support question here on Stack Overflow so our users can find this more easily)
When attaching to a channel, you need to explicitly provide the channel name you are attaching to such as:
var channel = ably.channels.get('announcements')
channel.attach()
Attaching to more than one channel in a single operation is not possible, i.e. the following is not supported:
var channel = ably.channels.get('foo:*')
channel.attach()
/* attempting to attach to all channels matching the name foo:* will not work */
This is not possible for a number of reasons:
Attaching to unbounded number of channels in a single operation will not scale for your client devices or our servers terminating those connections
Channels in Ably's cluster are dynamically distributed across the available resources and move frequently. Each channel is largely autonomous and this is important to ensure the system remains reliable without a single point of failure or congestion. If a client were to subscribe to all channels matching a wildcard, then a connection would need to be maintained to every server in the cluster that could possibly be running a channel in case a channel is created that matches that wildcard. This does not scale.
If you are subscribed to wildcard channels then it is impossible to offer the data delivery guarantees and quality of service Ably provides on channels because:
However, because Ably's connections are multiplexed thus allowing you to attach and detach from any channels dynamically over the same connection, it is of course possible to effectively subscribe to wildcard channels by attaching to channels as you need them.