Vue

This guide will help you add TalkJS to your Vue app and create a chat between users with the JavaScript SDK.

In the guide we'll walk you through installing TalkJS, creating a chatbox and starting a conversation. For the full code for this example, see the Vue getting started example in our GitHub repo.

Prerequisites

To make the most of this guide, you need:

Install TalkJS

To get started, install the talkjs package:

1npm install talkjs

Add TalkJS to your app

Next, add TalkJS to your app. Create a Chat.vue Single-File Component wherever you keep your components in your project.

In Chat.vue, add a script tag with Vue's setup attribute and import TalkJS:

1// Chat.vue
2<script setup>import Talk from 'talkjs';</script>

Then, for either a chatbox or an inbox pre-built chat UI, add this container element to the template of Chat.vue:

1// Chat.vue
2<template>
3 <div id="talkjs-container" style="width: 100%; height: 500px">
4 <i>Loading chat...</i>
5 </div>
6</template>

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.

You can then import this component in the place you want it in your app, for example in App.vue:

1// App.vue
2<script setup>
3 import Chat from "./components/Chat.vue";
4</script>
5
6<template>
7 <main>
8 <Chat />
9 </main>
10</template>

View an existing conversation

You can now view a conversation. Add the following code to your Chat.vue file:

1// Chat.vue
2<script setup>
3 import Talk from 'talkjs';
4 import { onMounted } from 'vue';
5
6 onMounted(() => {
7 Talk.ready.then(() => {
8 const session = new Talk.Session({
9 appId: '<APP_ID>',
10 userId: 'sample_user_alice',
11 });
12
13 const chatbox = session.createChatbox();
14 chatbox.select('sample_conversation');
15 chatbox.mount(document.getElementById('talkjs-container'));
16 });
17 });
18</script>

To set up your chat as soon as the Chat component gets mounted, this code uses Vue's onMounted() lifecycle hook.

Let's step through what the code inside Talk.ready.then is doing:

  • 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 you'll use in this tutorial. You'll also need to specify a current user to send messages as. In this example, you're setting userId to be an existing user, the sample_user_alice sample user.
  • 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.)

Run your app. For example, for the chatbox UI, you should get something like the following:

Loading chat...

Try sending Sebastian a message! You can also try switching your userId to sample_user_sebastian and viewing the other side of the conversation.

If the chat window doesn't show up, make sure that you entered your app ID, replacing <APP_ID> in the code.

Create new users and conversations

So far in this guide you've used a sample user and conversation. Next, you'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, you'll hard-code some user data instead.

Modify your previous code inside Talk.ready.then by changing the highlighted lines:

1// Chat.vue
2Talk.ready.then(() {
3 const session = new Talk.Session({
4 appId: "<APP_ID>",
5 userId: "nina",
6 });
7 session.currentUser.createIfNotExists({
8 name: "Nina",
9 email: "[email protected]",
10 photoUrl: "https://talkjs.com/new-web/avatar-7.jpg",
11 welcomeMessage: "Hi!",
12 });
13
14 const otherRef = session.user("frank");
15 otherRef.createIfNotExists({
16 name: "Frank",
17 email: "[email protected]",
18 photoUrl: "https://talkjs.com/new-web/avatar-8.jpg",
19 welcomeMessage: "Hey, how can I help?",
20 });
21
22 const conversationRef = session.conversation("new_conversation");
23 conversationRef.createIfNotExists();
24 conversationRef.participant(otherRef).createIfNotExists();
25
26 const chatbox = session.createChatbox();
27 chatbox.select(conversationRef);
28 chatbox.mount(document.getElementById('talkjs-container'));
29});

The updated version of the code does the following:

  • Connects to TalkJS as nina and sets initial user data if she does not yet exist.
  • Gets a reference to the other user, frank, and sets initial user data if he does not yet exist.
  • Gets a reference to the new_conversation conversation and sets initial conversation data if it does not yet exist. This also adds the current user, nina to the conversation.
  • Gets a reference to the frank participant in the conversation, and creates the participant if it does not already exist, adding frank to the conversation.
  • Creates the UI, selects the conversation, and mounts 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 Vue app.

Next steps

In this guide, you've added powerful user-to-user chat to your Vue 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.

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.vue
2<script setup>
3 import Talk from 'talkjs';
4 import { onMounted } from 'vue';
5
6 onMounted(() => {
7 Talk.ready.then(function () {
8 const session = new Talk.Session({
9 appId: "<APP_ID>",
10 userId: "nina",
11 });
12 session.currentUser.createIfNotExists({
13 name: "Nina",
14 email: "[email protected]",
15 photoUrl: "https://talkjs.com/new-web/avatar-7.jpg",
16 welcomeMessage: "Hi!",
17 });
18
19 const otherRef = session.user("frank");
20 otherRef.createIfNotExists({
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 conversationRef = session.conversation("new_conversation");
28 conversationRef.createIfNotExists();
29 conversationRef.participant(otherRef).createIfNotExists();
30
31 const chatbox = session.createChatbox();
32 chatbox.select(conversationRef);
33 chatbox.mount(document.getElementById("talkjs-container"));
34 });
35 });
36</script>
37
38<template>
39 <div id="talkjs-container" style="width: 100%; height: 500px">
40 <i>Loading chat...</i>
41 </div>
42</template>

For full working code for this example, see the Vue getting started GitHub repo.