Svelte
Preview
Components v2 is under development, but already safe to use in production.
It currently only offers limited features. Only the chatbox chat UI is currently available, not the inbox or popup. New capabilities are added on a rolling basis.
Components v2 lets you fully customize the appearance and behavior of your chat with web components. In this guide we'll show you how to add a chatbox component to your app and customize its theme.
To make the most of this guide, you will need:
- A TalkJS account
- A Svelte app to which you'd like to add TalkJS chat
To get started, install @talkjs/components
, along with the @talkjs/theme-default
theme and the react
and react-dom
dependencies:
1npm install @talkjs/components @talkjs/theme-default react react-dom
With TalkJS components and a theme installed, you can now view an existing sample conversation.
To view an existing sample conversation, import the components and theme into your app:
1<script lang="ts">2 import '@talkjs/components/web';3 import * as defaultTheme from '@talkjs/theme-default';4 import '@talkjs/components/style.css';5 import '@talkjs/theme-default/style.css';6</script>
Next, add the t-chatbox
web component and styling:
1<script lang="ts">2 import '@talkjs/components/web';3 import * as defaultTheme from '@talkjs/theme-default';4 import '@talkjs/components/style.css';5 import '@talkjs/theme-default/style.css';67 const appId = '<APP_ID>';8</script>910<main>11 <div class="wrapper">12 <t-chatbox13 app-id={appId}14 user-id="sample_user_alice"15 conversation-id="sample_conversation"16 theme={defaultTheme}17 ></t-chatbox>18 </div>19</main>2021<style>22 .wrapper {23 width: 400px;24 height: 600px;25 }26</style>
The t-chatbox
component has the following attributes:
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
.theme
: the theme you want to use. In this case we've imported the default theme.
Run your app. 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 TalkJS Core.
Install the @talkjs/core
package:
1npm install @talkjs/core
Then import the TalkSession
from the package into your component, along with Svelte's onDestroy
hook:
1<script setup>2 import '@talkjs/components/web';3 import * as defaultTheme from '@talkjs/theme-default';4 import { TalkSession } from '@talkjs/core';5 import { onDestroy } from 'svelte';6 import '@talkjs/components/style.css';7 import '@talkjs/theme-default/style.css';8</script>
Add the following code to the script
section of your component:
1const userId = 'frank';2const otherUserId = 'nina';3const conversationId = 'my_conversation';45const session = new TalkSession({ appId, userId });6session.currentUser.createIfNotExists({ name: 'Frank' });7session.user(otherUserId).createIfNotExists({ name: 'Nina' });8const conversation = session.conversation(conversationId);9conversation.createIfNotExists();10conversation.participant(otherUserId).createIfNotExists();1112onDestroy(() => {13 session.destroy();14});
Update the t-chatbox
component in your template to use the new variables:
1<t-chatbox2 app-id={appId}3 user-id={userId4 conversation-id={conversationId}5 theme={defaultTheme}6></t-chatbox>
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 the code so far, so that you can see how it all fits together:
1<script lang="ts">2 import '@talkjs/components/web';3 import * as defaultTheme from '@talkjs/theme-default';4 import { TalkSession } from '@talkjs/core';5 import { onDestroy } from 'svelte';6 import '@talkjs/components/style.css';7 import '@talkjs/theme-default/style.css';89 // Define the TalkJS configuration10 const appId = '<APP_ID>';11 const userId = 'frank';12 const otherUserId = 'nina';13 const conversationId = 'my_conversation';1415 const session = new TalkSession({ appId, userId });16 session.currentUser.createIfNotExists({ name: 'Frank' });17 session.user(otherUserId).createIfNotExists({ name: 'Nina' });18 const conversation = session.conversation(conversationId);19 conversation.createIfNotExists();20 conversation.participant(otherUserId).createIfNotExists();2122 onDestroy(() => {23 session.destroy();24 });25</script>2627<main>28 <div class="wrapper">29 <t-chatbox30 app-id={appId}31 user-id={userId}32 conversation-id={conversationId}33 theme={defaultTheme}34 ></t-chatbox>35 </div>36</main>3738<style>39 .wrapper {40 width: 400px;41 height: 600px;42 }43</style>
With a custom theme, you can change any aspect of your chat to match your own style, giving you full control over the look and feel of your chat UI.
Download the theme files from our open source theme-default
GitHub repo. Then, extract the src
folder and add it your source code. Update your imports to point at the downloaded files:
1<script lang="ts">2 import '@talkjs/components/web';3 import * as customTheme from './src'; // Use the path to the files you copied4 import { TalkSession } from '@talkjs/core';5 import { onDestroy } from 'svelte';6 import '@talkjs/components/style.css';7 import './src/index.css';8</script>
Pass the custom theme to the chatbox:
1<t-chatbox2 app-id={appId}3 user-id={userId}4 conversation-id={conversationId}5 theme={customTheme}6></t-chatbox>
You can now customize any aspect of your theme by editing the CSS and JavaScript files of the individual components in the custom-theme
folder directly.
For styling changes, edit the CSS files. For example, to give message field a different background color, you can edit the custom-theme/components/MessageField.css
file. If you change background-color: #ececec
to background-color: lightblue
, then the message field gets a light blue background color:
For behavior changes, edit the JavaScript files. For example, to add a new message action, add the following to the MessageActionMenu.js
file:
1return html`2 <div className="t-theme-message-action-menu">3 ${permissions.canReply &&4 html`<button t-action="reply" onClick=${() => setReferencedMessage(message.id)}>${t.REPLY_TO_MESSAGE}</button>`}5 ${permissions.canDelete &&6 html`<button t-action="delete" onClick=${() => chatbox.deleteMessage(message.id)}>${t.DELETE_MESSAGE}</button>`}7 <button t-action="example" onClick=${() => console.log('This is an example message action')}>8 Example message action9 </button>10 </div>11`;
You should now see Example message action in the message action menu for your messages:
Check your browser console, and you should see "This is an example message action" logged to the console.
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<t-chatbox2 app-id={appId}3 user-id={userId}4 conversation-id={conversationId}5 theme={customTheme}6 token={token}7/>
You also need to pass the token to your session:
1const session = new TalkSession({ 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.