Building a chat application is a rite of passage for many developers. But building a good one? That's a different story.
A production-ready chat app needs more than just sending messages. You need real-time typing indicators, a "who's online" presence list, and persistent message history so users don't lose their conversations on a page reload. This list of features can quickly turn a weekend project into a multi-week backend engineering effort.
What if you could skip all of that?
Today, we'll show you how to build a complete, real-time chat application with all those features in about three minutes, using a single React component from our newly updated @vaultrice/react-components
library.
The Star of the Show: <ChatRoom />
The <ChatRoom />
component is an all-in-one solution that combines a feature-rich <Chat />
component with a live <Presence />
indicator. It's designed to give you a complete collaborative experience out of the box.
The 3-Minute Challenge
Let's get started.
Step 1: Setup (1 minute)
First, grab your free Vaultrice credentials and install the library.
- Get Credentials: Sign up for a free Vaultrice account, create a project, and get your API keys.
- Install the Library:
npm install @vaultrice/react-components
Step 2: Add the Component (1 minute)
Now, import the <ChatRoom />
component and add it to your app. All you need to provide is a unique id
for the room, the user
's information, and your credentials
.
import { ChatRoom } from '@vaultrice/react-components'
export default function TeamChat() {
const currentUser = {
id: "user-123",
name: "John Doe",
avatarUrl: "https://example.com/avatar.jpg"
}
return (
<ChatRoom
id="team-chat-room"
user={currentUser}
title="Engineering Team"
credentials={{
projectId: "YOUR_PROJECT_ID",
apiKey: "YOUR_API_KEY",
apiSecret: "YOUR_API_SECRET"
}}
/>
)
}
At this point, you already have a fully functional chat room with real-time messages, typing indicators, and a live presence list.
A Quick Note on the currentUser Object
The currentUser
object in our example represents your currently logged-in user. You would typically get this information from your own authentication system, for example, by decoding a JWT or fetching user data from your database after they log in. Vaultrice uses this to identify the user in the chat and presence list.
Step 3: Add Persistent Message History & Auto-Cleanup (less than 1 minute)
Want to save the chat history so conversations are never lost? Just add persistMessages={true}
.
You can also control how long messages are stored by setting a ttl
(Time-to-Live). This is perfect for automatically cleaning up old conversations (e.g., after 30 days).
<ChatRoom
id="team-chat-room"
user={currentUser}
title="Engineering Team"
credentials={{ /* ... */ }}
// Add these for message history and auto-cleanup
persistMessages={true}
messageHistoryLimit={100} // Store the last 100 messages
instanceOptions={{
ttl: 30 * 24 * 60 * 60 * 1000 // 30 days in milliseconds
}}
/>
And you're done. In about three minutes, you have a complete, persistent, real-time chat application.
Making It Production-Ready: Security
While this demo gets you running in minutes, Vaultrice is built with production security in mind. Before deploying a real chat application, you could enable some security features:
-
Secure Authentication with Access Tokens: For the best security, you can avoid exposing your
apiSecret
in your frontend code. The recommended approach is to have your backend generate short-lived access tokens for your clients.On your backend (e.g., in a Next.js API route):
// This endpoint generates a temporary token for the logged-in user. import { retrieveAccessToken } from '@vaultrice/sdk'; export async function GET(request) { const accessToken = await retrieveAccessToken( process.env.VAULTRICE_PROJECT_ID, process.env.VAULTRICE_API_KEY, process.env.VAULTRICE_API_SECRET ); return Response.json({ accessToken }); }
In your React component:
<ChatRoom id="team-chat-room" user={currentUser} credentials={{ projectId: "YOUR_PROJECT_ID", getAccessToken: async () => { const { accessToken } = await fetch('/api/vaultrice-token').then(r => r.json()); return accessToken; } }} />
Origin Restriction (SF1): In your Vaultrice dashboard, you can lock your API keys to your specific domain(s). This is your first line of defense, ensuring that even if a key is exposed, it can only be used from your website.
Object ID Signature Verification (SF2): For private chat rooms, you can use your backend to generate a secure signature for the room's
id
. The<ChatRoom />
component then sends this signature to Vaultrice, which verifies that the user is authorized to join that specific room. This prevents users from accessing chat rooms they shouldn't.
You can learn more about these and other features in our in-depth Security Guide.
Try It Yourself: Interactive Demo
Don't just take our word for it. This Storybook demo is a live, running instance of the <ChatRoom />
component. You can change the current user, toggle features, and even enable persistence to see how it works across page reloads.
Watch a short demo video that shows this exact example chat in action:
Deconstructing the Magic
How is this possible? The <ChatRoom />
component does the heavy lifting for you by:
- Using the
@vaultrice/react
hooks to manage all real-time and state logic. - Leveraging the Presence API to show who's online.
- Using atomic array operations (
push
andsplice
) to safely store message history without race conditions. - Running on the globally distributed, zero-ops
@vaultrice/sdk
.
Conclusion
Building powerful, real-time collaborative features should be fast and fun, not a backend nightmare. With the new components in @vaultrice/react-components
, we're providing not just the backend infrastructure, but the polished, production-ready frontend solutions to go with it.
Go ahead and give the <ChatRoom />
a try. We can't wait to see what you build.
Top comments (0)