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.

Prerequisites

To make the most of this tutorial, you need:

  1. A TalkJS account
  2. A basic understanding of Svelte
  3. A Svelte app to add TalkJS to. If you don’t have an existing Svelte app yet, follow these steps to create one.

Install TalkJS

To get started, install TalkJS in your Svelte app:

1npm install talkjs --save

Create a chat component

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.svelte
2<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.svelte
2<script>
3 import Talk from 'talkjs';
4</script>
5
6<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.svelte
2<script>
3 import Chat from '../components/Chat.svelte'
4</script>
5
6<main>
7 <Chat />
8</main>

View an existing conversation

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.svelte
2<script>
3 import Talk from 'talkjs';
4 import { onMount } from 'svelte';
5
6 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 });
13
14 const conversation = session.getOrCreateConversation(
15 'sample_conversation'
16 );
17 conversation.setParticipant(me);
18
19 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 setting me to be an existing user, the sample_user_alice sample user. For a user that already exists, we can call the Talk.User constructor with just their user ID.
  • Next, use the getOrCreateConversation method to fetch the existing conversation with an ID of sample_conversation.

  • Then you create the chat UI. Call the createChatbox method for a chatbox, createInbox for an inbox, or createPopup for a popup UI. Use the select method to display the sample conversation, and then use the mount 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:

Loading chat...

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.

Create new users and conversations

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.svelte
2onMount(async () => {
3 await Talk.ready;
4 const me = new Talk.User({
5 id: 'nina',
6 name: 'Nina',
7 email: '[email protected]',
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',
18 email: '[email protected]',
19 photoUrl: 'https://talkjs.com/new-web/avatar-8.jpg',
20 welcomeMessage: 'Hey, how can I help?',
21 });
22
23 const conversation = session.getOrCreateConversation('new_conversation');
24 conversation.setParticipant(me);
25 conversation.setParticipant(other);
26
27 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 the Talk.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 of new_conversation, and then add the two new users to the conversation with the setParticipant 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:

Example chat with synced user and conversation

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.

Next steps

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.

Full code example

Here's what your working example should look like, in full:

1// Chat.svelte
2<script>
3 import Talk from 'talkjs';
4 import { onMount } from 'svelte';
5
6 onMount(async () => {
7 await Talk.ready;
8 const me = new Talk.User({
9 id: 'nina',
10 name: 'Nina',
11 email: '[email protected]',
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',
22 email: '[email protected]',
23 photoUrl: 'https://talkjs.com/new-web/avatar-8.jpg',
24 welcomeMessage: 'Hey, how can I help?',
25 });
26
27 const conversation = session.getOrCreateConversation('new_conversation');
28 conversation.setParticipant(me);
29 conversation.setParticipant(other);
30
31 const chatbox = session.createChatbox();
32 chatbox.select(conversation);
33 chatbox.mount(document.getElementById('talkjs-container'));
34 });
35</script>
36
37<div id="talkjs-container" style="height: 600px">Loading chats..</div>