Svelte
This guide helps you add TalkJS to your Svelte app. It covers the core concepts of TalkJS, and introduces you to its JavaScript SDK.
In this guide we'll use SvelteKit, the official application framework recommended by the Svelte team. For an example of a basic Svelte app with TalkJS that doesn't use SvelteKit, see our TalkJS Svelte example GitHub repo.
To make the most of this tutorial, you need:
- A TalkJS account
- A basic understanding of Svelte
- A Svelte app to add TalkJS to. If you don’t have an existing Svelte app yet, follow these steps to create one.
To get started, install TalkJS in your Svelte app:
1npm install talkjs --save
The details of how and where you want to integrate TalkJS will depend on your app. For this example, create a /components/Chat.svelte
component in your project. In this component, you can import TalkJS:
1// Chat.svelte2<script>3 import Talk from 'talkjs';4</script>
For either a chatbox or an inbox pre-built chat UI, also add a container element in which to load your chat:
1// Chat.svelte2<script>3 import Talk from 'talkjs';4</script>56<div id="talkjs-container" style="height: 600px">Loading chats..</div>
If you're adding a popup UI, you can omit this container, as the popup gets overlaid on top of your page. By default the popup displays in the bottom right corner, but you can change the position of the popup button on your page.
Next, import your new chat component. For this example, import the component into the top-level +page.svelte
file:
1// +page.svelte2<script>3 import Chat from '../components/Chat.svelte'4</script>56<main>7 <Chat />8</main>
You're now ready to view a conversation. Choose the tab for your preferred chat UI, and add the following code to your Chat.svelte
component:
1// Chat.svelte2<script>3 import Talk from 'talkjs';4 import { onMount } from 'svelte';56 onMount(async () => {7 await Talk.ready;8 const me = new Talk.User('sample_user_alice');9 const session = new Talk.Session({10 appId: '<APP_ID>',11 me: me,12 });1314 const conversation = session.getOrCreateConversation(15 'sample_conversation'16 );17 conversation.setParticipant(me);1819 const chatbox = session.createChatbox();20 chatbox.select(conversation);21 chatbox.mount(document.getElementById('talkjs-container'));22 });23</script>
Let's step through what this code is doing:
- First, you make a connection to the TalkJS servers, known as a session. You'll need to replace
<APP_ID>
with your own app ID, which you can find on the Settings page of your TalkJS dashboard. For this tutorial, we recommend using the app ID for TalkJS's test mode, which has built-in sample users and conversations which we'll use in this tutorial. You'll also need to specify a current user to send messages as. In this example, you're settingme
to be an existing user, thesample_user_alice
sample user. For a user that already exists, we can call theTalk.User
constructor with just their user ID.
Next, use the
getOrCreateConversation
method to fetch the existing conversation with an ID ofsample_conversation
.Then you create the chat UI. Call the
createChatbox
method for a chatbox,createInbox
for an inbox, orcreatePopup
for a popup UI. Use theselect
method to display the sample conversation, and then use themount
method to render the UI. (The methods here are linked for the chatbox, but are also available on the inbox and popup UI.)
For example, for the chatbox UI, you should see something like the following:
Try sending Sebastian a message! You can also try switching your user ID to sample_user_sebastian
and viewing the other side of the conversation.
If you don't see the chat window, make sure that you entered your app ID, replacing <APP_ID>
in the code.
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. Usually, you would create users based on the data from your database. For this getting started guide, we've hard-coded our user data instead.
Modify your previous code by changing the highlighted lines:
1// Chat.svelte2onMount(async () => {3 await Talk.ready;4 const me = new Talk.User({5 id: 'nina',6 name: 'Nina',8 photoUrl: 'https://talkjs.com/new-web/avatar-7.jpg',9 welcomeMessage: 'Hi!',10 });11 const session = new Talk.Session({12 appId: '<APP_ID>',13 me: me,14 });15 const other = new Talk.User({16 id: 'frank',17 name: 'Frank',19 photoUrl: 'https://talkjs.com/new-web/avatar-8.jpg',20 welcomeMessage: 'Hey, how can I help?',21 });2223 const conversation = session.getOrCreateConversation('new_conversation');24 conversation.setParticipant(me);25 conversation.setParticipant(other);2627 const chatbox = session.createChatbox();28 chatbox.select(conversation);29 chatbox.mount(document.getElementById('talkjs-container'));30});
Here's what you're doing in the updated version:
- You create a new user with an ID of
nina
to be the current user. As this user is new, you pass the full user object to theTalk.User
constructor rather than just the user ID. - You create a TalkJS session and add the new current user, as in the previous section.
- You create a second new user with an ID of
frank
. - You use the
getOrCreateConversation
method to create a new conversation with an ID ofnew_conversation
, and then add the two new users to the conversation with thesetParticipant
method. - You create the UI, select the conversation, and mount the UI as in the previous section.
When you mount the UI, the new users and conversation are synced with the TalkJS servers.
You should now get something like the following:
You can also create and sync users or conversations from your backend with the REST API. If you want to only sync users or conversations with the REST API, you can disable syncing in the browser. See Browser Synchronization for more details.
That's it! You now have a working chat component in your Svelte app.
In this guide, you've added powerful user-to-user chat to your Svelte app. You also learned more about the fundamentals of TalkJS, and how it all fits together.
Most importantly, you've built a starting point to try out all the features TalkJS offers. For example, you could create a new UI theme in the Theme Editor, customize your chat with action buttons or HTML panels, or enable email notifications.
If you're planning to use reactive statements in Svelte, also check out a more advanced Svelte example.
Before you go live, make sure you enable authentication. Authentication keeps your user data secure, by ensuring that only legitimate users can connect to your chat. For details, see: Authentication.
Here's what your working example should look like, in full:
1// Chat.svelte2<script>3 import Talk from 'talkjs';4 import { onMount } from 'svelte';56 onMount(async () => {7 await Talk.ready;8 const me = new Talk.User({9 id: 'nina',10 name: 'Nina',12 photoUrl: 'https://talkjs.com/new-web/avatar-7.jpg',13 welcomeMessage: 'Hi!',14 });15 const session = new Talk.Session({16 appId: '<APP_ID>',17 me: me,18 });19 const other = new Talk.User({20 id: 'frank',21 name: 'Frank',23 photoUrl: 'https://talkjs.com/new-web/avatar-8.jpg',24 welcomeMessage: 'Hey, how can I help?',25 });2627 const conversation = session.getOrCreateConversation('new_conversation');28 conversation.setParticipant(me);29 conversation.setParticipant(other);3031 const chatbox = session.createChatbox();32 chatbox.select(conversation);33 chatbox.mount(document.getElementById('talkjs-container'));34 });35</script>3637<div id="talkjs-container" style="height: 600px">Loading chats..</div>