Skip to main content
This guide will get you started with Ably Chat on a new React application built with Vite. You’ll learn how to create chat rooms, send and edit messages, and implement realtime features like typing indicators and presence. You’ll also cover message history, reactions, and proper connection management. Screenshot of the completed React Chat application showing a web interface with connection status, a message input field, realtime message display, and a presence indicator showing online users.

Prerequisites

Ably

  1. Sign up for an Ably account.
  2. Create a new app, and get your first API key. You can use the root API key that is provided by default, within the API Keys tab to get started.

Create a New Project

  1. Create a new React + TypeScript project using Vite. For detailed instructions, refer to the Vite documentation.
  1. Setup Tailwind CSS for styling the application. Ensure you import tailwind in your local App.CSS file and add it to your vite.config.ts file. For installation instructions, see the Tailwind CSS documentation for Vite.
  1. Install the Ably Chat SDK, this will also install the Ably Pub/Sub SDK as a dependency:
  1. Create a .env file in your project root and add your API key:

Step 1: Set Up the Ably and Chat Client Providers

The Ably Pub/Sub SDK and the Ably Chat SDK expose React hooks and context providers to make it easier to use them in your React components. The AblyProvider and ChatClientProvider should be used at the top level of your application, typically in main.tsx. In production, you should use token authentication to avoid exposing your API keys publicly, the clientId is used to identify the client. Replace the contents of your src/main.tsx file with the following code to set up the providers:

Step 2: Connect to Ably

Clients establish a connection with Ably when they instantiate an SDK. This enables them to send and receive messages in realtime across channels. This hook must be nested within a ChatClientProvider. In your project, open src/App.tsx, and add the following functions: Run your application by starting the development server: Open your browser to localhost:5173, and you will see the connection status reflected in the UI: "Currently connected!".

Step 3: Create a Room

Now that you have a connection to Ably, you can create a room. Use rooms to separate and organize clients and messages into different topics, or ‘chat rooms’. Rooms are the entry object into Chat, providing access to all of its features, such as messages, presence and reactions. The ChatRoomProvider provides access to a specific chat room to all child components in the component tree. Update your main app component to include the ChatRoomProvider:

Step 4: Send a Message

Messages are how your clients interact with one another and Ably Chat exposes a useMessages() hook to interact with the messages feature of the Chat SDK. Add a new component called ChatBox to send and receive messages: Add the ChatBox component to your main app component:

Next Steps

Continue to explore the documentation with React as the selected language: Explore the Ably CLI further, or check out the Chat JS API references for additional functionality.

Build docs developers (and LLMs) love