<Chatbox> | <t-chatbox>
The Chatbox component represents a chatbox UI for a single conversation.
Your app's unique TalkJS ID. Get it from the Settings page of the dashboard.
The ID of the current user.
The ID of the conversation to display.
The theme to use.
A token to authenticate the session with. See our Authentication guide and Token reference for details and examples.
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.
When set to true
, pressing the Enter key sends the message (if there is text in the message field). When set to false
, the only way to send a message is by clicking the Send button. Defaults to true
.
Sets whether the chat header is visible. Defaults to true
.
Sets whether the message input field is visible. Defaults to true
.
Sets whether spellcheck is enabled in the message field. Defaults to false
.
Adds custom placeholder text for the message input field. The default placeholder text is "Say something...".
Function that triggers when the user deletes a message with the TalkJS UI. It receives a DeleteMessageEvent
as an argument.
Function that triggers when the user sends a message with the TalkJS UI. It receives a SendMessageEvent
as an argument.
Whatever value you pass here is available for you to use in your theme components as the themeCustom
prop.
This section gives some examples that demonstrate 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 theme={defaultTheme}6/>
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 = 'frank';3const conversationId = 'new_conversation';4const session = getTalkSession({ appId, userId });56useEffect(() => {7 session.currentUser.createIfNotExists({ name: 'Frank' });8 const conversation = session.conversation(conversationId);9 conversation.createIfNotExists();10}, [session, conversationId]);
Pass the new user and conversation to your chatbox:
1<Chatbox2 appId={appId}3 userId={userId}4 conversationId={conversationId}5 theme={defaultTheme}6/>
If you are using Components without a package manager, see our guide.
Hide the chat header:
1<Chatbox2 appId="<APP_ID>"3 userId="sample_user_alice"4 conversationId="sample_conversation"5 theme={defaultTheme}6 chatHeaderVisible={false}7/>
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 theme={defaultTheme}6 onSendMessage={(event) => {7 console.log('Message sent: ', event.message);8 }}9/>
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 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 ConversationSnapshot
.
1export interface SendMessageEvent {2 currentUser: UserSnapshot;3 message: MessageSnapshot;4 conversation: ConversationSnapshot;5}