Angular

This guide helps you add the TalkJS inbox chat UI to your Angular app.

Get your app ID

To get started, create a TalkJS account or login to your dashboard. 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

Create the Talk service

First, create a service that contains TalkJS methods. This make it easier to reuse these methods in any part of your application.

ng generate service talk

Next, import TalkJS into the service you just created. Add the following code in the talk.service.ts file.

TS
import Talk from 'talkjs';

You also want to declare a variable for the current user in the service. This way all methods can have access to it.

TS
export class TalkService {
private currentUser: Talk.User;
}

In the service, add methods that will do the following:

  1. Create a TalkJS user
  2. Create a TalkJS session
  3. Get or create a TalkJS conversation
  4. Create the Inbox

Start with the first.

Create a TalkJS user

The first step is to 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. Add a method to the service that maps a user object from your app to a Talk.User:

TS
async createUser(applicationUser: any) {
await Talk.ready;
return new Talk.User({
id: applicationUser.id,
name: applicationUser.username,
photoUrl: applicationUser.photoUrl,
role: applicationUser.role
});
}

Be sure to change the applicationUser type to your application's user type and edit the code accordingly.

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 TalkJS dashboard, and the current TalkJS user. Here you'll use a hard-coded value for the current user, but remember to change it to use the current user logged in to your application.

TS
async createCurrentSession() {
await Talk.ready;
const user = {
id: 1,
username: 'Alice',
photoUrl: 'https://talkjs.com/images/avatar-1.jpg',
welcomeMessage: 'Hey there! How are you? :-)',
role: 'default'
};
this.currentUser = await this.createUser(user);
const session = new Talk.Session({
appId: 'YOUR_APP_ID',
me: this.currentUser
});
return session;
}

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

Create a conversation

Next, create a conversation. Use the getOrCreateConversation method to get the conversation between the two users if the conversation already exists, or otherwise to create a new conversation.

TS
private async getOrCreateConversation(session: Talk.Session, otherApplicationUser: any) {
const otherUser = await this.createUser(otherApplicationUser);
const conversation = session.getOrCreateConversation(Talk.oneOnOneId(this.currentUser, otherUser));
conversation.setParticipant(this.currentUser);
conversation.setParticipant(otherUser);
return conversation;
}

The Talk.oneOnOneId method computes a conversation ID based on participants' IDs. Use this method if you want to create a conversation between only two users. You may want to create a group conversation or generate a conversation ID associated with a transaction that happened on your platform, for example a purchase. You can find how to do that in the documentation of the conversation ID.

Create 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 the following method. The method contains another hard-coded user to serve as the other application user.

TS
async createInbox(session: Talk.Session) {
const otherApplicationUser = {
id: 5,
username: 'Lo',
photoUrl: 'https://talkjs.com/images/avatar-5.jpg',
welcomeMessage: 'Hey, how can I help?',
role: 'default'
};
const conversation = await this.getOrCreateConversation(session, otherApplicationUser);
const inbox = session.createInbox();
inbox.select(conversation);
return inbox;
}

Now you can import the service you just created in any component and use the methods.

Using the Talk service in a component

For this section, you'll use the talk service in the app component. Remember that you can repeat these steps in any component.

Add an element to mount the Inbox

In the HTML file of the component app.component.html, add the element you want to mount the inbox on.

HTML
<div #talkjsContainer style="height: 500px">Loading...</div>

The #talkjsContainer lets you refer to the element in your component code.

Import the Talk service

Next, import the Talk Service you created into the component file app.component.ts and initialize it in the constructor method. Also import OnInit, ViewChild and ElementRef which you'll use in the component.

TS
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { TalkService } from 'src/app/talk.service';
import Talk from 'talkjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
private inbox: Talk.Inbox;
private session: Talk.Session;
constructor(private talkService: TalkService) {}
}

Mount the inbox

First, add a view query to reference the element you added to the HTML file. In the app.component.ts file, add the following:

TS
@ViewChild('talkjsContainer') talkjsContainer!: ElementRef;

Next, add a method to use the service methods you created earlier to create the inbox and mount it on the HTML element you added. Then call this method in the ngOnInit lifecycle hook.

TS
ngOnInit() {
this.createInbox();
}
private async createInbox() {
const session = await this.talkService.createCurrentSession();
this.inbox = await this.talkService.createInbox(session);
this.inbox.mount(this.talkjsContainer.nativeElement);
}

That's it. The inbox should now be running on your app. Here is the final result and the full code for this:

Select the result tab to see the Inbox. Go ahead, have a chat with Lo. He won't respond yet, but hey, it's a start! If you get an error, verify that appId is correct.

You can find several examples for different use cases in Angular on the TalkJS examples repository on GitHub.

Next, you can learn how to add notifications.