Getting started
This guide will help you quickly add TalkJS to your Next.js app and create a chat between two users. It uses TalkJS's React SDK, which provides highly customizable chat UI components for your application. We'll use it alongside the JavaScript Data API, which we'll use for data manipulation tasks like synchronizing users and conversations.
In the guide we'll walk you through installing TalkJS, viewing an existing conversation, creating new users and conversations, customizing the UI, and properly securing it all.
To make the most of this guide, you will need:
- A TalkJS account
- A Next.js app to which you'd like to add TalkJS chat. If you don't have a Next.js app yet, you can use
create-next-app
to get started
To get started, install @talkjs/react-components
:
1npm install @talkjs/react-components
We'll set up a Chat
component to show a sample conversation.
First, import the TalkJS components and default theme's CSS.
1import { Chatbox } from '@talkjs/react-components';2import '@talkjs/react-components/default.css';
TalkJS can only run in the browser, so we will use Next.js's Client Components to render our chat component on the client only. To do this, add 'use client';
to the top of the component file:
JavaScript
1'use client';23import { Chatbox } from '@talkjs/react-components';4import '@talkjs/react-components/default.css';
Next, add the Chatbox
to your Next.js component:
1function Chat() {2 const appId = '<APP_ID>';34 return (5 <div style={{ width: '400px', height: '800px' }}>6 <Chatbox7 appId={appId}8 userId="sample_user_alice"9 conversationId="sample_conversation"10 />11 </div>12 );13}1415export default Chat;
The Chatbox
here takes the following props:
appId
: Your TalkJS app ID. You can find your app ID on the Settings page of your TalkJS dashboard. For this guide, use the app ID for your test mode, which has built-in sample users and conversations.
userId
: An identifier for the current user to send messages as. This example uses the ID of an existing sample user,sample_user_alice
.conversationId
: An identifier of the conversation that you want to view. This example uses the ID for a built-in sample conversation,sample_conversation
.
Here, the chatbox is wrapped in a divider that applies CSS styling to set the size of the chatbox. You can modify the size to suit your needs.
You can now use the Chat
Next.js component anywhere in your app.
To view a working chat, import the Chat
Next.js component onto a page where you'd like to have your chat, for example on page.tsx
.
To ensure that the Chat
client component is only rendered on the client, you can use dynamic imports with next/dynamic
and turn off server-side pre-rendering:
1import dynamic from 'next/dynamic';23const Chat = dynamic(() => import('./components/Chat'), {4 ssr: false,5});67export default function Home() {8 return <Chat />;9}
To view the chat, run your app:
1npm run dev
You should get something like the following:
You now have a fully-featured chat window running in your app. Try sending a message!
To view the conversation as a different user, change the userId
. For example, try switching the userId
to sample_user_sebastian
to view the other side of the sample conversation.
If the chat window doesn't show up, make sure that you've entered your app ID correctly.
So far in this guide we've used a sample user and conversation. Next, we'll create new users and a conversation between them, and sync them with the TalkJS servers. To do this, we'll use the JavaScript Data API.
Install the @talkjs/core
package:
1npm install @talkjs/core
Then import getTalkSession
from the package into your component, along with React's useEffect
:
1import { Chatbox } from '@talkjs/react-components';2import '@talkjs/react-components/default.css';3import { getTalkSession } from '@talkjs/core';4import { useEffect } from 'react';
Add the following code to your component:
1function Chat() {2 const appId = '<APP_ID>';3 const userId = 'frank';4 const otherUserId = 'nina';5 const conversationId = 'new_conversation';6 const session = getTalkSession({ appId, userId });78 useEffect(() => {9 session.currentUser.createIfNotExists({ name: 'Frank' });10 session.user(otherUserId).createIfNotExists({ name: 'Nina' });1112 const conversation = session.conversation(conversationId);13 conversation.createIfNotExists();14 conversation.participant(otherUserId).createIfNotExists();15 }, [session, conversationId, otherUserId]);1617 return (18 <div style={{ width: '400px', height: '800px' }}>19 <Chatbox appId={appId} userId={userId} conversationId={conversationId} />20 </div>21 );22}
This code creates a new TalkJS session, which provides access to a continuous, up-to-date flow of your TalkJS data. It then creates two new users and a conversation between them.
Here's an example with the code so far, so that you can see how it all fits together. The following code adds TalkJS chat to a Chat
Next.js component and imports that component onto the page.tsx
page.
1'use client';23import Chat from './components/Chat';45export default function Page() {6 return (7 <div>8 <Chat />9 </div>10 );11}
The easiest way to customize TalkJS's components, is using the props and events that the components expose. See each component’s reference documentation for a list of available props.
As an example if you want to hide the built-in chat header, because it doesn't work well with the rest of your UI, you'd render the chatbox like this:
1<Chatbox2 appId={appId}3 userId={userId}4 conversationId={conversationId}5 chatHeaderVisible={false}6/>
If you need even more flexibility, TalkJS has an extensive theme system, which lets you adapt the styles, markup and behavior of the entire chat UI. See the Customization guide for more details.
Once you're using TalkJS in production you'll need to enable authentication, so that only legitimate users can connect to your chat. You'll need a backend server that can generate tokens for your users to authenticate with. See our Authentication docs for more detail on how to set this up.
To pass the token to your chatbox, add the token
prop:
1<Chatbox2 appId={appId}3 userId={userId}4 conversationId={conversationId}5 theme={customTheme}6 token={token}7/>
You also need to pass the token to your session:
1const session = getTalkSession({ appId, userId, token });
The token is different each time you connect to TalkJS, so you'll need to call your backend to generate it.
Once you are happy that your chat is loading without errors when you provide a token, go the the Settings page of your dashboard and enable the Authentication (identity verification) option in the Security settings section. When authentication is enabled, only users with a valid token can connect to your chat.
For more ways to customize your chat, see our Chatbox reference docs.