Switch between conversations

Once you've added a chatbox or popup to your site, and you have some conversations going, you might want your users to be able to switch between them.

In a popup

Let's say you have a few buttons on a page, each corresponding to a conversation topic:

HTML
1<button onclick="buttonClicked(this.innerHTML)">Conversation A</button>
2<button onclick="buttonClicked(this.innerHTML)">Conversation B</button>
3<button onclick="buttonClicked(this.innerHTML)">Conversation C</button>

Then we can define buttonClicked(conversationId) as follows

JavaScript
1let popup;
2
3function buttonClicked(conversationId) {
4 // assume `session` has been set elsewhere
5 const conversation = session.getOrCreateConversation(conversationId);
6
7 if (popup) {
8 popup.show(); //in case popup is hidden
9 popup.select(conversation); //select the conversation clicked on
10 } else {
11 //if there is no existing popup, create one
12 popup = session.createPopup();
13 popup.select(conversation);
14 popup.mount({ show: true });
15 }
16}

When the user clicks on a button, the text within the button is used as a conversation id. The popup then makes this the active conversation. select takes either a Conversation object or conversation id as its parameter - see select.

In a chatbox

Assuming someChatbox exists, we can simply do:

JavaScript
1function buttonClicked(conversationId) {
2 const conversation =
3 window.talkSession.getOrCreateConversation(conversationId);
4 someChatbox.select(conversation);
5}