Svelte

This guide helps you add TalkJS in your Svelte app. It covers the core concepts of TalkJS, and introduces you to its JavaScript SDK.

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

Install TalkJS

To get started, install TalkJS in your Svelte app:

npm install talkjs --save

Then, import TalkJS into the component you want to use for chat:

JavaScript
<script>
import Talk from 'talkjs';
</script>

TalkJS is loaded asynchronously to improve the performance of your site. Before you can use it, make sure that TalkJS is fully loaded and ready to use. To do that, wait for the Talk.ready promise to resolve before making any calls to the TalkJS SDK.

import { onMount } from 'svelte';
onMount(async () => {
await Talk.ready;
// Safe to use the SDK here
});

Talk.ready is a standard JavaScript promise, so you can call .then or await it at any point.

Create users

In TalkJS, a user is a person that uses your app. Typically, you would have one TalkJS user for each user in your own database. Each connection to TalkJS has to specify a current user to send messages as.

Usually, you would create users based on the data from your database. For this example, you'll hard-code one instead:

JavaScript
// After `await Talk.ready`
const currentUser = new Talk.User({
id: '1',
name: 'Henry Mill',
photoUrl: 'henry.jpeg',
welcomeMessage: 'Hello!',
role: 'default',
});

Remember, anything using the Talk API should only run after we await Talk.ready.

Next, create another user:

JavaScript
// After `await Talk.ready`
const otherUser = new Talk.User({
id: '2',
name: 'Jessica Wells',
photoUrl: 'jessica.jpeg',
welcomeMessage: 'Hello!',
role: 'default',
});

Connect to TalkJS

To synchronize data between a browser tab and TalkJS, you need to create a new connection, known as a session. The session stays open until the user navigates away from the page, and it automatically synchronizes data in both directions with the TalkJS servers.

To create a new session, you need your app ID and a user to send messages as, namely the current user. You can find YOUR_APP_ID on the Settings page of your TalkJS dashboard.

onMount(async () => {
await Talk.ready;
// Create users here
const session = new Talk.Session({
appId: 'YOUR_APP_ID',
me: currentUser,
});
});

Start a conversation

So far, you have loaded TalkJS, created some users, and opened a connection to the server. Now you can let the users talk to each other.

A conversation is a place for two or more users to chat. That could mean a simple direct message between users, or it might be something fancier. TalkJS works with all kinds of conversations, from group chats to product-specific conversations.

For this example, create a conversation between 'Henry' and 'Jessica':

JavaScript
// After `Talk.ready` and creating users
const conversationID = Talk.oneOnOneId(currentUser, otherUser);
const conversation = session.getOrCreateConversation(conversationId);
conversation.setParticipant(currentUser);
conversation.setParticipant(otherUser);

If these users have talked before, the auto-generated conversationId is the same, meaning TalkJS loads the pre-existing conversation and all previous messages. Otherwise, it creates a new conversation.

Add the UI

At this point, everything is set up to allow your users to chat. All you need is a user interface.

TalkJS offers multiple UIs for your project out of the box. This guide uses the chatbox, but the other UIs work the same way.

Create a new chatbox and select the conversation you made earlier. This tells the UI which conversation it should display.

JavaScript
// After creating the conversation
const chatbox = session.createChatbox();
chatbox.select(conversation);

To display your chatbox on the page, you need to mount it on an HTML element. Create a div element and store it in a variable using bind:this:

<script>
/* ... */
let chatboxEl;
/* ... */
</script>
<div bind:this={chatboxEl} />

Now you can pass that reference to chatbox.mount, which displays the chatbox UI inside your div:

// After chatbox.select
chatbox.mount(this.chatboxEl);

Check your browser, and you should see a chat window running in your app.

Try sending Jessica a message, and play around with TalkJS.

If you don't see the chat window, make sure that you entered your app ID correctly, replacing YOUR_APP_ID in the code.

Next steps

In this short guide, you've taken your Svelte app to the next level with powerful user-to-user chat. 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.

You might notice that most of your code sits inside onMount. That prevents you from using reactive statements. To make it easier to use reactive statements, you can wrap the Talk.ready promise in a component. For more information, check out a more advanced Svelte example.

If you would like to try out more features, you could create a group chat by adding more users, create a new UI theme, or even enable email notifications.