# Pusher Adapter Ably enables migration from Pusher to Ably using its Pusher Adapter. The protocol adapter handles all background translation and only requires an API key change. Using an adapter introduces some latency and is slower than using an Ably SDK, however the impact is typically 1-10ms. It will also be slightly slower than using Pusher natively, but only if you are close to whichever Pusher data center used. If you aren't close to the Pusher data center you've chosen, then the extra latency from using the adapter should be more than compensated for by being able to use a data center that is close to you. This is because Ably automatically connects clients to the data center closest to them. The Pusher Adapter provides some of the advantages of Ably, such as inter-regional message federation, however others, such as [continuity guarantees](https://ably.com/four-pillars-of-dependability), fallback host support, and [message history](https://ably.com/docs/storage-history/history.md) are only available when using an Ably SDK. If an [Ably SDK](https://ably.com/docs/sdks.md) is available in your chosen platform, it is recommended you use that, or plan to transition to it eventually. > Automatic routing to the closest datacenter. > > Ably automatically connects clients to the closest datacenter using [latency-based DNS routing](https://ably.com/docs/platform/architecture/edge-network.md#dns-organization-and-latency-based-routing), compensating for any protocol adapter overhead with optimal network proximity. ## Supported features The following Pusher features are supported using the adapter: * Realtime subscribe * Realtime presence * Realtime publish * REST publish * REST get occupied channels * REST get presence set * REST get user count ## Enable the Pusher Adapter The Pusher protocol is not supported by default in Ably apps. This is due to security reasons associated with the use of public channels. > **Important** > > Normally, it is not possible to connect to Ably without a complete API key, or a [token](https://ably.com/docs/auth/token.md) derived from one. When the Pusher Adapter is enabled, you can connect to Pusher public channels with only the Ably API [key name](https://ably.com/docs/auth.md#format). The key name is a combination of the app ID and the key ID. To enable the Pusher Adapter in an app: 1. Log into your Ably [dashboard](https://ably.com/dashboard) and create or select the app you're using. 2. Click the **Settings** tab. 3. Check the box for **Pusher protocol support** under **Protocol Adapter Settings**. 4. Click **Save settings**. Once you save the settings, you will be offered the choice for Ably to create a set of namespaces for you. These are required to provide interoperability with Pusher client libraries: * Pusher public channels will use the `public:` namespace. * Pusher private channels will use the `private:` namespace. * Pusher presence channels will use the `presence:` namespace. Pusher clients are anonymous by default, so these namespaces must have the **Require identification** [rule](https://ably.com/docs/channels.md#rules) disabled unless every client using them [connects with a client ID](#identify). If you are receiving error code `40160` then **Require identification** is enabled and the client has not supplied a client ID. ## Configure a Pusher SDK Initialize your Pusher SDK to connect to Ably. All of Pusher's SDKs are supported, and need to be configured similar to the following: ### Javascript ``` const pusher = new Pusher('appId.keyId', { wsHost: 'main.pusher.ably.net', httpHost: 'main.pusher.ably.net', disableStats: true, forceTls: true, cluster: 'eu', authEndpoint: '...', }); ``` > **Note** > > You should only use your [Ably API key name](https://ably.com/docs/auth.md#format), not the full API key. This is the part before the colon `:`. | Option | Description | |--------|-------------| | wsHost | Set the Websocket host to point to Ably. | | httpHost | Set the HTTP requests host to point to Ably. | | disableStats | Disable the collection of stats in Pusher. | | forceTls | Force the connection to use TLS. This isn't required but strongly recommended by Ably to avoid sending private keys over a plain text connection. | | cluster | Set this to any value as it is required by Pusher. It has no impact on the Ably Pusher endpoint as Ably will use the closest data center available to the client. | | authEndpoint | The address of your [auth server](#configure-rest), if you are using one. | You can also add any other Pusher options that you normally use. ### Identify a client To give a Pusher client an Ably [client ID](https://ably.com/docs/auth/identified-clients.md), set the `wsPath` option to `/clientid/` followed by the URL-encoded client ID. Pusher SDKs prefix the WebSocket path with `wsPath`, and the adapter applies that client ID to the whole connection: #### Javascript ``` const pusher = new Pusher('appId.keyId', { wsHost: 'main.pusher.ably.net', httpHost: 'main.pusher.ably.net', wsPath: `/clientid/${encodeURIComponent(clientId)}`, disableStats: true, forceTls: true, cluster: 'eu', authEndpoint: '...', }); ``` Note that if that client later tries to join a presence channel with a different clientId, while it can still attach to the presence channel and receive messages from it, it will not be permitted to enter the presence set of the channel (and the client will receive a `pusher:error` event explaining why). > **Important** > > Ably trusts whatever client ID the client puts in `wsPath`, so any client can claim any client ID. > > This is a consequence of combining Pusher's auth model with Ably's. A Pusher client connects using only the public API key name, with the auth server only consulted when subscribing to a private or presence channel. Ably binds a client ID to a connection when the connection is established, and at that point there is no auth step that could verify it. So the client ID is self-asserted, similar to [a clientId from an Ably connection using basic auth](https://ably.com/docs/auth/identified-clients.md#basic). If you need a verified identity, use an [Ably SDK](https://ably.com/docs/sdks.md) with [token authentication](https://ably.com/docs/auth/token.md), where your auth server can bind the client ID into the token. ### Configure a Pusher server library If doing anything other than subscribing to public channels, such as authorizing a client to access a private or presence channel or publishing from your backend, you will need to configure a Pusher server-side library to point at Ably. Using Ruby as an example: #### Ruby ``` Pusher::Client.new( app_id: 'appId', key: 'appId.keyId', secret: 'keySecret', host: 'main.pusher.ably.net', use_tls: true ) ``` All other Pusher server libraries are configured in a similar way. | Option | Description | |--------|-------------| | app_id | Your Ably app ID. This is the part of the API key before the first dot. | | key | Your Ably API [key name](https://ably.com/docs/auth.md#format). This is everything before the colon `:` in your API key. | | secret | The secret portion of your API key. This is everything after the colon `:` in your API key. | | host | Set the host to point to Ably: `main.pusher.ably.net`. | | use_tls | Use TLS for the connection. This isn't required but is strongly recommended to avoid sending your API key secret over a plain text connection. | > **Note** > > The adapter uses the standard Ably service behind the scenes, so you can use Pusher and Ably SDKs side-by-side against the same channels. For example, you can publish and subscribe using Pusher libraries, but use the [Ably REST API](https://ably.com/docs/api/rest-api.md) to retrieve [message history](https://ably.com/docs/storage-history/history.md), which isn't available through the Pusher libraries. Bear in mind [channel name mapping](#mapping) when mixing libraries. ## Channel mapping Ably and Pusher have different naming restrictions for channel names, and use namespaces differently. Channel names are mapped when using the adapter as follows: * Pusher public channels will use the `public:` namespace. * Pusher private channels will use the `private:` namespace. * Pusher presence channels will use the `presence:` namespace. * Channels not in any of the above namespaces were receive a `private-ablyroot-` prefix. Colons are not permitted in Pusher channel names, but are used as namespace separators in Ably. Because of this, the adapter maps semicolons to colons in Ably channel names and colons to semicolons in Pusher channel names. This means that you will be unable to access any Ably channels with semicolons in their name. The following are some example channel name mappings: | Pusher Adapter channel | Ably channel | |------------------------|--------------| | presence-foo | presence:foo | | private-foo | private:foo | | foo | public:foo | | foo;bar | public:foo:bar | | private-ablyroot-foo | foo | | private-ablyroot-foo;bar | foo:bar | ## User and subscriber count Both user count and subscriber count currently only work on presence channels. * User count returns the size of the presence set once it has been made unique by `clientId`. * Subscriber count returns the raw size of the presence set. ## Related Topics - [Overview](https://ably.com/docs/protocols.md): Clients can use the Ably network protocol adapters. This is especially useful where an Ably SDK is not available for your language of choice, or where platform resource constraints prohibit use of an SDK. - [Server-Sent Events (SSE)](https://ably.com/docs/protocols/sse.md): Ably provides support for Server-Sent Events (SSE). This is useful for where browser clients support SSE, and the use case does not require or support the resources used by an Ably SDK. - [MQTT](https://ably.com/docs/protocols/mqtt.md): Any MQTT-enabled client can communicate with the Ably service through the Ably MQTT protocol adapter. This is especially useful where an Ably SDK is not available for your language of choice. - [PubNub Adapter](https://ably.com/docs/protocols/pubnub.md): Use the PubNub Adapter to migrate from PubNub to Ably by only changing your API key. ## Documentation Index To discover additional Ably documentation: 1. Fetch [llms.txt](https://ably.com/llms.txt) for the canonical list of available pages. 2. Identify relevant URLs from that index. 3. Fetch target pages as needed. Avoid using assumed or outdated documentation paths.