Large group chat

This guide will help you quickly add TalkJS to your app and create a large group chat with guest users with the JavaScript SDK. Guest access is a lightweight access mode that can scale to larger numbers of users compared to normal participant access. Guests can read and send messages but cannot be mentioned or receive notifications, and they will not appear in the participant list of conversations. See Participant access and guest access for more details.

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 as a guest

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

1Talk.ready.then(function () {
2 const me = new Talk.User('sample_user_alice');
3 const session = new Talk.Session({
4 appId: '<APP_ID>', // add your TalkJS app ID here
5 me: me,
6 });
7 const conversation = session.getOrCreateConversation(
8 'sample_large_group_chat'
9 );
11 const chatbox = session.createChatbox();
12, { asGuest: true });
13 chatbox.mount(document.getElementById('talkjs-container'));
14 });

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_large_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, setting asGuest to true to view the conversation as a guest user.
  • Finally we use the mount method to render the chatbox UI inside the talkjs-container element.

You should see something like this:

Loading chat...

Try sending a message!

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

Create a new guest user 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>', // replace with your app ID
11 me: me,
12 });
14 const conversation = session.getOrCreateConversation('new_conversation');
16 const chatbox = session.createChatbox();
17, { asGuest: true });
18 chatbox.mount(document.getElementById('talkjs-container'));
19 });

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 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 with asGuest: true 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>', // replace with your app ID
30 me: me,
31 });
32 const conversation = session.getOrCreateConversation('new_conversation');
34 const chatbox = session.createChatbox();
35, { asGuest: true });
36 chatbox.mount(document.getElementById('talkjs-container'));
37 });
38 </script>
40 <body>
41 <!-- container element in which TalkJS will display a chat UI -->
42 <div id='talkjs-container' style='width: 90%; margin: 30px; height: 500px'>
43 <i>Loading chat...</i>
44 </div>
45 </body>