On this page

Send files

Attach files to messages to share information or improve collaboration.

Chat SDK supports multiple file attachments per message (unlike the Swift 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 ([InputFile]) of a MessageDraft object.

Sample code

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Attach two files of different formats to a text message.

1

Get all message files

files returns all files attached to a message.

Method signature

This method has the following signature:

1message.files

Sample code

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

List all files attached to a message on the support channel.

1

Get all channel files

getFiles() returns all files attached to messages on a given channel.

Method signature

This method takes the following parameters:

1channel.getFiles(
2 limit: Int = 100,
3 next: String? = nil
4 ) async throws -> (files: [GetFileItem], page: PubNubHashedPage?)

Input

* required
ParameterDescription
limit
Type: Int
Default:
100
Number of files to return.
next
Type: String
Default:
n/a
String token to get the next batch of files.

Output

ParameterDescription
(files: [GetFileItem], page: PubNubHashedPage?)
Type: object
Returned tuple containing these fields: files and page.
 → files
Type: [GetFileItem]
Array containing file details.
 → page
Type: PubNubHashedPage
Pagination details.
 → → next
Type: 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.
 → → total
Type: Int
Total number of files.

GetFileItem contains the following properties:

ParameterDescription
name
Type: String
Name of the file, like error-1.jpg.
id
Type: String
Unique identifier assigned to the file by PubNub, like 736499374.
url
Type: String
File's direct downloadable URL, like https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg.

Sample code

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

List all files published on the support channel.

1

Delete 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) async throws

Input

* required
ParameterDescription
id *
Type: String
Default:
n/a
Unique identifier assigned to the file by PubNub.
name *
Type: String
Default:
n/a
Name of the file.

Output

ParameterDescription
Void
Indicates that the operation completed successfully with no additional data.

Sample code

Sample code

The code samples in Swift Chat SDK focus on asynchronous code execution.

You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.

Remove a file named error-screenshot.png from the support channel.

1

Last updated on