Vue

This guide helps you add the TalkJS Inbox to your Vue application. For end-to-end tutorials on using TalkJS with Vue, see:

Get your app ID

To get started, create an account or login to your dashboard to get your app ID. You can get your app ID from the Settings page of the dashboard.

Install TalkJS

Add TalkJS to your project by installing it.

1npm install talkjs --save
2
3# or, if you use yarn
4yarn add talkjs

In this guide you'll create a Single File Component which uses the TalkJS Inbox. Create Inbox.vue wherever you keep the rest of your components in your project.

Add the TalkJS container to your template

In the template of Inbox.vue you need to add a div that acts as the container for TalkJS.

1//Inbox.vue
2<template>
3 <div ref="talkjs" style="width: 90%; margin: 30px; height: 500px">
4 <i>Loading chat...</i>
5 </div>
6</template>

Add a script tag to your template, and import TalkJS:

1//Inbox.vue
2<script>
3 import Talk from 'talkjs';
4</script>

Next, add a name and declare the props that you need for your component. In this example you only need one prop: an object representing the current user.

1//Inbox.vue
2
3export default {
4 name: 'Inbox',
5 props: {
6 currentUser: {
7 type: Object,
8 required: true
9 }
10 }
11 }

Get TalkJS up and running

You want to set up your inbox as soon as the component gets mounted, so do that inside the mounted lifecycle hook. The TalkJS SDK loads asynchronously, so you need to wait for the Talk.ready promise before you can proceed. Don't forget to add async before mounted() to make the function asynchronous.

1//Inbox.vue
2async mounted() {
3 await Talk.ready
4}

Create a TalkJS user

Create a TalkJS user. The Talk.User object is used to synchronize user data with TalkJS, so you can display it inside the chat UI. For this guide, you will create a TalkJS user assuming that the currentUser is passed as a prop to your component. Please note that in this example you add several properties to your user, but only the id is required.

1//Inbox.vue
2
3const me = new Talk.User({
4 id: this.currentUser.id,
5 name: this.currentUser.name,
6 email: this.currentUser.email,
7 photoUrl: this.currentUser.photoUrl,
8 welcomeMessage: "Hey there! How are you? :-)",
9 //The welcomeMessage is the first message any other user will see when they start a chat with this user
10 role: "booker"
11})

Create a TalkJS session

A session represents a user's active browser tab. It also authenticates your app with TalkJS. To create a session, you need your app ID, which you can get from the Settings page of your dashboard, and the current TalkJS user. Add this to the Talk.ready callback.

1const talkSession = new Talk.Session({
2 appId: 'YOUR_APP_ID',
3 me: me,
4});

Remember to replace YOUR_APP_ID with the appId found on the Settings page of your TalkJS dashboard.

Create a conversation

A conversation happens between two or more people. So far you have just created one TalkJS user. Next, create another user to have a conversation with. For this example, you'll use a hard-coded dummy user.

1const other = new Talk.User({
2 id: '654321',
3 name: 'Sebastian',
4 email: '[email protected]',
5 photoUrl: 'https://talkjs.com/images/avatar-5.jpg',
6 welcomeMessage: 'Hey, how can I help?',
7 role: 'default',
8});

Next, create a conversation. The getOrCreateConversation method attempts to get the conversation between the two users if it previously exists or create a new one otherwise. The Talk.oneOnOneId method computes a conversation ID based on participants' IDs. Use this method if you want to create a conversation between two users. You may want to create a group conversation, or generate a conversation ID associated with a transaction that happened on your platform, such as a purchase. You can find how to do that in the documentation on the conversation ID.

1const conversation = talkSession.getOrCreateConversation(
2 Talk.oneOnOneId(me, other)
3);

Next, set the participants of the conversation to the users you created.

1conversation.setParticipant(me);
2conversation.setParticipant(other);

Create and mount the inbox

The inbox is one of the UI types of TalkJS. It shows a user's conversation history and it allows them to write messages. To create the inbox, add this to your method:

1var inbox = talkSession.createInbox();
2inbox.select(conversation);

You need to call the inbox mount method after creating the inbox to make it visible on your app. Pass the element you create at the beginning of this guide to this method. Add the following code next:

1inbox.mount(this.$refs.talkjs);

That's it. You now have a Vue inbox component ready to be used in your app. Add it your App.vue, pass an object as the currentUser prop, and you’re good to go.

Here is the full code of this example:

1//Inbox.vue
2
3<template>
4 <div ref="talkjs" style="width: 1000px; margin: 30px; height: 500px">
5 <i>Loading chat...</i>
6 </div>
7</template>
8
9<script>
10 import Talk from 'talkjs';
11 export default {
12 name: 'Inbox',
13 props: {
14 currentUser: {
15 type: Object,
16 required: true
17 }
18 },
19 async mounted() {
20 await Talk.ready
21 const me = new Talk.User({
22 id: this.currentUser.id,
23 name: this.currentUser.name,
24 email: this.currentUser.email,
25 photoUrl: this.currentUser.photoUrl,
26 welcomeMessage: "Hey there! How are you? :-)",
27 role: "booker"
28 })
29
30 const talkSession = new Talk.Session({
31 appId: 'YOUR_APP_ID',
32 me: me,
33 });
34
35 const other = new Talk.User({
36 id: '654321',
37 name: 'Sebastian',
38 email: '[email protected]',
39 photoUrl: 'https://demo.talkjs.com/img/sebastian.jpg',
40 welcomeMessage: 'Hey, how can I help?',
41 role: 'default',
42 });
43
44 const conversation = talkSession.getOrCreateConversation(
45 Talk.oneOnOneId(me, other)
46 );
47
48 conversation.setParticipant(me);
49 conversation.setParticipant(other);
50
51 const inbox = talkSession.createInbox();
52 inbox.select(conversation);
53
54 inbox.mount(this.$refs.talkjs);
55
56 }
57 }
58</script>