<Chatbox>
The <Chatbox>
React component represents a chatbox UI for a single conversation, and integrates with the user's TalkJS session.
Your app's unique TalkJS ID. You can find it on the Settings page of the TalkJS dashboard.
The ID of the current user.
The ID of the conversation to display.
Optional. The name of the theme to apply to the chatbox. A theme is a collection of React components and styles that allow you to customize the appearance and behavior of your chat. By default, a preset theme is provided, which you can override by using a custom theme. See Customize your theme.
A token to authenticate the session with. See the Authentication guide and Token reference for details and examples.
Required when authentication is enabled, otherwise optional.
Optional. A function that fetches and returns a new authentication token from your server. TalkJS calls this function whenever the current token is about to expire. This callback is designed to work with any backend setup. See Refreshable tokens for details and examples.
Optional. When set to true
, pressing the Enter key sends the message, as long as there's text in the message field. When set to false
, users can only send a message with the Send button. Defaults to true
.
Optional. Sets whether the chat header is visible. Defaults to true
.
Optional. Sets whether the message input field is visible. Defaults to true
.
Optional. Sets whether spellcheck is enabled in the message field. Defaults to false
.
Optional. Adds custom placeholder text for the message input field. The default placeholder text is "Say something...".
Optional. Function that triggers when the user deletes a message with the TalkJS UI. It receives a DeleteMessageEvent
as an argument.
Optional. Function that triggers when the user sends a message with the TalkJS UI. It receives a SendMessageEvent
as an argument.
Optional. Whatever value you pass here is available for you to use in your theme components as the themeCustom
prop.
The following gives some examples of how to use the Chatbox
component.
Create a chatbox and pass it an existing user, conversation, and the default theme:
1<Chatbox2 appId="<APP_ID>"3 userId="sample_user_alice"4 conversationId="sample_conversation"5/>
Install the @talkjs/core
package, which lets you read, subscribe to, and update your chat data:
1npm install @talkjs/core
Import it into the component where you want to use it:
1import { getTalkSession } from '@talkjs/core';
Then create the session, user, and conversation:
1const appId = '<APP_ID>';2const userId = 'sebastian';3const conversationId = 'new_conversation';4const session = getTalkSession({ appId, userId });56useEffect(() => {7 session.currentUser.createIfNotExists({ name: 'Sebastian' });8 const conversation = session.conversation(conversationId);9 conversation.createIfNotExists();10}, [session, conversationId]);
Pass the new user and conversation to your chatbox:
1<Chatbox appId={appId} userId={userId} conversationId={conversationId} />
Hide the chat header:
1<Chatbox2 appId="<APP_ID>"3 userId="sample_user_alice"4 conversationId="sample_conversation"5 chatHeaderVisible={false}6/>
Respond to a sent message event by printing details to the console:
1<Chatbox2 appId="<APP_ID>"3 userId="sample_user_alice"4 conversationId="sample_conversation"5 onSendMessage={(event) => {6 console.log('Message sent: ', event.message);7 }}8/>
The following interfaces define the event objects that are passed to chatbox properties.
Event object passed to the onDeleteMessage
callback. Contains a UserSnapshot
, MessageSnapshot
and a ConversationSnapshot
.
1export interface DeleteMessageEvent {2 currentUser: UserSnapshot;3 message: MessageSnapshot;4 conversation: ConversationSnapshot;5}
Event object passed to the onSendMessage
callback. Contains a UserSnapshot
, MessageSnapshot
and a ConversationSnapshot
.
1export interface SendMessageEvent {2 currentUser: UserSnapshot;3 message: MessageSnapshot;4 conversation: ConversationSnapshot;5}