Group chat

This guide will help you quickly add TalkJS to your app and create a group chat between three or more users with the JavaScript SDK.

In the guide we'll walk you through installing TalkJS, creating a chatbox and starting a conversation.


To make the most of this guide, you will need a TalkJS account.

You'll probably also have an existing app that you want to add TalkJS to.

Add TalkJS to your app

Add the following script to your app to load TalkJS:

1<!-- minified snippet to load TalkJS without delaying your page -->
5;k=t.Promise;t.Talk={v:3,ready:{then:function(f){if(k)return new k(function(r,e){l.push([f,r,e])});l
6.push([f])},catch:function(){return k&&new k()},c:l}};})(window,document,[]);

Then include this container element in the place in which you want to add your chat:

1<!-- container element in which TalkJS will display a chat UI -->
2<div id='talkjs-container' style='width: 90%; margin: 30px; height: 500px'>
3 <i>Loading chat...</i>

View an existing conversation

Next, we'll view an existing conversation. Add the following code to your app:

1Talk.ready.then(function () {
2 const me = new Talk.User('sample_user_alice');
3 const talkSession = new Talk.Session({
4 appId: '<APP_ID>',
5 me: me,
6 });
8 const conversation = talkSession.getOrCreateConversation(
9 'sample_group_chat'
10 );
11 conversation.setParticipant(me);
13 const chatbox = talkSession.createChatbox();
15 chatbox.mount(document.getElementById('talkjs-container'));

Let's step through what this code is doing:

  • First, we 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 we'll use in this tutorial. You'll also need to specify a current user to send messages as. In this example, we've set me to be an existing user, the sample_user_alice sample user. For a user that already exists, we can call the Talk.User constructor with just their user ID.
  • Next, we use the getOrCreateConversation method to fetch the existing conversation with an ID of sample_group_chat.

  • Then we create the chat UI. In this tutorial, we'll create a chatbox UI with the createChatbox method. We use the chatbox's select method to display the sample conversation, and then use the mount method to render the chatbox UI inside the talkjs-container element.

You should see something like this:

Loading chat...

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

If you don't see the chat window, make sure that you entered your App ID, replacing <APP_ID> in the code.

Create new users and conversations

So far in this guide we've used a sample user and conversation. Next, we'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, we've hard-coded our user data instead.

Modify your previous code by changing the highlighted lines:

1Talk.ready.then(function () {
2 const me = new Talk.User({
3 id: 'nina',
4 name: 'Nina',
5 email: '[email protected]',
6 photoUrl: '',
7 welcomeMessage: 'Hi!',
8 });
9 const session = new Talk.Session({
10 appId: '<APP_ID>',
11 me: me,
12 });
13 const frank = new Talk.User({
14 id: 'frank',
15 name: 'Frank',
16 email: '[email protected]',
17 photoUrl: '',
18 welcomeMessage: 'Hey, how can I help?',
19 });
20 const juliana = new Talk.User({
21 id: 'juliana',
22 name: 'Juliana',
23 email: '[email protected]',
24 photoUrl: '',
25 welcomeMessage: 'Hey, how can I help?',
26 });
28 const conversation = session.getOrCreateConversation('new_conversation');
29 conversation.setParticipant(me);
30 conversation.setParticipant(frank);
31 conversation.setParticipant(juliana);
33 const chatbox = session.createChatbox();
35 chatbox.mount(document.getElementById('talkjs-container'));
36 });

Here's what we're doing in the updated version:

  • We create a new user with an ID of nina to be our current user. As this user is new, we pass the full user object to the Talk.User constructor rather than just the user ID.
  • We create a TalkJS session and add our new current user as in the previous section.
  • We create two more new users with IDs of frank and juliana.
  • We use the getOrCreateConversation method to create a new conversation with an ID of new_conversation, and then add the two new users to the conversation with the setParticipant method.
  • We create a chatbox, select the conversation and mount the chatbox as in the previous section.

When you mount the chatbox, the new users and conversation are synced with the TalkJS servers.

You should now see something like this:

Example chat with synced user and conversation

If you prefer, you can instead create and sync users or conversations from your backend with our 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.

Next steps

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

For more ideas, try browsing the many examples we offer for different use cases.

Full code example

Here's what your working example should look like, in full:

1<!doctype html>
3<html lang='en'>
4 <head>
5 <meta charset='utf-8' />
6 <meta name='viewport' content='width=device-width, initial-scale=1' />
8 <title>TalkJS tutorial</title>
9 </head>
11 <!-- minified snippet to load TalkJS without delaying your page -->
12 <script>
13 (function(t,a,l,k,j,s){
14 s=a.createElement('script');s.async=1;s.src='';a.head.appendChild(s)
15 ;k=t.Promise;t.Talk={v:3,ready:{then:function(f){if(k)return new k(function(r,e){l.push([f,r,e])});l
16 .push([f])},catch:function(){return k&&new k()},c:l}};})(window,document,[]);
17 </script>
19 <script>
20 Talk.ready.then(function () {
21 const me = new Talk.User({
22 id: 'nina',
23 name: 'Nina',
24 email: '[email protected]',
25 photoUrl: '',
26 welcomeMessage: 'Hi!',
27 });
28 const session = new Talk.Session({
29 appId: '<APP_ID>',
30 me: me,
31 });
32 const frank = new Talk.User({
33 id: 'frank',
34 name: 'Frank',
35 email: '[email protected]',
36 photoUrl: '',
37 welcomeMessage: 'Hey, how can I help?',
38 });
39 const juliana = new Talk.User({
40 id: 'juliana',
41 name: 'Juliana',
42 email: '[email protected]',
43 photoUrl: '',
44 welcomeMessage: 'Hey, how can I help?',
45 });
47 const conversation = session.getOrCreateConversation('new_conversation');
48 conversation.setParticipant(me);
49 conversation.setParticipant(frank);
50 conversation.setParticipant(juliana);
52 const chatbox = session.createChatbox();
54 chatbox.mount(document.getElementById('talkjs-container'));
55 });
56 </script>
58 <body>
59 <!-- container element in which TalkJS will display a chat UI -->
60 <div id='talkjs-container' style='width: 90%; margin: 30px; height: 500px'>
61 <i>Loading chat...</i>
62 </div>
63 </body>