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.

npm install talkjs --save
# or, if you use yarn
yarn 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.

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

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

//Inbox.vue
<script>
import Talk from 'talkjs';
</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.

JavaScript
//Inbox.vue
export default {
name: 'Inbox',
props: {
currentUser: {
type: Object,
required: true
}
}
}

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.

JavaScript
//Inbox.vue
async mounted() {
await Talk.ready
}

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.

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

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.

JavaScript
const talkSession = new Talk.Session({
appId: 'YOUR_APP_ID',
me: me,
});

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.

JavaScript
const other = new Talk.User({
id: '654321',
name: 'Sebastian',
photoUrl: 'https://talkjs.com/images/avatar-5.jpg',
welcomeMessage: 'Hey, how can I help?',
role: 'default',
});

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.

JavaScript
const conversation = talkSession.getOrCreateConversation(
Talk.oneOnOneId(me, other)
);

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

JavaScript
conversation.setParticipant(me);
conversation.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:

JavaScript
var inbox = talkSession.createInbox();
inbox.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:

JavaScript
inbox.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:

HTML
//Inbox.vue
<template>
<div ref="talkjs" style="width: 1000px; margin: 30px; height: 500px">
<i>Loading chat...</i>
</div>
</template>
<script>
import Talk from 'talkjs';
export default {
name: 'Inbox',
props: {
currentUser: {
type: Object,
required: true
}
},
async mounted() {
await Talk.ready
const me = new Talk.User({
id: this.currentUser.id,
name: this.currentUser.name,
email: this.currentUser.email,
photoUrl: this.currentUser.photoUrl,
welcomeMessage: "Hey there! How are you? :-)",
role: "booker"
})
const talkSession = new Talk.Session({
appId: 'YOUR_APP_ID',
me: me,
});
const other = new Talk.User({
id: '654321',
name: 'Sebastian',
photoUrl: 'https://demo.talkjs.com/img/sebastian.jpg',
welcomeMessage: 'Hey, how can I help?',
role: 'default',
});
const conversation = talkSession.getOrCreateConversation(
Talk.oneOnOneId(me, other)
);
conversation.setParticipant(me);
conversation.setParticipant(other);
const inbox = talkSession.createInbox();
inbox.select(conversation);
inbox.mount(this.$refs.talkjs);
}
}
</script>