Skip to main content
This quickstart guide will get you up and running with Ably Pub/Sub. You’ll establish a realtime connection to Ably and learn to publish and subscribe to messages.

Prerequisites

  1. Sign up for an Ably account.
  2. Create a new app, and create your first API key in the API Keys tab of the dashboard.
  3. Your API key will need the publish and subscribe capabilities.

Step 1: Install the SDK

Install the Ably SDK for your platform:
npm install ably

Step 2: Connect to Ably

Create a realtime client instance using your API key:
import Ably from 'ably';

const realtime = new Ably.Realtime('YOUR_API_KEY');

await realtime.connection.once('connected');
console.log('Connected to Ably!');

Step 3: Get a channel

Channels are used to organize messages into different topics:
const channel = realtime.channels.get('my-channel');

Step 4: Subscribe to messages

Subscribe to receive messages published to the channel:
await channel.subscribe((message) => {
  console.log('Received:', message.data);
});

Step 5: Publish a message

Publish a message to the channel:
await channel.publish('greeting', 'Hello, World!');

Complete example

Here’s a complete working example:
import Ably from 'ably';

const realtime = new Ably.Realtime('YOUR_API_KEY');

await realtime.connection.once('connected');
console.log('Connected to Ably!');

const channel = realtime.channels.get('my-channel');

await channel.subscribe((message) => {
  console.log('Received:', message.data);
});

await channel.publish('greeting', 'Hello, World!');

Next steps

Build docs developers (and LLMs) love