# Chat

Canonical page: https://docs.ovok.com/java-sdk/chat

`ovok.chat()` is person-to-person chat between patients and their care team. `threads()` opens and lists the signed-in person's threads, `messages()` reads and sends messages in a thread, and `attachments()` uploads files to send with them.

## Open a thread and send a message [#open-a-thread-and-send-a-message]

```java
JsonNode thread = ovok.chat().threads().open(
        Map.of("patient", "Patient/123", "participants", List.of("Practitioner/456")),
        RequestOptions.defaults());
String threadId = thread.get("id").asText();

ovok.chat().messages().send(threadId, Map.of("text", "How are you feeling today?"), RequestOptions.defaults());
ovok.chat().read(threadId, null, RequestOptions.defaults());
```

- `open` answers the thread these participants already have about the patient, if there is one. Every participant must be allowed to read the patient.
- `send` adds a random `clientId` when you pass none. The server remembers it, so a retried send is stored once.
- `read` marks the thread read now, or at the `readAt` you pass.

## Send a file [#send-a-file]

```java
JsonNode attachment = ovok.chat().attachments().upload(
        Files.readAllBytes(scan), "scan.pdf", "application/pdf", RequestOptions.defaults());

ovok.chat().messages().send(threadId,
        Map.of("text", "My latest results", "attachments", List.of(attachment.get("id").asText())),
        RequestOptions.defaults());
```

- `upload` takes a PDF, PNG, JPEG or WebP of up to 10 MB and keeps it private. It stores the file where the server says, and throws `OvokServiceException` when the store refuses it.
- An upload can go out in as many threads as you like.
- `attachments().link(attachmentId, options)` and `messages().attachmentLink(threadId, messageId, attachmentId, options)` answer a download `url` that works for the next hour.

## Read threads and messages [#read-threads-and-messages]

`threads().list` and `messages().list` return a `Page<JsonNode>` that follows the cursor. Iterating the page loads the next ones. See [paging](/java-sdk/fhir-and-observations#paging).

| Call | What it does |
| --- | --- |
| `threads().list(params, options)` | Lists threads, newest message first. Filter by `patient` or `updatedSince`, and set `limit`. |
| `threads().retrieve(threadId, options)` | Reads one thread. |
| `messages().list(threadId, params, options)` | Lists messages, newest first. Later pages go further back. |

Every method is in the [`ChatResource` reference](/java-sdk/reference/chat-resource).

## See also [#see-also]

- [`ChatResource` reference](/java-sdk/reference/chat-resource): every method.
- [Push](/java-sdk/push): register devices for notifications.
