---
url: https://talkjs.com/docs/Guides/Resource_Discussions/
title: "Chats attached to resources | Guides | TalkJS Docs"
---

For AI agents: Find a documentation index at https://talkjs.com/llms.txt (full content at https://talkjs.com/llms-full.txt). Get a markdown version of any page by appending .md to its URL path.

# Chats attached to resources

Keep each conversation with the video, task, application or document it belongs to.

Many apps let users discuss specific resources, such as a video, a task, a job application, a line in a document or a cell in a spreadsheet. This guide shows how to attach a chat to a resource and reopen the same conversation when someone returns to it.

The example below is a project management app. Selecting a task opens its chat beside the task list.

![A project management app with its navigation, task list and the selected task's chat open.](https://talkjs.com/docs/resource-discussions.png)

## Choose a conversation ID for the resource

Conversation IDs must be globally unique and unguessable to someone without access to the resource. If your resources already have random UUIDs, build the conversation ID from those IDs. For example:

-   Task: `task_<task UUID>`
-   Document line: `line_<document UUID>_<line ID>`
-   Spreadsheet cell: `cell_<spreadsheet UUID>_<row ID>_<column ID>`
-   Version comparison: `comparison_<document UUID>_<left version ID>_<right version ID>`

Use stable line and row IDs so their discussions stay attached when the content moves. Including both document version IDs keeps a comparison's discussion with those versions when a new one is uploaded.

If your app uses sequential resource IDs, generate a random UUID once and save it on the resource as its conversation ID. Reuse the saved ID whenever opening that resource's chat.

## Open the chat when a resource is selected

Add `openDiscussion` to the screen that displays the chat.

**React**

```jsx
import { useState } from 'react';
import { getTalkSession } from '@talkjs/core';

// In your existing component:
const session = getTalkSession({ appId, userId });
const [conversationId, setConversationId] = useState(null);

async function openDiscussion(id, title) {
  const conversation = session.conversation(id);
  await conversation.createIfNotExists({ subject: title });
  await conversation.participant(otherUserId).createIfNotExists();
  setConversationId(id);
}
```

**JavaScript**

```javascript
import { getTalkSession } from '@talkjs/core';

const session = getTalkSession({ appId, userId });
const chatbox = document.querySelector('t-chatbox');

async function openDiscussion(id, title) {
  const conversation = session.conversation(id);
  await conversation.createIfNotExists({ subject: title });
  await conversation.participant(otherUserId).createIfNotExists();
  chatbox.conversationId = id;
}
```

**Angular**

```typescript
import { getTalkSession } from '@talkjs/core';

// In your existing component:
conversationId: string | null = null;

async openDiscussion(id: string, title: string) {
  const session = getTalkSession({ appId: this.appId, userId: this.userId });
  const conversation = session.conversation(id);
  await conversation.createIfNotExists({ subject: title });
  await conversation.participant(this.otherUserId).createIfNotExists();
  this.conversationId = id;
}
```

**Vue**

```javascript
import { ref } from 'vue';
import { getTalkSession } from '@talkjs/core';

const session = getTalkSession({ appId, userId });
const conversationId = ref(null);

async function openDiscussion(id, title) {
  const conversation = session.conversation(id);
  await conversation.createIfNotExists({ subject: title });
  await conversation.participant(otherUserId).createIfNotExists();
  conversationId.value = id;
}
```

**Svelte**

```javascript
import { getTalkSession } from '@talkjs/core';

const session = getTalkSession({ appId, userId });
let conversationId = null;

async function openDiscussion(id, title) {
  const conversation = session.conversation(id);
  await conversation.createIfNotExists({ subject: title });
  await conversation.participant(otherUserId).createIfNotExists();
  conversationId = id;
}
```

**React Native**

```jsx
import { useState } from 'react';
import * as TalkRn from '@talkjs/react-native';

// In your existing screen:
const [discussion, setDiscussion] = useState(null);

function openDiscussion(id, title) {
  const builder = TalkRn.getConversationBuilder(id);
  builder.setAttributes({ subject: title });
  builder.setParticipant(me);
  builder.setParticipant(other);
  setDiscussion({ id, builder });
}
```

**Flutter**

```dart
// In your chat screen's State:
Conversation? conversation;

void openDiscussion(String id, String title) {
  final next = session.getConversation(
    id: id,
    subject: title,
    participants: {Participant(me), Participant(other)},
  );
  setState(() {
    conversation = next;
  });
}
```

When the user selects a task, call `openDiscussion` with its conversation ID and title.

**React**

```javascript
openDiscussion(`task_${task.id}`, task.title);
```

**JavaScript**

```javascript
openDiscussion(`task_${task.id}`, task.title);
```

**Angular**

```typescript
this.openDiscussion(`task_${task.id}`, task.title);
```

**Vue**

```javascript
openDiscussion(`task_${task.id}`, task.title);
```

**Svelte**

```javascript
openDiscussion(`task_${task.id}`, task.title);
```

**React Native**

```javascript
openDiscussion(`task_${task.id}`, task.title);
```

**Flutter**

```dart
openDiscussion('task_${task.id}', task.title);
```

Display the selected conversation in your chatbox.

**React**

```jsx
<Chatbox
  appId={appId}
  userId={userId}
  conversationId={conversationId}
/>
```

**JavaScript**

```html
<!-- openDiscussion sets conversationId on this chatbox. -->
<t-chatbox app-id="YOUR_APP_ID" user-id="YOUR_USER_ID"></t-chatbox>
```

**Angular**

```html
<t-chatbox
  [app-id]="appId"
  [user-id]="userId"
  [conversation-id]="conversationId">
</t-chatbox>
```

**Vue**

```html
<t-chatbox
  :app-id="appId"
  :user-id="userId"
  :conversation-id="conversationId">
</t-chatbox>
```

**Svelte**

```html
<t-chatbox
  app-id={appId}
  user-id={userId}
  conversation-id={conversationId}>
</t-chatbox>
```

**React Native**

```jsx
{discussion && (
  <TalkRn.Chatbox
    key={discussion.id}
    conversationBuilder={discussion.builder}
    style={{ flex: 1 }}
  />
)}
```

**Flutter**

```dart
conversation == null
    ? const SizedBox.shrink()
    : ChatBox(session: session, conversation: conversation!)
```
