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 the following script to your app to load TalkJS:
1<!-- minified snippet to load TalkJS without delaying your page -->2<script>3(function(t,a,l,k,j,s){4s=a.createElement('script');s.async=1;s.src='https://cdn.talkjs.com/talk.js';a.head.appendChild(s)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])});l6.push([f])},catch:function(){return k&&new k()},c:l}};})(window,document,[]);7</script>
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>4</div>
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 here5 me: me,6 });7 const conversation = session.getOrCreateConversation(8 'sample_large_group_chat'9 );1011 const chatbox = session.createChatbox();12 chatbox.select(conversation, { 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 setme
to be an existing user, thesample_user_alice
sample user. For a user that already exists, we can call theTalk.User
constructor with just their user ID. - Next, we use the
getOrCreateConversation
method to fetch the existing conversation with an ID ofsample_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, settingasGuest
totrue
to view the conversation as a guest user. - Finally we use the
mount
method to render the chatbox UI inside thetalkjs-container
element.
You should see something like this:
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.
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',6 photoUrl: 'https://talkjs.com/new-web/avatar-7.jpg',7 welcomeMessage: 'Hi!',8 });9 const session = new Talk.Session({10 appId: '<APP_ID>', // replace with your app ID11 me: me,12 });1314 const conversation = session.getOrCreateConversation('new_conversation');1516 const chatbox = session.createChatbox();17 chatbox.select(conversation, { 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 theTalk.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 ofnew_conversation
, and then add the two new users to the conversation with thesetParticipant
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:
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.
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.
Here's what your working example should look like, in full:
1<!doctype html>23<html lang='en'>4 <head>5 <meta charset='utf-8' />6 <meta name='viewport' content='width=device-width, initial-scale=1' />78 <title>TalkJS tutorial</title>9 </head>1011 <!-- 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='https://cdn.talkjs.com/talk.js';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])});l16 .push([f])},catch:function(){return k&&new k()},c:l}};})(window,document,[]);17 </script>1819 <script>20 Talk.ready.then(function () {21 const me = new Talk.User({22 id: 'nina',23 name: 'Nina',25 photoUrl: 'https://talkjs.com/new-web/avatar-7.jpg',26 welcomeMessage: 'Hi!',27 });28 const session = new Talk.Session({29 appId: '<APP_ID>', // replace with your app ID30 me: me,31 });32 const conversation = session.getOrCreateConversation('new_conversation');3334 const chatbox = session.createChatbox();35 chatbox.select(conversation, { asGuest: true });36 chatbox.mount(document.getElementById('talkjs-container'));37 });38 </script>3940 <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>46</html>