Send files
Attach files to messages to share information or improve collaboration.
Chat SDK supports multiple file attachments per message (unlike the JavaScript SDK). Each file must be 5 MB or less. Files upload sequentially, so consider implementing a progress indicator for better UX.
Requires File Sharing
Enable File Sharing in the Admin Portal and configure storage region and retention. Align file retention with Message Persistence retention.
Send files
Attach files to a draft message and publish using send().
Method signature
To add files, you must add elements to the files field (MutableList<InputFile>) of a MessageDraft object.
Sample code
Attach two files of different formats to a text message.
1// create a draft message
2val messageDraft = channel.createMessageDraft(isTypingIndicatorTriggered = channel.type != ChannelType.PUBLIC)
3
4// add a listener
5val listener = { elements: List<MessageElement>, suggestedMentions: PNFuture<List<SuggestedMention>> ->
6 updateUI(elements) // updateUI is your own function for updating UI
7 suggestedMentions.async { result ->
8 result.onSuccess { updateSuggestions(it) } // updateSuggestions is your own function for displaying suggestions
9 }
10 }
11messageDraft.addChangeListener(listener)
12
13// work on editing message with messageDraft.update(text)
14
15// attach files
show all 21 linesGet all message files
files returns all files attached to a message.
Field signature
This field has the following signature:
1val files: List<File>
Sample code
List all files attached to the last message on the support channel.
1val channel: Channel
2// ...
3
4// get the last message from the channel’s history
5channel.getHistory(count = 1).async { historyResult ->
6 historyResult.onSuccess { historyResponse ->
7 val lastMessage = historyResponse.messages.firstOrNull()
8 if (lastMessage != null) {
9 // access the files property of the last message
10 val files = lastMessage.files
11
12 if (files.isNotEmpty()) {
13 println("The last message contains the following files:")
14 files.forEach { file ->
15 println("File Name: ${file.name}, File Type: ${file.type}")
show all 27 linesGet all channel files
getFiles() returns all files attached to messages on a channel.
Method signature
This method takes the following parameters:
1channel.getFiles(
2 limit: Int?,
3 next: String?
4): PNFuture<GetFilesResult>
Input
| Parameter | Description |
|---|---|
limitType: IntDefault: 100 | Number of files to return. |
nextType: StringDefault: n/a | String token to get the next batch of files. |
Output
| Parameter | Description |
|---|---|
PNFuture<GetFilesResult>Type: object | Returned object containing these fields: files, next, and total. |
→ filesType: Collection<GetFileItem> | Array containing file details. |
→ nextType: String | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
→ totalType: Int | Total number of files. |
GetFileItem contains the following properties:
| Parameter | Description |
|---|---|
nameType: String | Name of the file, like error-1.jpg. |
idType: String | Unique identifier assigned to the file by PubNub, like 736499374. |
urlType: String | File's direct downloadable URL, like https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg. |
Sample code
List all files published on the support channel.
1val channel: Channel
2
3fun fetchFiles(channel: Channel, next: String?) {
4 channel.getFiles(limit = 100, next = next).async { result ->
5 result.onSuccess { filesResult ->
6 filesResult.files.forEach { file ->
7 println("File name: ${file.name}, File URL: ${file.url}")
8 }
9
10 // Check if there is another page of results
11 if (filesResult.next != null) {
12 fetchFiles(channel, filesResult.next)
13 }
14 }.onFailure {
15 // handle error
show all 23 linesDelete files
deleteFile() removes files from PubNub storage.
Endpoint limitation
After deleting a file, you cannot identify which historical messages contained it.
Method signature
This method takes the following parameters:
1channel.deleteFile(
2 id: String,
3 name: String
4): PNFuture<PNDeleteFileResult>
Input
| Parameter | Description |
|---|---|
id *Type: StringDefault: n/a | Unique identifier assigned to the file by PubNub. |
name *Type: StringDefault: n/a | Name of the file. |
Output
| Type | Description |
|---|---|
PNFuture<PNDeleteFileResult> | Status code of the response. |
Sample code
Remove a file named error-screenshot.png from the support channel.
1val channel: Channel
2// ...
3
4// delete the file named "error-screenshot.png" from the "support" channel
5val fileId = "file-id-to-delete" // Replace this with the actual file ID
6val fileName = "error-screenshot.png"
7
8channel.deleteFile(fileId, fileName).async { result ->
9 result.onSuccess {
10 println("File '$fileName' successfully deleted from 'support' channel.")
11 }.onFailure {
12 // handle error
13 println("Error deleting file '$fileName': ${it.message}")
14 }
15}